use*_*387 0 java swing dispose jframe jbutton
在点击任何一个难度按钮后,我试图处理难度窗口,但不会发生.我试过了.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);
}
}
Run Code Online (Sandbox Code Playgroud)
dispose()方法不是静态的,所以直接从JFrame类调用它是行不通的
JFrame.dispose();
Run Code Online (Sandbox Code Playgroud)
试着做 :
dispose();
Run Code Online (Sandbox Code Playgroud)
或者处置frame您创建的对象
frame.dispose();
Run Code Online (Sandbox Code Playgroud)
阅读更多关于 JFrame
首先,你要扩展JFrame并创建一个JFrame对象,如果我没有错,就不应该这样做.
public class Game extends JFrame{
public static JFrame frame = new JFrame();
Run Code Online (Sandbox Code Playgroud)
正如@Salah所说,JFrame不是静态的,所以它应该是:
public JFrame frame = new JFrame();
Run Code Online (Sandbox Code Playgroud)
为了解决你的问题,你要处理一个新的JFrame(是的,你在一个类中有3个JFrame,而不是1,这就是你想要的),JFrame.dispose();如果你已经创建了一个对象,或者你正在扩展JFrame,那么你能够:
this.dispose(); //For the extended JFrame
Run Code Online (Sandbox Code Playgroud)
要么
frame.dispose(); //For the object you created
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8767 次 |
| 最近记录: |