Java Applet - Adding a Button to Decrease a Total

Java Applet - Adding a Button to Decrease a Total

Hello everyone! Here is my current code:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Final extends Applet implements ActionListener
{
    public int testone = 0;
    public void init()
    {
        Button one = new Button("Increase");
        one.addActionListener(this);
        add(one);
    }
    public void paint(Graphics g)
    {
        g.drawString("Total: " + gettestone(),20,50);
    }
    public void actionPerformed(ActionEvent e)
    {
        increasetestone();
        repaint();
    }
    public synchronized int gettestone()
    {
        return testone;
    }
    public synchronized void increasetestone() {
        testone++;
    }
}

When you run this current code, it will display a button in a frame, that when clicked on will increase a total. Basically it will count the total number of times you click the button.

What I need is for their to be a second button that when clicked will decrease that total amount. So basically there would be one button that when clicked, would increase that value, and another that when clicked, will decrease the value.

Thanks so much for your help!

View Answers

June 8, 2012 at 5:33 PM

Here is an applet program that will increment and decrement the integer value. There are two buttons in an applet for increment and decrement respectively. On clicking these button, value will get incremented and decremented.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Final extends Applet {
    public int testone = 0;
    public void init()    {
        Button one = new Button("Increase");
        Button two = new Button("Decrease");
        one.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e) {
        increasetestone();
        repaint();
          }
        });
         two.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e) {
        decreasetestone();
        repaint();
          }
        });
        add(one);
        add(two);
    }
    public void paint(Graphics g)
    {
        g.drawString("Total: " + gettestone(),20,50);
    }

    public synchronized int gettestone()
    {
        return testone;         
    }
    public synchronized void increasetestone() {
        testone++;
    }
    public synchronized void decreasetestone() {
        testone--;
    }
}









