Hi all, I have a few questions regarding a program meant to output some simple information. I created an application with a frame, panel, JButtons, and JLabels that all involve displaying an integer that increments or decrements by 1 when pressed by either button, respectively. My problem is that everything is displayed correctly, but the JLabel displaying the integer doesn't update when either button is pressed. Here's my code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AddSubtractGUI { private int WIDTH = 300; private int HEIGHT = 75; private int pushTotal = 50; //integer that is incremented/decremented private JFrame frame; private JPanel panel; private JLabel totalDisplay, pushTotalDisplay; //displays the push total private JButton addPush, subPush; //addition and subtraction buttons public AddSubtractGUI() { frame = new JFrame ("Add/Subtract"); //title of application frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); //closes application totalDisplay = new JLabel ("Total : "); pushTotalDisplay = new JLabel ("50"); addPush = new JButton ("Add!"); subPush = new JButton ("Subtract!"); panel = new JPanel(); panel.setPreferredSize (new Dimension(WIDTH, HEIGHT)); panel.setBackground (Color.cyan); panel.add (totalDisplay); panel.add (pushTotalDisplay); panel.add (addPush); panel.add (subPush); frame.getContentPane().add (panel); //package to display all components of app } public void display() //enables frame { frame.pack(); frame.setVisible(true); } private class AddListener implements ActionListener //addition button upon press { public void actionPerformed (ActionEvent event) { pushTotal++; pushTotalDisplay.setText(Integer.toString (pushTotal) ); } } private class SubListener implements ActionListener //subtraction button upon press { public void actionPerformed (ActionEvent event) { pushTotal--; pushTotalDisplay.setText(Integer.toString (pushTotal) ); } } } xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx public class AddSubtractDriver //driver program { public static void main (String[] args) { AddSubtractGUI converter = new AddSubtractGUI(); converter.display(); } }
I'm trying to get my JButtons to update properly so that the push total updates when they are pressed, and corresponds with the operation that they are labeled as.
I run my code through my driver as an application, rather than as an applet. I apologize for any ambiguity, this is my first post. Thanks!