这段代码将在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) {
}
}
}
请帮助我理解我的错误。
编辑:感谢所有的帮助,我看到我遇到的问题是将所有代码放在方法之外。现在,将代码移入构造函数后即可运行。
最佳答案
尝试将所有这些行放入构造函数中,以获取错误信息。之所以会出现这些错误,是因为您在类的“主体”中调用任何封装之外的属性的方法。
此处的建议是:检查属性的可见性,从来没有一个好的习惯让它们像这样自由。 ;)
关于java - 编译从Java中的Swing的JFrame类返回缺少的方法和包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38287463/