2013-04-21 116 views
0

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

当我得到一个错误是我

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

第2]有下有一个红色的线和给我的错误

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")}; 
    } 
} 
+0

一般提示。不要扩展'JFrame',只需使用一个框架的实例。 – 2013-04-21 04:57:00

回答

2
JButton[] = { 

应该是这样的:

JButton[] buttonArray = { 
1

您已经声明buttons作为一个实例变量:

private JButton[] buttons; 

因此,你需要这个变量设置为这样:

buttons = new JButton[] { new JButton("A") ... 
+1

初始化数组的这种形式只有在声明变量的同时声明数组时才有效。 – 2013-04-21 05:02:38

+0

哎呀,错过了。增加了初始化来回答。 – 2013-04-21 05:03:33

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")};