Java Applet - Adding a Button to Decrease a Total

Ads
 

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--;
    }
}

Ads









Related Tutorials/Questions & Answers:
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
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
Advertisements
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
Java Applet   How to add Image in Java Applet? what is getDocumentBase
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
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
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  If i insert in database from applet this work, but from applet.html don't
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 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
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
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 applet  create applets to accept the personal details and sales details in the database. create jdbc application to connect the applets with the database
java - Applet
java  Hi, I need very urgent code............ please help me......... How to convert text to wave conversion? I need java code............ Regards, Valarmathi
java - Applet
://www.roseindia.net/java/example/java/swing/graphics2D/index.shtml Thanks
Java applet
Java applet  I wanted as many clicks are there ,circles should be displayed there. I tried this code but it erases previous circles. plz help. public void mouseClicked(MouseEvent m) { x=m.getX(); y=m.getY
Java applet
Java applet  I wanted as many clicks are there ,circles should be displayed there. I tried this code but it erases previous circles. plz help. public void mouseClicked(MouseEvent m) { x=m.getX(); y=m.getY
Java Applet
Java Applet  Hi, I have a query, on every mouse click an oval should be drawn. But in my program I have used repaint() function, therefore previous oval gets erased.How to avoid this so that I get more than one oval. PFB
Java Applet
Java Applet  Hi, I have a query, on every mouse click an oval should be drawn. But in my program I have used repaint() function, therefore previous oval gets erased.How to avoid this so that I get more than one oval. PFB
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
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
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 applet notpad
java applet notpad  i need Codes Or programe in java applet ; i want add this programe to my site html > please help me our   Learn Java...: Java Applet Tutorials
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
Simple java applet
Simple java applet  Create a Java applet to display your address on the screen. Use different colors for background and text   Please visit the following link: Applet Examples
Applet - Passing Parameter in Java Applet
Applet - Passing Parameter in Java Applet       Introduction Java applet has the feature... page. Applet will display "Hello! Java Applet"  if no parameter
Java Application change to java applet
Java Application change to java applet   Hi every Java Masters i'm Java beginner ,and i want below this figures source code convert to Java Applet... extends Applet implements ActionListener{ TextField text,output; Label
java Application to java Applet
java Application to java Applet  sorry Java master disturb again how can i modify below this Java application to applet because in swtich case i had...; System.out.println("Rental Total Charges= "+df.format(amount)); System.out.println
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

Ads