Java Can';t似乎没有从另一个类向框架添加面板
我已经为此挣扎了一段时间。请参阅下面的代码。我正在尝试将HighscorePaneel添加到主机。我制作了highscorePaneel和mainFrame。但是这条线Java Can';t似乎没有从另一个类向框架添加面板,java,Java,我已经为此挣扎了一段时间。请参阅下面的代码。我正在尝试将HighscorePaneel添加到主机。我制作了highscorePaneel和mainFrame。但是这条线 mainFrame.add(highscorePaneel); 在eclipse中给出了一个错误“大型机无法解决”,我不知道为什么 你能告诉我我做错了什么吗 package lunarlockout; public class LunarLockoutApp { public static void
mainFrame.add(highscorePaneel);
在eclipse中给出了一个错误“大型机无法解决”,我不知道为什么
你能告诉我我做错了什么吗
package lunarlockout;
public class LunarLockoutApp
{
public static void main(String[] args)
{
Mainframe mainFrame = new Mainframe();
}
}
第二类(删除了一些不需要发布的代码)
三等舱
public class HighscorePaneel extends JPanel implements ActionListener {
public HighscorePaneel(){
setLayout(null);
setSize(330, 528);
setLocation(461, 137);
setBackground( new Color(255, 255, 255, 25) );
setVisible(true);
}
还有我的最后一节课,有错误的那节课:
package lunarlockout;
public class HighscoreInzien {
Mainframe mainFrame;
public HighscoreInzien(Mainframe mainFrame) {
this.mainFrame = mainFrame;
}
public void run() {
HighscorePaneel highscorePaneel = new HighscorePaneel();
mainFrame.add(highscorePaneel);
throw new UnsupportedOperationException();
}
}
HoofdmenuPaneel类
public class HoofdmenuPaneel extends JPanel implements ActionListener {
private JButton selecteerLevelButton;
private JButton spelHervattenButton;
private JButton highscoresButton;
private JButton afsluitenButton;
public HoofdmenuPaneel(Mainframe mainFrame){
this.mainFrame = mainFrame;
setLayout(null);
setSize(330, 528);
setLocation(461, 137);
setBackground( new Color(255, 255, 255, 25) );
public void actionPerformed(ActionEvent e) {
if (e.getSource() == selecteerLevelButton)
{
System.out.println("selecteerLevelButton");
}
else if(e.getSource() == spelHervattenButton)
{
System.out.println("spelHervattenButton");
}
else if(e.getSource() == highscoresButton)
{
this.hide();
HighscoreInzien highscoreInzienTask = new HighscoreInzien(mainFrame);
highscoreInzienTask.run();
}
}
将
大型机
对象传递给HighscoreInzien
类
这样做:
public class HighscoreInzien {
Mainframe mainFrame;
public HighscoreInzien(Mainframe mainFrame) {
this.mainFrame = mainFrame;
}
...
}
public class HoofdmenuPaneel extends JPanel implements ActionListener {
Mainframe mainFrame;
public HoofdmenuPaneel(Mainframe mainFrame){
this.mainFrame = mainFrame;
...
}
}
public class Mainframe extends JFrame {
public Mainframe(){
...
HoofdmenuPaneel hoofdmenuPaneel = new HoofdmenuPaneel(this);
...
}
}
在
HighscoreInzien
类中没有名为mainFrame
的变量。是否所有类都已成功编译?是HighscorePaneel
提供了所需的所有实现。aaaaaah,这很有意义!但这是我的主要。。如何将其链接到我的主要方法?@Braj,我的程序中没有其他错误,这是唯一的一个。它也正是我想要它做的。我忘了在调用HighscoreInzien的run方法的地方添加一个类,但这是可行的,所以我想不需要发布。HoofdmenuPaneel类在哪里?现在添加了它,不完整,无法显示所有按钮。这解决了HighscoreInzien
类中的错误,但在myHoofdmenuPaneel
中创建一个新的。它表示构造函数HighscoreInzien()未定义。我知道我必须在那里通过一些ARG。但我想不出是哪一个,以及如何。又是大型机了吗?对其他类也是这样。以相同的方式创建构造函数以传递大型机
。甚至传递到大型机
本身?因为当我这样做时,我的主类会给出一个错误。“构造函数Mainframe()未定义”-我现在将编辑上面的代码到我在mcallHoofdmenuPaneel HoofdmenuPaneel=new HoofdmenuPaneel(此)在大型机
类中执行代码>操作。不要在大型机
类中执行此操作。只需为HighscoreInzien
和HoofdmenuPaneel
这样做即可。
public class HighscoreInzien {
Mainframe mainFrame;
public HighscoreInzien(Mainframe mainFrame) {
this.mainFrame = mainFrame;
}
...
}
public class HoofdmenuPaneel extends JPanel implements ActionListener {
Mainframe mainFrame;
public HoofdmenuPaneel(Mainframe mainFrame){
this.mainFrame = mainFrame;
...
}
}
public class Mainframe extends JFrame {
public Mainframe(){
...
HoofdmenuPaneel hoofdmenuPaneel = new HoofdmenuPaneel(this);
...
}
}