2013-04-21 113 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")};