I'm just working through an online course and I have replicated a GUI program up to this point, two text fields and six buttons. We are at a point where we are assigning event responses to each button. I have assigned responses to both the Clear button and the Quit button but now all buttons are quitting the program and I'm not sure why. Can anyone help?
import javax.swing.JFrame;
public class CalcTest
{
public static void main(String[] args)
{
Calc application = new Calc();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//ensures program ends when you close the jframe
application.setSize(400 , 400);
application.setVisible(true);
}
}
Second
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.event.ActionListener;//needed for detecting when a user has pressed Enter
import java.awt.event.ActionEvent;
public class Calc extends JFrame
{
private JLabel firstNumberLabel; //use to display text that a user cannot edit
private JTextField firstNumberField;
private JLabel secondNumberLabel;
private JTextField secondNumberField;
private JTextField resultField;
private JPanel buttonPanel = new JPanel();//example of button panel being created outside of constructor
private JButton addButton = new JButton("Add");
private JButton subtractButton = new JButton("Subtract");
private JButton multiplyButton = new JButton("Multiply");
private JButton divideButton = new JButton("Divide");
private JButton clearButton = new JButton("Clear");
private JButton quitButton = new JButton("Quit");
public Calc()
{
setLayout(null);//turn off Layout Manager to manually change layout
firstNumberLabel = new JLabel("Enter First Number: ");//Label used to display text that cannot be edited
firstNumberLabel.setBounds(10, 10, 150, 25);//set location in the frame
add(firstNumberLabel);//this "add" method is inherited from class JFrame
firstNumberField = new JTextField("0.00");//Field used to display text that can be edited
firstNumberField.setBounds(170 , 10 , 150 , 25 );
add(firstNumberField);
secondNumberLabel = new JLabel("Enter Second Number: ");
secondNumberLabel.setBounds(10, 45 , 150, 25);//set location in the frame
add(secondNumberLabel);
secondNumberField = new JTextField("0.00");
secondNumberField.setBounds(170 , 45 , 150 , 25 );
add(secondNumberField);
resultField = new JTextField("0.00");//result of calculation box in centre of frame
resultField.setBounds(100 , 90 , 200 , 50);
resultField.setEditable(false);//ensure the user cannot edit this box
add(resultField);
buttonPanel.setBounds(100 , 160 , 200 , 150);
buttonPanel.setBackground(Color.red); //set colour to make it stand out from the grey
add(buttonPanel);//"add" adds to the JFrame
buttonPanel.setLayout(new GridLayout (3,2));//GridLayout automatically sorts in rows and columns
JButton[] buttons = {addButton , subtractButton , multiplyButton ,
divideButton , clearButton , quitButton};
ButtonHandler handler = new ButtonHandler();
for(JButton button : buttons)
{
buttonPanel.add(button);
button.addActionListener(handler);
}//end for
}//end of constructor for class Calc
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == clearButton)
{
firstNumberField.setText("0.00");
secondNumberField.setText("0.00");
}
else if(event.getSource() == quitButton);
{
System.exit(0);
}
}
}//end of inner class ButtonHandler
}//end class Calc