Chris
Chris

Reputation: 256

GUI buttons in array

I am working on a project and we are learning arrays. I need to create a GUI that has 12 buttons but need to be in an array. I am not finished with my code, so it may have errors because of that.

Where I am getting an error is on my

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

the 2nd ] has a red line under it and gives me the error

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

Heres my code so far:

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

Upvotes: 0

Views: 1296

Answers (3)

Fls'Zen
Fls'Zen

Reputation: 4654

You have declared buttons as an instance variable:

private JButton[] buttons;  

So you need to set that variable as so:

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

Upvotes: 1

aymankoo
aymankoo

Reputation: 671

Where is the name of the variable???

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

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168815

JButton[] = {

Should be something like:

JButton[] buttonArray = {

Upvotes: 2

Related Questions