我正在制作一个类似于嗡嗡声的游戏,其中主要的问题是当我创建JFrame和面板的类从其他类扩展时,因为通过创建该类的项目,我有+1帧。所以我有额外的帧我不想要。代码是这样的:
public static void main(String[] args) {
GameStart game;
game = new GameStart();
game.gReady();
}
public class GameStart extends JFrame{
protected JPanel panel, headline;
protected BorderLayout border;
protected GridLayout grid, head;
protected JLabel question;
protected JButton[] button;
protected JLabel label;
protected JLabel empty;
public GameStart(){
setSize(500,400);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
panel = new JPanel();
headline = new JPanel();
question = new JLabel();
button = new JButton[4];
for(int i=0;i<4;i++){
button[i] = new JButton();
panel.add(button[i]);
}
label = new JLabel();
empty = new JLabel();
border = new BorderLayout();
grid = new GridLayout(4,1);
head = new GridLayout(1,0);
headline.add(question);
headline.setLayout(head);
setLayout(border);
panel.setLayout(grid);
add(panel, BorderLayout.WEST);
add(headline, BorderLayout.PAGE_START);
panel.setVisible(true);
headline.setVisible(true);
setVisible(true);
}
这是有问题的课程,因为我试图找出
public void gReady(){
GameMenu game;
game = new GameMenu();
game.languageMenu();
}
languageMenu()甚至可以为空,但GameMenu()需要扩展GameStart()。有没有办法阻止其他“子”类创建额外的帧?
public class GameMenu extends GameStart {
public GameMenu(){}
public void languageMenu(){}
}
答案 0 :(得分:1)
因为你在GameStart的构造函数中调用了setVisable,所以当你创建它时它会弹出。 从那里删除它并仅在您想要显示JFrame时调用它。
另外,为什么要用GameMenu扩展呢?只需创建不同的JFrame