我一直在努力解决这个问题。请参阅下面的代码。我试图将HighscorePaneel添加到大型机。我已经制作了highscorePaneel和mainFrame。但行
mainFrame.add(highscorePaneel);
在eclipse中给出错误" mainFrame无法解析"而且我不知道为什么。
你能告诉我我做错了什么吗?
package lunarlockout;
public class LunarLockoutApp
{
public static void main(String[] args)
{
Mainframe mainFrame = new Mainframe();
}
}
第二课(删除了一些不需要发布的代码)
package lunarlockout;
public class Mainframe extends JFrame {
public Mainframe(){
setSize(1200, 800);
setTitle("Lunar Lockout!");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
HoofdmenuPaneel hoofdmenuPaneel = new HoofdmenuPaneel();
add(hoofdmenuPaneel);
setVisible(true);
}
}
第三类
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();
}
}
答案 0 :(得分:0)
将Mainframe
对象传递给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);
...
}
}