關於JFace中的對話框MessageDialog類等其它類型對話框


對話框是軟件系統中最常用到的界面,對話框無處不在,從界面結構來說,對話框主要是由主體的界面組件和底部窗體按鈕組成.

之前的例子中已經頻繁的使用到了MessageDialog.openInformation方法來彈出一個簡單的對話框.MessageDialog只是Dialog中的一種.

在Dialog中還有很多其他的對話框可以使用.

 

信息提示框(MessageDialog類)中的常用方法

1.static void openInformation(Shell parent, java.lang.String title, String message)  

例子:MessageDialog.openInformation(shell,"標題","提示信息"),它的三個參數都可以接受null值.

此方法僅僅用於提示信息.

2.static boolean openConfirm(Shell parent, java.lang.String title, String message)  

例子:

boolean b = MessageDialog.openConfirm(shell,"標題","提示信息");

if(b)

  System.out.println("你單擊了"確定"按鈕);

else

  System.out.println("你單擊了"取消"按鈕);

 

3.static boolean openQuestion(Shell parent, java.lang.String title, String message) 

使用方法和openConfirm一模一樣,只是界面按鈕變成了"是","否"

4. void openError(Shell parent,String title,String message)

例子:MessageDialog.openError(shell,"標題","提示信息");

錯誤提示框,界面上換成了明顯的紅叉圖標.

5.void openWarning(Shell parent,String title,String message)

例子:MessageDialog.openWarning(shell,"標題","提示信息");

警告提示框,界面上換成了明顯的警告圖標.

 

如果僅提示很少的信息,則JFace的MessageDialog彈出的對話框顯示的太大,SWT的MessageBox或許更好一些.兩者相對比較而言,MessageBox可以自動調整對話框大小,但彈出一個對話框MessageBox要3句.而MessageDialog只需要一句.

1.MessageBox最簡單的使用代碼

MessageBox1.java

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

public class MessageBox1 {
    public static void main(String[] args) {
        final Display display = Display.getDefault();
        final Shell shell = new Shell();
        shell.setSize(327, 253);
        // ---------創建窗口中的其他界面組件-------------
        MessageBox messageBox = new MessageBox(shell);
        messageBox.setMessage("Hello World!");
        messageBox.open();
        // -----------------END------------------------
//        shell.layout();
//        shell.open();
        shell.dispose();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

 

2.MessageBox的式樣和open方法返回值實例

MessageBox2.java

 1 import org.eclipse.swt.SWT;
 2 import org.eclipse.swt.widgets.Display;
 3 import org.eclipse.swt.widgets.MessageBox;
 4 import org.eclipse.swt.widgets.Shell;
 5 
 6 public class MessageBox2 {
 7     public static void main(String[] args) {
 8         final Display display = Display.getDefault();
 9         final Shell shell = new Shell();
10         shell.setSize(327, 253);
11         // ---------創建窗口中的其他界面組件-------------
12         MessageBox msgbox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
13         msgbox.setMessage("確認刪除嗎?");
14         int rc = msgbox.open();
15         if (rc == SWT.YES)
16             System.out.println("您單擊了“是”按鈕");
17         if (rc == SWT.NO)
18             System.out.println("您單擊了“否”按鈕");
19         // -----------------END------------------------
20         // shell.layout();
21         // shell.open();
22         shell.dispose();
23         while (!shell.isDisposed()) {
24             if (!display.readAndDispatch())
25                 display.sleep();
26         }
27         display.dispose();
28     }
29 }

 

運行結果:

代碼中SWT.ICON_QUESTION式樣用來在對話框上顯示一個問號圖標,它的其他式樣的效果圖下圖.

另一種式樣SWT.YES|SWT.NO則用來顯示"是,否"按鈕,它的組合方式還有很多.

 

顏色選擇對話框(ColorDialog類)

SWT/JFace中設置顏色都需要用到Color類,如button.setBackground(Color color)是設置按鈕背景色,而ColorDialog則可以彈出一個顏色選擇對話框讓用戶選擇顏色.

ColorDialog1.java

 1 import org.eclipse.swt.graphics.Color;
 2 import org.eclipse.swt.graphics.RGB;
 3 import org.eclipse.swt.widgets.ColorDialog;
 4 import org.eclipse.swt.widgets.Display;
 5 import org.eclipse.swt.widgets.Shell;
 6 
 7 public class ColorDialog1 {
 8     public static void main(String[] args) {
 9         final Display display = Display.getDefault();
10         final Shell shell = new Shell();
11         shell.setSize(327, 253);
12         // ---------創建窗口中的其他界面組件-------------
13         ColorDialog dialog = new ColorDialog(shell);// 創建顏色選擇對話框
14         RGB rgb = dialog.open();// 打開顏色選擇對話框,並得到包含所選顏色的RGB值的對象
15         if (rgb != null) {// 根據rgb生成一個color對象
16             Color color = new Color(shell.getDisplay(), rgb);
17             // 使用顏色對象color ……。記得用完后調用color.dispose()將color釋放掉
18         }
19         // -----------------END------------------------
20         // shell.layout();
21         // shell.open();
22         shell.dispose();
23         while (!shell.isDisposed()) {
24             if (!display.readAndDispatch())
25                 display.sleep();
26         }
27         display.dispose();
28     }
29 }

 

運行結果:

字體選擇對話框(FontDialog類)

FontDialog提供了字體選擇對話框,它返回一個字體數據對象FontData,然后可以用FontData生成一個字體對象Font.使用FontDialog的實例如下:

FontDialog1.java

 1 import org.eclipse.swt.graphics.Font;
 2 import org.eclipse.swt.graphics.FontData;
 3 import org.eclipse.swt.widgets.Display;
 4 import org.eclipse.swt.widgets.FontDialog;
 5 import org.eclipse.swt.widgets.Shell;
 6 
 7 public class FontDialog1 {
 8     public static void main(String[] args) {
 9         final Display display = Display.getDefault();
10         final Shell shell = new Shell();
11         shell.setSize(327, 253);
12         // ---------創建窗口中的其他界面組件-------------
13         FontDialog dialog = new FontDialog(shell);
14         FontData fontData = dialog.open();
15         if (fontData != null) {
16             Font font = new Font(shell.getDisplay(), fontData);
17             // 使用字體對象font ……。記得用完后,調用font.dispose()將font釋放掉
18         }
19         // -----------------END------------------------
20         // shell.layout();
21         // shell.open();
22         shell.dispose();
23         while (!shell.isDisposed()) {
24             if (!display.readAndDispatch())
25                 display.sleep();
26         }
27         display.dispose();
28     }
29 }

 

運行結果:

打印設置對話框(PrintDialog類)

打印在軟件系統中是必不可少的功能,在SWT/JFace中要用到打印類Printer,PrintDialog則提供了打印設置對話框,它可以返回一個打印數據對象PrinterData,然后根據PrinterData可以得到打印對象Printer.

PrintDialog1.java

 1 import org.eclipse.swt.printing.PrintDialog;
 2 import org.eclipse.swt.printing.Printer;
 3 import org.eclipse.swt.printing.PrinterData;
 4 import org.eclipse.swt.widgets.Display;
 5 import org.eclipse.swt.widgets.Shell;
 6 
 7 public class PrintDialog1 {
 8     public static void main(String[] args) {
 9         final Display display = Display.getDefault();
10         final Shell shell = new Shell();
11         shell.setSize(327, 253);
12         // ---------創建窗口中的其他界面組件-------------
13         PrintDialog dialog = new PrintDialog(shell);
14         PrinterData printerData = dialog.open();
15         if (printerData != null) {
16             Printer printer = new Printer(printerData);
17             // 使用printer對象 ……。記得用完后,調用printer.dispose()將printer釋放掉
18         }
19         // -----------------END------------------------
20         // shell.layout();
21         // shell.open();
22         shell.dispose();
23         while (!shell.isDisposed()) {
24             if (!display.readAndDispatch())
25                 display.sleep();
26         }
27         display.dispose();
28     }
29 }

 

運行結果:

目錄選擇對話框(DirectoryDialog類)

目錄選擇對話框,它提供了一個界面讓用戶選擇目錄,然后返回所選目錄的路徑(一個字符串),使用DirectoryDialog類的實例如下:

DirectoryDialog1.java

import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class DirectoryDialog1 {
    public static void main(String[] args) {
        final Display display = Display.getDefault();
        final Shell shell = new Shell();
        shell.setSize(327, 253);
        // ---------創建窗口中的其他界面組件-------------
        DirectoryDialog dialog = new DirectoryDialog(shell);
        dialog.setText("目錄"); // 設置窗口標題
        dialog.setMessage("請選擇一個目錄:"); // 設置提示文字
        dialog.setFilterPath("c:/windows"); // 設置初始目錄
        String dir = dialog.open(); // 打開對話框並返回一個包含所選目錄路徑的字符串
        if (dir != null)
            System.out.println(dir); // 打印出所選擇目錄的絕對路徑
        // -----------------END------------------------
        // shell.layout();
        // shell.open();
        shell.dispose();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

 

運行結果:

文件選擇對話框(FileDialog類)

文件選擇對話框,提供一個界面讓用戶選擇文件,然后返回所選文件愛你的全路徑(路徑+文件名).FileDialog還能設置文件選擇的類型限制,也能選擇多個文件.

一個最簡單的實例:

FileDialog1.java

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

public class FileDialog1 {
    public static void main(String[] args) {
        final Display display = Display.getDefault();
        final Shell shell = new Shell();
        shell.setSize(327, 253);
        // ---------創建窗口中的其他界面組件-------------
        FileDialog dialog = new FileDialog(shell, SWT.OPEN);
        dialog.setFilterPath("c:/windows"); // 設置初始路徑
        String fileName = dialog.open(); // 返回所選文件的全路徑(路徑+文件名)
        // -----------------END------------------------
        // shell.layout();
        // shell.open();
        shell.dispose();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

 

運行結果:

2.選擇多個文件(SWT.MULTI式樣)

FileDialog2.java

 1 import org.eclipse.swt.SWT;
 2 import org.eclipse.swt.widgets.Display;
 3 import org.eclipse.swt.widgets.FileDialog;
 4 import org.eclipse.swt.widgets.Shell;
 5 
 6 public class FileDialog2 {
 7     public static void main(String[] args) {
 8         final Display display = Display.getDefault();
 9         final Shell shell = new Shell();
10         shell.setSize(327, 253);
11         // ---------創建窗口中的其他界面組件-------------
12         FileDialog dialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI);// 加MULTI式樣
13         String fileName = dialog.open(); // 返回最后一個選擇文件的全路徑
14         String[] fileNames = dialog.getFileNames(); // 返回所有選擇文件的文件名,不包含路徑
15         System.out.println(fileName == null ? "" : fileName);// 打印open方法的返回值
16         // 用一個循環將數組中的文件名加上路徑打印出來
17         for (int i = 0; i < fileNames.length; i++)
18             System.out.println(dialog.getFilterPath() + "\\" + fileNames[i]);
19         // -----------------END------------------------
20         // shell.layout();
21         // shell.open();
22         shell.dispose();
23         while (!shell.isDisposed()) {
24             if (!display.readAndDispatch())
25                 display.sleep();
26         }
27         display.dispose();
28     }
29 }

 

3.文件選擇的限制類型(setFilterExtensions方法)

FileDialog3.java

 1 import org.eclipse.swt.SWT;
 2 import org.eclipse.swt.widgets.Display;
 3 import org.eclipse.swt.widgets.FileDialog;
 4 import org.eclipse.swt.widgets.Shell;
 5 
 6 public class FileDialog3 {
 7     public static void main(String[] args) {
 8         final Display display = Display.getDefault();
 9         final Shell shell = new Shell();
10         shell.setSize(327, 253);
11         // ---------創建窗口中的其他界面組件-------------
12         FileDialog dialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI);
13         dialog.setFilterNames(new String[] { "可執行文件 (*.exe)", "Microsoft Excel (*.xls)" });
14         dialog.setFilterExtensions(new String[] { "*.exe", "*.xls", "*.jpg", "*.*" });
15         String fileName = dialog.open();
16         System.out.println(fileName);
17         // -----------------END------------------------
18         // shell.layout();
19         // shell.open();
20         shell.dispose();
21         while (!shell.isDisposed()) {
22             if (!display.readAndDispatch())
23                 display.sleep();
24         }
25         display.dispose();
26     }
27 }

 

程序說明:

setFilterExtensions設置文件選擇的限制類型.

setFilterNames設置文件類型的說明文字,不要也可以.

setFilterExtensions參數的數組元素會和setFilterNames參數的數組元素依順序相對應,像*.jpg類型本應該對 應setFilterNames中數組的第三個元素,但此數組只有兩個元素,所以setFilterNames沒有為*.jpg類型設置名稱.則界面默認以類型*.jpg作為其名稱.

4.保存對話框(SWT.SAVE式樣)

保存對話框並不能提供實際保存文件的功能,它僅僅是彈出一個文件對話框,然后返回一個包含路徑信息的字符串,具體文件的保存操作(指將文件信息寫入磁盤)需要另外寫程序去實現.保存對話框的實例如下:

FileDialog4.java

 1 import org.eclipse.swt.SWT;
 2 import org.eclipse.swt.widgets.Display;
 3 import org.eclipse.swt.widgets.FileDialog;
 4 import org.eclipse.swt.widgets.Shell;
 5 
 6 public class FileDialog4 {
 7     public static void main(String[] args) {
 8         final Display display = Display.getDefault();
 9         final Shell shell = new Shell();
10         shell.setSize(327, 253);
11         // ---------創建窗口中的其他界面組件-------------
12         FileDialog dlg = new FileDialog(shell, SWT.SAVE);
13         String fileName = dlg.open();
14         if (fileName != null)
15             System.out.println(fileName);
16         // -----------------END------------------------
17         // shell.layout();
18         // shell.open();
19         shell.dispose();
20         while (!shell.isDisposed()) {
21             if (!display.readAndDispatch())
22                 display.sleep();
23         }
24         display.dispose();
25     }
26 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM