0

我正在做一个项目,我们正在学习数组。我需要创建一个有 12 个按钮但需要在数组中的 GUI。我的代码还没有完成,因此它可能会因此出现错误。

我收到错误的地方在我的

JButton[] = { new Jbutton("1"), ...}; 

第二个]下面有一条红线,给了我错误

Syntax error on token "]" VariableDeclaratorld expected after this token

到目前为止,这是我的代码:

public class TextButtonsHW extends JFrame implements ActionListener {
private JButton[] buttons;  
private JTextArea textArea; 
private final int ENTER;    
private final int SPACE;    
private final int CLEAR;    

public TextButtonsHW(String title) {
    super(title); 
    JButton[] = { new JButton("A"), new JButton("B"), new JButton("C"),
                  new JButton("1"), new JButton("2"), new JButton("3"),
                  new JButton("X"), new JButton("Y"), new JButton("Z"),
                  new JButton("Enter"), new JButton("Space"), new JButton("Clear")};
    }
}
4

3 回答 3

2
JButton[] = {

应该是这样的:

JButton[] buttonArray = {
于 2013-04-21T04:55:51.353 回答
1

您已声明buttons为实例变量:

private JButton[] buttons;  

因此,您需要像这样设置该变量:

buttons = new JButton[] { new JButton("A") ...
于 2013-04-21T04:57:31.540 回答
1

变量名在哪里???

 JButton[] buttons = { new JButton("A"), new JButton("B"), new JButton("C"),
              new JButton("1"), new JButton("2"), new JButton("3"),
              new JButton("X"), new JButton("Y"), new JButton("Z"),
              new JButton("Enter"), new JButton("Space"), new JButton("Clear")};
于 2013-04-21T04:58:30.627 回答