Related Tutorials/Questions & Answers:
Hide Close Button in Java Applet - Java Server Faces Questions
Hide Close Button in Java Applet  I have an applet while running...... button or close button. Java Applet Code - Show and Hide Buttons on Applet  Java Applet code to display minimize and maximize button on Applet and code
Not getting desired output while button is pressed in java applet program
Not getting desired output while button is pressed in java applet program ... T1 and T2 get subtracted and comes in T3 after we press the button.../// import...*; import java.awt.Font; public class abc extends Applet implements ActionListener
Advertisements
How to decrease image thumbnail size in java?
How to decrease image thumbnail size in java?  Hi, I my web application I have to create the thumbnail size image from the image file uploaded by user. How to decrease image thumbnail size in java? Thanks
java applet - Applet
java applet  I want to close applet window which is open by another button of applet program. plz tell me!   Hi Friend, Try...://www.roseindia.net/java/example/java/applet/ Thanks
Write an applet that prints "Lear Java it is useful" at the current cursor position whenever the mouse left button is clicked.
Write an applet that prints "Lear Java it is useful" at the current cursor position whenever the mouse left button is clicked.   Write an applet that prints "Lear Java it is useful" at the current cursor position whenever
java applet problem - Applet
java applet problem  i was doing a program using java applet. I want... for add,multiply,divide,minus and a clear and exit button to clear the textboxes and to exit from the applet respectively.Now i want to display a message when
java applet - Applet
java applet  wants to creat address bar on my java applet.  Hi Applet don't provide a facility to create a address bar directly. You just create a text box. In this text box if you enter any http address
loading Java Applet - Applet
loading Java Applet  Hi, I use the archive tag to download my applet into the Browser It takes too long to load. Can I do it in several steps... control the loading within the applet? thanks
Java Applet
Java Applet   How to add Image in Java Applet? what is getDocumentBase
java applet
java applet  Creation of Applet for Personal Details Form When.... The applet must include the following validations: ď?® The FirstName, Address1... format, and should be later than the current date. Create an OK button
Applet
Applet  I have a java applet that has a button. On clicking the button it should disappear and another applet should appear. How to write this code???? Also in login applet after successful login it should display admin applet
java - Applet
java  how to connect database table with scrollbar in java applet
java - Applet
java  how to connect database table with scrollbar in java applet
java - Applet
java  what is applet?  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/applet/ Thanks
java - Applet
java  i have button browse button in my applet form.when i click on browse button i have to go to file storage box(what ever files stored in my... in the mail).i have display that file path in my applet form.am learner please send me
java applet
java applet  why java applet programs doesn't contain main method
java applet
java applet  why java applet programs doesn't contain main method
Java applet
Java applet  What tags are mandatory when creating HTML to display an applet
Java applet
Java applet   How do I go from my applet to another JSP or HTML page
java applet
java applet  If i insert in database from applet this work, but from applet.html don't
java - Applet
java  what is applet
Java Applet
Java Applet  Hi, What is Java Applet and how it can be useful in development of Java programs? Can anyone tell me the url to learn Java Applet? Thanks   Hi, See the tutorial at: Applications and Applets Thanks
java - Applet
java  how to communicate to my servlet through applet.   ... this information to the servlet in the normal way. The applet pretends... is concerned, the applet is just another HTTP client. For more infomation
Java program to generate the total count
Java program to generate the total count  I need a java program which would take a text file as an input and scans through it to generate the output. For Ex. : If the text file contains the following information: There is/are 3
java applet
java applet   I want code to implement (1) user will enter how many nodes (2)it should allow that no. of clicks and circle should be displayed at that position
Write an applet program to transfer the content of the text field into the component on clicking a button
Write an applet program to transfer the content of the text field into the component on clicking a button  Write an applet program to transfer the content of the text field into the component on clicking a button
java applet
java applet  Web page that accepts a matrix as input and computes its transpose. The web page should have two text boxes and a submit button labbeled... of the web page, Submit button should be clicked. Once clicked, a number of text boxes
java applet
java applet  Web page that accepts a matrix as input and computes its transpose. The web page should have two text boxes and a submit button labbeled... of the web page, Submit button should be clicked. Once clicked, a number of text boxes
java total lost data in hard drive
java total lost data in hard drive  please help me, i need java source code about total lost data in hard drive. how to find the total deleted data ih hard drive using java programming... this my project...pls help me
java total lost data in hard drive
java total lost data in hard drive  please help me, i need java source code about total lost data in hard drive. how to find the total deleted data ih hard drive using java programming... this my project...pls help me
java total lost data in hard drive
java total lost data in hard drive  please help me, i need java source code about total lost data in hard drive. how to find the total deleted data ih hard drive using java programming... this my project...pls help me
JAVA TOTAL LOST DATA IN HARD DRIVE
JAVA TOTAL LOST DATA IN HARD DRIVE  please help me, i need java source code about total lost data in hard drive. how to find the total deleted data ih hard drive using java programming... this my project...pls help me
java applet prog
java applet prog  applet to display scrolling text from right to left in an applet window using thread. text should be accepted by html parameter
java - Applet
java  1.An applet program to draw a line graph for y=2x+5.[for suitable values of x & y] 2. An applet program to draw following shapes (1)cone... java.applet.Applet; public class CubeExample extends Applet { Stroke
need a login applet in java
need a login applet in java  i'm java beginner. Can some java master teach me how to write a login applet? combine with my coding import... java.text.*; import java.awt.event.*; public class BoatRental extends Applet implements
need a login applet in java
need a login applet in java  i'm java beginner. Can some java master teach me how to write a login applet? combine with my coding import... java.text.*; import java.awt.event.*; public class BoatRental extends Applet implements
core java - Applet
core java  how can draw rectangle in applet. when i pressed in mouse button the rectangle's size will also increase
The Java Applet Viewer
The Java Applet Viewer       Applet viewer is a command line program to run Java applets...; the browser should be Java enabled.To create an applet, we need to define
Java Programming - Applet
Java Programming  Write an applet that accepts a string as input... Applet implements ActionListener { Label l1,l2,l3; TextField text; Button b... = new Button("Reverse"); b.addActionListener(this); p.add(l1); p.add(text
disable keyboard in java applet
disable keyboard in java applet  How to disable key board of my client in an java applet embedded in a website while the applet is running.? Plz help
Applet - Passing Parameter in Java Applet
Applet - Passing Parameter in Java Applet       Introduction Java applet has the feature... passed from html page. Applet will display "Hello! Java Applet" 
Java applet
Java applet  What is AppletStub Interface
Java applet
Java applet  what are types of applets
Java applet
Java applet  How do you set security in applets
Java applet
Java applet   How are the differences between Applets and Applications
Java applet
Java applet  What is the sequence for calling the methods by AWT for applets
Java applet
Java applet  What is the relationship between the Canvas class and the Graphics class
Java applet
Java applet  What is the relationship between the Canvas class and the Graphics class
java - Applet
java  what is the use of java.utl  Hi Friend, The java...://www.roseindia.net/java/example/java/util/ http://www.roseindia.net/java/example/java/util/java-util-package.shtml Thanks
Java applet
Java applet  How do I determine the width and height of my application

Ads