Trên một trong các chương trình của tôi, tôi muốn Hộp thoại xuất hiện khi người dùng cố gắng thoát khỏi ứng dụng. Sau đó người dùng phải chọn lưu một số trạng thái của chương trình, không lưu hoặc hủy thao tác thoát.Java: Cách hủy thoát ứng dụng
tôi đã viết này trong một nỗ lực để tìm một giải pháp đầu tiên và sau đó thực hiện nó ony:
import javax.swing.*;
import java.awt.Dimension;
import java.awt.event.*;
class WL implements WindowListener
{
private boolean statussaved;
private JFrame tframe;
WL (JFrame frame)
{
statussaved = false;
tframe = frame;
}
@Override public void windowActivated (WindowEvent w) { }
@Override public void windowClosed (WindowEvent w) { }
@Override public void windowDeactivated (WindowEvent w) { }
@Override public void windowDeiconified (WindowEvent w) { }
@Override public void windowIconified (WindowEvent w) { }
@Override public void windowOpened (WindowEvent w) { }
@Override public void windowClosing (WindowEvent w)
{
if (statussaved)
{
return;
}
final JDialog diag = new JDialog (tframe, "Save Progress", true);
diag.setPreferredSize (new Dimension (500, 100));
diag.setResizable (false);
diag.setDefaultCloseOperation (JDialog.DISPOSE_ON_CLOSE);
JPanel notifypanel = new JPanel();
notifypanel.add (new JLabel ("Do you want to save the current status ?"));
JButton yesbutton = new JButton ("Yes");
JButton nobutton = new JButton ("No");
JButton cancelbutton = new JButton ("Cancel");
yesbutton.addActionListener (new ActionListener()
{
@Override public void actionPerformed (ActionEvent a)
{
//SAVE THE STATUS
System.out.println ("Saving status...");
statussaved = true;
diag.dispose();
tframe.dispose();
}
});
nobutton.addActionListener (new ActionListener()
{
@Override public void actionPerformed (ActionEvent a)
{
//just exit/close the application
diag.dispose();
tframe.dispose();
}
});
cancelbutton.addActionListener (new ActionListener()
{
@Override public void actionPerformed (ActionEvent a)
{
//DON'T EXIT !!!
diag.dispose();
}
});
notifypanel.add (yesbutton);
notifypanel.add (nobutton);
notifypanel.add (cancelbutton);
diag.setContentPane (notifypanel);
diag.pack();
diag.setVisible (true);
}
}
public class SaveTest
{
public static void main (String[] args)
{
SwingUtilities.invokeLater (new Runnable()
{
@Override public void run()
{
JFrame frame = new JFrame ("Save Test");
frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener (new WL (frame));
JLabel lab = new JLabel ("just some information");
frame.setPreferredSize (new Dimension (400, 300));
frame.setResizable (false);
frame.add (lab);
frame.pack();
frame.setVisible (true);
}
});
}
}
Nó biên dịch và chạy mà không cần bất kỳ sự thay đổi, vì vậy bạn có thể kiểm tra nó.
Lựa chọn "Có" và "Không" hoạt động như mong đợi, nhưng tôi hoàn toàn không biết viết gì trong số ActionListener
của nút "Hủy". Điều tôi muốn là khi người dùng nhấp vào nút "Hủy", hộp thoại sẽ biến mất, nhưng cửa sổ chính vẫn hiển thị (tức là chương trình vẫn tiếp tục chạy).
Bây giờ, vì tất cả điều này được thực hiện theo phương pháp windowClosing
, rõ ràng là một số loại tín hiệu vứt bỏ đã được gửi để tiêu hủy JFrame
. Điều này có nghĩa rằng có lẽ không có cách nào điều này có thể được thực hiện trong thiết kế hiện tại. Tôi không ngại tổ chức lại/thiết kế lại tất cả những điều này để làm cho nó hoạt động. Chỉ là ... Tôi không biết làm thế nào.
Bất kỳ ý tưởng nào?
Cảm ơn bạn, tôi đã làm điều đó và nó hoạt động :). PS: Trên thực tế, thay đổi là cho 'mainframe', không phải' diag'. –