import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JF1 implements ActionListener {
JFrame f;
Container cp;
public static void main(String argv[]) {
new JF1();
}
public JF1() {
//Setup JFrame
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
f=new JFrame("JFrame Test");
f.setSize(400,300);
f.setLocationRelativeTo(null);
cp=f.getContentPane();
cp.setLayout(null);
//Build Elements
f.setVisible(true); //必須放在建立全部元件之後
//f.pack(); //for using Layout Mgr.
//f.pack(); //for using Layout Mgr.
//Close JFrame
f.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
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);}
}
});
}
public void actionPerformed(ActionEvent e) {
//if (e.getSource()==cut) {}
}
}
JF1.java 編譯後會產生兩個 class 檔 : JF1.class 與 JF1$1.class (內部類別).
第二種 JF2.java 則是使用另一個類別 WindowHandler 來處理 Window 事件, 由於關閉視窗時用 JOptionPane 來確認, 所以要傳入 ContentPane 或 JFrame 物件進去 :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JF2 implements ActionListener {
JFrame f;
Container cp;
public static void main(String argv[]) {
new JF2();
}
public JF2() {
//Setup JFrame
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
f=new JFrame("JFrame Test");
f.setSize(400,300);
f.setLocationRelativeTo(null);
cp=f.getContentPane();
cp.setLayout(null);
//Build Elements
f.setVisible(true); //必須放在建立全部元件之後
//f.pack(); //for using Layout Mgr.
//f.pack(); //for using Layout Mgr.
//Close JFrame
f.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
f.addWindowListener(new WindowHandler(cp)); //傳入 f 亦可
}
public void actionPerformed(ActionEvent e) {
//if (e.getSource()==cut) {}
}
}
class WindowHandler extends WindowAdapter {
Container cp;
public WindowHandler(Container cp) {this.cp=cp;}
public void windowClosing(WindowEvent e) {
int result=JOptionPane.showConfirmDialog(cp,
"確定要結束程式嗎?",
"確認訊息",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (result==JOptionPane.YES_OPTION) {System.exit(0);}
}
}
JF2.java 編譯後則是產生 JF2.class 與 WindowHandler.class 兩個類別. 注意, 呼叫 WindowHandler() 建構子時, 可以傳入 ContentPane, 也可以傳入 JFrame 實體, 如下列第三種模板 JF3.java 所示 :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JF3 implements ActionListener {
JFrame f;
public static void main(String argv[]) {
new JF3();
}
public JF3() {
//Setup JFrame
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
f=new JFrame("JFrame Test");
f.setSize(400,300);
f.setLocationRelativeTo(null);
Container cp=f.getContentPane();
cp.setLayout(null);
//Build Elements
f.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
f.addWindowListener(new WindowHandler(f)); //傳入 JFrame
}
public void actionPerformed(ActionEvent e) {
//if (e.getSource()==id) {}
}
}
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);}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JF3 implements ActionListener {
JFrame f;
public static void main(String argv[]) {
new JF3();
}
public JF3() {
//Setup JFrame
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
f=new JFrame("JFrame Test");
f.setSize(400,300);
f.setLocationRelativeTo(null);
Container cp=f.getContentPane();
cp.setLayout(null);
//Build Elements
f.setVisible(true); //必須放在建立全部元件之後
//f.pack(); //for using Layout Mgr.
//Close JFrame //f.pack(); //for using Layout Mgr.
f.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
f.addWindowListener(new WindowHandler(f)); //傳入 JFrame
}
public void actionPerformed(ActionEvent e) {
//if (e.getSource()==id) {}
}
}
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);}
}
}
沒有留言:
張貼留言