BoxLayout:编译错误

时间:2014-12-11 12:14:27

标签: java swing layout jpanel boxlayout

我试图在我的程序中制作BoxLayout布局。我使用Eclipse,所以当我输入这些行时,

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

它在错误行中显示了这些错误:

- 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

当我尝试运行它时,会出现在控制台中:

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

at Create.main(Create.java:125)

125行是主要方法申报行。 我无法理解错误是什么以及如何调试它。

1 个答案:

答案 0 :(得分:3)

还有一个额外的结束括号,此行也必须位于方法或构造函数中:

内部构造函数

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