JAVA程式關閉視窗時,顯示確認訊息
class WindowHandler extends WindowAdapter {JFrame f;
public WindowHandler(JFrame f) {this.f=f;}
public void windowClosing(WindowEvent e) {
int result=JOptionPane.showConfirmDialog(f,
"確定要結束程式嗎?",
"確認訊息",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (result==JOptionPane.YES_OPTION) {System.exit(0);}
}
}
呼叫方法
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowHandler(frame));
JAVA程式的確認視窗
int opt=JOptionPane.showConfirmDialog(frame,"確定要儲存?","確認",JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if (opt==JOptionPane.YES_OPTION) {
st.execute(Y);
JOptionPane.showMessageDialog(frame,"儲存成功 ");
}
else if (opt==JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(frame,"您按了 NO (否) " );
}
else if (opt==JOptionPane.CANCEL_OPTION) {
JOptionPane.showMessageDialog(frame,"您按了 CANCEL (取消) " );
}
else if (opt==JOptionPane.CLOSED_OPTION) {
JOptionPane.showMessageDialog(frame,"您按了 CLOSE (關閉) " );
}
沒有留言:
張貼留言