刚开始使用GUI时,由于某种原因,当我设置默认的关闭操作时,它无法正常工作,而且在添加JLabel和TextField等组件时也是如此。我已经使用JFrame扩展了我的类,并且我的程序中没有其他具有相同名称的JFrame或JPanel。哦,还有,setVisible和setSize仍然可以工作,只是在添加组件或设置关闭操作时。这里是调用包含makeWindow()调用的方法的类。setDefaultCloseOperation not working
public static class Play implements ActionListener{
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
game.playGame();
}
这里是实际的方法:
private void makeWindow() {
JFrame window = new JFrame("Battleships 2.0");
JPanel canvas = new JPanel();
JLabel title = new JLabel("Battleships 2.0");
title.setFont(font);
JTextField userCoordinates = new JTextField();
window.setSize(500,500);
window.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
window.setResizable(false);
window.setVisible(true);
canvas.setLayout(new BoxLayout(canvas, BoxLayout.Y_AXIS));
window.add(canvas);
canvas.add(title);
canvas.add(userCoordinates);
}
尝试使用'window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);' – Hackerdarshi
“我已经扩展我的类JFrame的” ......在'JFrame的窗口=新的JFrame( “战舰2.0”); '你创建了一个普通的'JFrame',“你的班级”在哪里? –
如果你已经用JFrame扩展了你的类,那你为什么要创建另一个JFrame('window')?只要对其本身采取行动。顺便说一句,那是一个坏习惯。 – Hackerdarshi