Search code examples
javaswinglayoutjpanelboxlayout

BoxLayout: Compilation Errors


I'm trying to make a BoxLayout layout in my program. I use Eclipse, so when I entered these lines,

JTextArea fntxt = new JTextArea(1, 20);
JTextArea lntxt = new JTextArea(1, 20);
JButton back4 = new JButton("Back");
JButton sub2 = new JButton("Submit");
JLabel cal = new JLabel("Create new Account");
JPanel cac = new JPanel();
//Error line:
cac.setLayout(new BoxLayout(cac, BoxLayout.PAGE_AXIS));

//In Constructor
    cal.setFont(new Font("Times New Roman", Font.PLAIN, 24));
    cac.add(cal);
    cac.add(new JLabel("First Name:"));
    cac.add(fntxt);
    cac.add(new JLabel("Last Name"));
    cac.add(lntxt);
    cac.add(back4);
    cac.add(sub2);

it showed these errors in the error line:

- Syntax error on token ".", @ expected after this token
- Syntax error, insert "Identifier (" to complete 
 MethodHeaderName
- Syntax error, insert ")" to complete MethodDeclaration
- Syntax error, insert "SimpleName" to complete 
 QualifiedName

and when I try to run it, this comes up in the console:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

at Create.main(Create.java:125)

The 125 line is the main method declaration line. I can't understand what is the error and what to do to debug it.


Solution

  • There is an extra closing bracket, also this line has to be inside a method or constructor:

    inside constructor

    cac.setLayout(new BoxLayout(cac, BoxLayout.PAGE_AXIS)));
                                         remove this------^