0

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")};
    }
}
1
  • General tip. Don't extend JFrame, just use an instance of a frame. Commented Apr 21, 2013 at 4:57

3 Answers 3

2
JButton[] = {

Should be something like:

JButton[] buttonArray = {
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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") ...

2 Comments

That form of initializing an array is only valid when the array is declared at the same time as the variable is declared.
Whoops, missed that. Added initialization to answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.