I am trying to create a custom panel for a very basic statistical calculator program. I have buttons from one to 9 to enter the numbers, and a single inner class which implements ActionListener to handle them. The numbers should show up on a JLabel called display. The program compiles properly, however, the listener does not seem to work, and nothing happens when I click on those buttons. Here is the code :
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
@SuppressWarnings("serial") public class StatCalcPanelBeta extends JPanel{
private class InputHandler implements ActionListener{
@Override public void actionPerformed(ActionEvent evt){
String str = evt.toString();
switch(str){
case "1":
inputString.append("1");
inputField.setText(inputString.toString());
break;
case "2":
inputString.append("2");
inputField.setText(inputString.toString());
break;
case "3":
inputString.append("3");
inputField.setText(inputString.toString());
break;
case "4":
inputString.append("4");
inputField.setText(inputString.toString());
break;
case "5":
inputString.append("5");
inputField.setText(inputString.toString());
break;
case "6":
inputString.append("6");
inputField.setText(inputString.toString());
break;
case "7":
inputString.append("7");
inputField.setText(inputString.toString());
break;
case "8":
inputString.append("8");
inputField.setText(inputString.toString());
break;
case "9":
inputString.append("9");
inputField.setText(inputString.toString());
break;
}
}
}
private final StatCalc calculator;
private JLabel display;
private JPanel displayPanel;
private JTextField inputField;
private final StringBuffer inputString;
private JButton enter;
private JButton button_1;
private JButton button_2;
private JButton button_3;
private JButton button_4;
private JButton button_5;
private JButton button_6;
private JButton button_7;
private JButton button_8;
private JButton button_9;
private JPanel inputPanel;
private InputHandler ioListener;
private JButton count;
private JButton sum;
private JButton mean;
private JButton max;
private JButton min;
private JButton standardDeviation;
private JPanel functionPanel;
public StatCalcPanelBeta(){
calculator = new StatCalc();
inputString = new StringBuffer();
initialize();
}
private void initialize(){
setLayout(null);
displayPanel = new JPanel();
displayPanel.setBorder(new TitledBorder(new BevelBorder(BevelBorder.LOWERED, null, null,
null, null), "Display", TitledBorder.LEADING, TitledBorder.TOP, null, null));
displayPanel.setBounds(10, 11, 190, 53);
add(displayPanel);
displayPanel.setLayout(null);
display = new JLabel("Display");
display.setBounds(0, 11, 180, 37);
displayPanel.add(display);
display.setHorizontalAlignment(SwingConstants.CENTER);
inputPanel = new JPanel();
inputPanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null),
"Input", TitledBorder.LEADING, TitledBorder.TOP, null, null));
inputPanel.setBounds(0, 72, 211, 145);
this.add(inputPanel);
inputPanel.setLayout(null);
ioListener = new InputHandler();
inputField = new JTextField(inputString.toString());
inputField.setEditable(false);
inputField.setBounds(9, 16, 86, 20);
inputPanel.add(inputField);
inputField.setColumns(10);
enter = new JButton("Enter");
enter.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent arg0){
try{
calculator.enter(Double.parseDouble(inputString.toString()));
} catch(NumberFormatException e){
e.printStackTrace();
}
display.setText("");
}
});
enter.setToolTipText("Enters the number displayed on the input field");
enter.setBounds(110, 16, 89, 23);
inputPanel.add(enter);
button_1 = new JButton("1");
button_1.setBounds(6, 47, 65, 23);
button_1.addActionListener(ioListener);
inputPanel.add(button_1);
button_2 = new JButton("2");
button_2.setBounds(73, 47, 65, 23);
button_2.addActionListener(ioListener);
inputPanel.add(button_2);
button_3 = new JButton("3");
button_3.setBounds(140, 47, 65, 23);
button_3.addActionListener(ioListener);
inputPanel.add(button_3);
button_4 = new JButton("4");
button_4.setBounds(6, 81, 65, 23);
button_4.addActionListener(ioListener);
inputPanel.add(button_4);
button_5 = new JButton("5");
button_5.setBounds(73, 81, 65, 23);
button_5.addActionListener(ioListener);
inputPanel.add(button_5);
button_6 = new JButton("6");
button_6.setBounds(140, 81, 65, 23);
button_6.addActionListener(ioListener);
inputPanel.add(button_6);
button_7 = new JButton("7");
button_7.setBounds(6, 115, 65, 23);
button_7.addActionListener(ioListener);
inputPanel.add(button_7);
button_8 = new JButton("8");
button_8.setBounds(73, 115, 65, 23);
button_8.addActionListener(ioListener);
inputPanel.add(button_8);
button_9 = new JButton("9");
button_9.setBounds(140, 115, 65, 23);
button_9.addActionListener(ioListener);
inputPanel.add(button_9);
functionPanel = new JPanel();
functionPanel.setBorder(new TitledBorder(
new EtchedBorder(EtchedBorder.LOWERED, null, null), "Functions",
TitledBorder.LEADING, TitledBorder.TOP, null, null));
functionPanel.setBounds(0, 228, 211, 113);
this.add(functionPanel);
functionPanel.setLayout(null);
count = new JButton("Count");
count.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent arg0){
display.setText(Integer.toString(calculator.getCount()));
}
});
count.setBounds(6, 16, 65, 23);
functionPanel.add(count);
sum = new JButton("Sum");
sum.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent arg0){
display.setText(Double.toString(calculator.getSum()));
}
});
sum.setBounds(73, 16, 65, 23);
functionPanel.add(sum);
mean = new JButton("Mean");
mean.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent arg0){
display.setText(Double.toString(calculator.getMean()));
}
});
mean.setBounds(140, 16, 65, 23);
functionPanel.add(mean);
max = new JButton("Maximum");
max.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent arg0){
display.setText(Double.toString(calculator.getMax()));
}
});
max.setBounds(10, 49, 89, 23);
functionPanel.add(max);
min = new JButton("Minimum");
min.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent arg0){
display.setText(Double.toString(calculator.getMin()));
}
});
min.setBounds(111, 49, 89, 23);
functionPanel.add(min);
standardDeviation = new JButton("Standard Deviation");
standardDeviation.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent arg0){
display.setText(Double.toString(calculator.getStandardDeviation()));
}
});
standardDeviation.setBounds(33, 83, 147, 23);
functionPanel.add(standardDeviation);
}
}
All other classes associated with this are working properly, and so are the listeners of the other buttons.
This will never return “1” (or any of the other digits). If you want to retrieve the button, use
evt.getSource()and cast it to aJButton. From thatJButtonyou can then retrieve the correct information.The other option is to use the action-command. See
JButton#setActionCommandandActionEvent#getActionCommand