gpt4 book ai didi

java - 我的表达是非法的,我不知道为什么

转载 作者:行者123 更新时间:2023-12-01 17:09:02 25 4
gpt4 key购买 nike

当我编译此行时,第 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 语句不适用于对象。

关于java - 我的表达是非法的,我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24441831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com