有没有办法在不退出整个应用程序的情况下使用取消按钮关闭框架。我试过关注
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);
}
}