1. 程式人生 > 實用技巧 >Java基礎入門-第八章-02

Java基礎入門-第八章-02

Java-GUI-Example02

JDialog是Swing元件中通常用來表示對話視窗的頂級容器。

JDialog常用構造分類
JDialog(Frame owner) 構造方法,用來建立一個非模態的對話坤哥,owner為對話方塊所有者(頂級視窗JFrame)
JDialog(Frame owner,String title) 構造方法,建立一個具有指定標題的非模態對話方塊
JDialog(Frame owner,boolean modal) 構建一個有指定模式的無標題對話方塊

package GUI;
import javax.swing.*;
public class Example02 {
	private static void createAndShowGUI() {
		//建立並設定JFrame容器視窗
		JFrame frame =new JFrame ("JFrameTest");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(350, 150);
		frame.setVisible(true);
		//在JFrame容器視窗基礎上建立並設定JDialog容器視窗
		JDialog dialog=new JDialog(frame,"JDialog對話方塊",true);
		dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		dialog.setSize(200, 100);
		dialog.setVisible(true);
	}
	public static void main (String[] args) {
		//使用SwingUtilities工具類呼叫createAndShowGUI()方法執行並顯示GUI程式
		SwingUtilities.invokeLater(Example02::createAndShowGUI);
	}
}  

效果: