我想創建一個非常基本的統計計算器程序的自定義面板。我有從1到9的按鈕輸入數字,以及一個內部類實現ActionListener
來處理它們。這些號碼應顯示在名爲display
的JLabel
上。該程序編譯正確,但是,聽衆似乎沒有工作,沒有任何反應,當我點擊這些按鈕。下面是代碼:ActionListener不起作用
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);
}
}
這個工作正常關聯的所有其他類,等等都是其他按鈕的聽衆。
你應該認真考慮使用適當的LayoutManager的,而不是設置手動綁定。 –