当我编译此行74作为非法的表达开始时,为什么会这样?
我一直无法弄清楚,任何帮助将不胜感激
谢谢。
import java.awt.*;
import javax.swing.*;
public class NumericKeypadPanel2 extends JPanel
{
public NumericKeypadPanel2()
{
String num = " ";
JPanel panel_main = new JPanel();
panel_main.setLayout(new BorderLayout());
JPanel keypad = new JPanel();
keypad.setLayout(new GridLayout (4, 3));
keypad.setBorder (BorderFactory.createLineBorder (Color.black, 3));
JButton b1 = new JButton ("1");
JButton b2 = new JButton ("2");
JButton b3 = new JButton ("3");
JButton b4 = new JButton ("4");
JButton b5 = new JButton ("5");
JButton b6 = new JButton ("6");
JButton b7 = new JButton ("7");
JButton b8 = new JButton ("8");
JButton b9 = new JButton ("9");
JButton b10 = new JButton ("*");
JButton b11 = new JButton ("0");
JButton b12 = new JButton ("#");
NumListener listener = new NumListener();
b1.addActionListener (listener);
keypad.add (b1);
keypad.add (b2);
keypad.add (b3);
keypad.add (b4);
keypad.add (b5);
keypad.add (b6);
keypad.add (b7);
keypad.add (b8);
keypad.add (b9);
keypad.add (b10);
keypad.add (b11);
keypad.add (b12);
panel_main.add(keypad, BorderLayout.CENTER);
JButton clear = new JButton ("Clear");
JLabel display = new JLabel(num);
panel_main.add(clear, BorderLayout.SOUTH);
panel_main.add(display, BorderLayout.NORTH);
display.setBorder (BorderFactory.createLineBorder (Color.black, 3));
add(panel_main);
private class NumListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
Object source = event.getSource();
switch (source)
{
case b1:
display.setText(display.getText()+"1");
break;
case b2:
display.setText(display.getText()+"2");
break;
case b3:
display.setText(display.getText()+"3");
break;
case b4:
display.setText(display.getText()+"4");
break;
case b5:
display.setText(display.getText()+"5");
break;
case b6:
display.setText(display.getText()+"6");
break;
case b7:
display.setText(display.getText()+"7");
break;
case b8:
display.setText(display.getText()+"8");
break;
case b9:
display.setText(display.getText()+"9");
break;
case b10:
display.setText(display.getText()+"*");
break;
case b11:
display.setText(display.getText()+"0");
break;
case b12:
display.setText(display.getText()+"#");
break;
}
}
}
}
}
最佳答案
只是一个疯狂的猜测:
Object source = event.getSource();
switch (source){ <-- ERROR
据我所知,switch语句不适用于对象。