有什么方法可以在不退出整个应用程序的情况下使用“取消”按钮关闭框架。 我试过关注
setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
只有当我按下 X 关闭按钮关闭框架时,上面的行才有效。有没有其他好的方法来实现一个 JButton 来执行相同的操作
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class CloseTestForm extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CloseTestForm frame = new CloseTestForm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CloseTestForm() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnSave = new JButton("Save");
btnSave.setBounds(64, 141, 89, 23);
contentPane.add(btnSave);
JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//??
}
});
btnCancel.setBounds(64, 192, 89, 23);
contentPane.add(btnCancel);
}
}
最佳答案
setDefaultCloseOperation
使用任何东西,EXIT_ON_CLOSE
除外。
然后,对于取消按钮,只需 dispose()
窗口即可。如果应用程序有另一个正在运行的线程,它不会退出。您还可以通过调用 setVisible(false)
隐藏窗口。
关于java - 有没有什么方法可以在不使用 Cancel jButton 退出整个应用程序的情况下关闭 JFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13291385/