此代码将在NetBeans上为这些行返回三个错误:
leftPanel.setLayout (leftBox );
leftPanel.add (optionBar);
leftPanel.add (tabBar);
leftPanel.add (paintArea);
rightPanel.setLayout (rightBox);
rightPanel.add (toolbar);
rightPanel.add (toolArea);
它返回:
package rightPanel does not exist
<identifier> expected
missing method body, or declare abstract
<identifier> expected
----
该计划也存在这方面的问题:
setLayout(flo);
add(leftPanel);
add(rightPanel);
并返回:
无效的方法声明;需要返回类型
我已尝试搜索并找到所有类型问题的信息,但我无法找到与我个人问题直接对应的任何内容。
这里是完整的代码:
package level.builder;
import javax.swing.*;
import java.awt.*;
public class LevelBuilderGUI extends JFrame {
//Version For the Title
private static String version = "Alpha";
//Creating leftmost panel
JPanel leftPanel = new JPanel();
BoxLayout leftBox = new BoxLayout(leftPanel, BoxLayout.Y_AXIS);
leftPanel.setLayout (leftBox );
JPanel optionBar = new JPanel();
leftPanel.add (optionBar);
JPanel tabBar = new JPanel();
leftPanel.add (tabBar);
JPanel paintArea = new JPanel();
leftPanel.add (paintArea);
//Creating rightmost panel
JPanel rightPanel = new JPanel();
BoxLayout rightBox = new BoxLayout(rightPanel, BoxLayout.Y_AXIS);
rightPanel.setLayout (rightBox);
JPanel toolBar = new JPanel();
rightPanel.add (toolbar);
JPanel toolArea = new JPanel();
rightPanel.add (toolArea);
//Putting it all together
FlowLayout flo = new FlowLayout();
setLayout(flo);
add(leftPanel);
add(rightPanel);
public LevelBuilderGUI() {
super();
setTitle("Level Builder version" + version);
setLookAndFeel();
setSize(512, 128);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel);");
} catch (Exception e) {
}
}
}
请帮助我理解我的错误。
编辑:感谢所有帮助,我看到我遇到的问题是将所有代码放在方法之外。代码现在在被移入构造函数后运行。
答案 0 :(得分:0)
尝试将所有这些行放在构造函数中。您正在获取这些错误,因为您正在调用&#34; body&#34;上的属性的方法。在任何封装之外,你的班级。
这里有一些建议:检查属性的可见性,从来没有一个好的做法让他们像那样自由。 ;)