Ok so I have to use two buttons, one increment and one decrement. Also I must display a label with a number in it. When one of the buttons are pushed, the number in the label will either decrease or increase. I can't figure out how to change the number in the label once the button is pushed. I can get everything displayed right, but can't get the buttons to work. Here is my code:
HTML Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Number extends JPanel
{
private JLabel figure;
private JButton Increment;
private JButton Decrement;
private int value;
public Number (){
Increment = new JButton ("Increment");
Decrement = new JButton ("Decrement");
value = 50;
figure = new JLabel ("Value of Number: " + value );
Increment.addActionListener (new ButtonListener1());
//Decrement.addActionListener (new ButtonListener2());
add (Increment);
add (Decrement);
add (figure);
setPreferredSize (new Dimension(300, 100));
setBackground (Color.green);
}
private class ButtonListener1 implements ActionListener{
public void actionPerformed (ActionEvent event){
int incrementNumber;
Random generator = new Random();
incrementNumber = generator.nextInt(40) +10;
value = value + incrementNumber;
}
}
}
I know I only have one of the buttonlisteners set-up...just trying to get the 1st working first. Also here is the other class, but probably not needed:
HTML Code:
import javax.swing.JFrame;
public class NumberPanel{
public static void main (String[] args){
JFrame frame = new JFrame ("Number Program");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Number());
Number panel = new Number();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}