Java 无法处理 jframe 窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22945712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't dispose of jframe window?
提问by user3512387
I'm trying to dispose of the difficulty window after any one of the difficulty button's are clicked but it won't happen. I've tried .dispose
and frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
but i can't get it. Is it just placement or more?
我试图在点击任何一个难度按钮后处理难度窗口,但它不会发生。我试过了.dispose
,frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
但我无法得到它。它只是放置还是更多?
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
public class Game extends JFrame{
public static JFrame frame = new JFrame();
private JLabel lab;
public static void main(String[] args) {
Game difficulty = new Game();
difficulty.setSize(350,105);
difficulty.setTitle("Difficulty.");
difficulty.setVisible(true);
difficulty.setLocationRelativeTo(null);
/**Game sudoku = new Game();
sudoku.setSize(900, 900);
sudoku.setVisible(false);*/
}
public Game(){
setLayout(new FlowLayout());
lab = new JLabel("Please select your difficulty.");
add(lab);
JButton easy;
easy = new JButton("Easy");
add(easy);
easy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("You clicked the button");
JFrame.dispose();
}
});
JButton medium;
medium = new JButton("Medium");
add(medium);
JButton hard;
hard = new JButton("Hard");
add(hard);
JButton evil;
evil = new JButton("Evil!");
add(evil);
}
}
采纳答案by Salah
回答by waco001
If you're wanting to close the whole program, you can use System.exit(0);
如果你想关闭整个程序,你可以使用 System.exit(0);
回答by Roach Tenant
Try setting the jFrame to invisible before disposing it:
在处理之前尝试将 jFrame 设置为不可见:
public void disposeJFrame(JFrame frame){
frame.setVisible(false);
frame.dispose();
}
回答by elias
Instead JFrame.dispose();
, use frame.dispose()
or JFrame.this.dispose();
相反JFrame.dispose();
,使用frame.dispose()
或JFrame.this.dispose();
回答by Frakcool
First of all you're extending JFrame and creating an object of JFrame, if I'm not wrong, this shouldn't be done.
首先,您正在扩展 JFrame 并创建 JFrame 的对象,如果我没猜错,则不应这样做。
public class Game extends JFrame{
public static JFrame frame = new JFrame();
And as @Salahsaid, JFrame is not static, so it should be:
正如@Salah所说,JFrame 不是静态的,所以它应该是:
public JFrame frame = new JFrame();
To solve your problem, you're disposing a new JFrame (yes, you have 3 JFrames in one class, instead of 1, which is what you want), with: JFrame.dispose();
if you already created an object or you're extending JFrame, you can:
为了解决您的问题,您正在处理一个新的 JFrame(是的,您在一个类中有 3 个 JFrame,而不是 1 个,这是您想要的),并且:JFrame.dispose();
如果您已经创建了一个对象或者您正在扩展 JFrame,您能够:
this.dispose(); //For the extended JFrame
or
或者
frame.dispose(); //For the object you created
回答by besartm
I had the same problem:
我有同样的问题:
this.dispose();
solved my problem.
解决了我的问题。