当我编译这一行时,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;
}
}
}
}
}
答案 0 :(得分:5)
只是猜测:
Object source = event.getSource();
switch (source){ <-- ERROR
据我所知,switch语句不适用于对象。
答案 1 :(得分:0)
正如爱德华在下面所说,切换语句不适用于常规(?)对象。它们仅适用于int
,short
,char
和byte
类型。他们也使用枚举。这是我的建议 - 使用if / else if语句并为所有按钮添加动作侦听器,因此它们链接到actionPerformed
方法:
@Override
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if(o.equals(b1)) display.setText(display.getText()+"1");
else if(o.equals(b1)) display.setText(display.getText()+"1");
else if(o.equals(b2)) display.setText(display.getText()+"2");
else if(o.equals(b3)) display.setText(display.getText()+"3");
else if(o.equals(b4)) display.setText(display.getText()+"4");
else if(o.equals(b5)) display.setText(display.getText()+"5");
else if(o.equals(b6)) display.setText(display.getText()+"6");
else if(o.equals(b7)) display.setText(display.getText()+"7");
else if(o.equals(b8)) display.setText(display.getText()+"8");
else if(o.equals(b9)) display.setText(display.getText()+"9");
else if(o.equals(b10)) display.setText(display.getText()+"*");
else if(o.equals(b11)) display.setText(display.getText()+"0");
else if(o.equals(b12)) display.setText(display.getText()+"#");
}