《Eclipse SWT/JFACE 核心應(yīng)用》 清華大學(xué)出版社 9.8 對話框
SWT中的對話框有消息提示框(MessageBox)、文件目錄對話框(DirectoryDialog)、文件對話框(FileDialog)、顏色
對話框(ColorDialog)、字體對話框(FontDialog)、打印對話框(PrintDialog)等。
package www.swt.com.ch9;
import java.io.File;
public class DialogSample {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout( new GridLayout());
shell.setText("Dialog Sample");
Button b1 = new Button ( shell,SWT.NONE);
b1.setText("消息提示框");
b1.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
//創(chuàng)建消息框?qū)ο?,使用警告圖標(biāo)并顯示是和否按鈕
MessageBox box = new MessageBox( shell ,SWT.ICON_ERROR|SWT.YES|SWT.NO);
//設(shè)置對話框的標(biāo)題
box.setText("錯誤消息對話框");
//設(shè)置對話框顯示的消息
box.setMessage("讀取文件發(fā)生錯誤!");
//打開對話框,將返回值賦給choice
int choice = box.open();
//打印出所選擇的值
if (choice==SWT.YES)
System.out.print("Yes");
else if ( choice==SWT.NO)
System.out.print("No");
}
});
Button b2 = new Button ( shell,SWT.NONE);
b2.setText("目錄選取對話框");
b2.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
DirectoryDialog dialog = new DirectoryDialog(shell);
//設(shè)置顯示在窗口上方的提示信息
dialog.setMessage("請選擇所要保存的文件夾");
//設(shè)置對話框的標(biāo)題
dialog.setText("選擇文件目錄");
//設(shè)置打開時默認(rèn)的文件目錄
dialog.setFilterPath("C:\\");
//打開窗口,返回用戶所選的文件目錄
String saveFile = dialog.open();
if ( saveFile != null )
{
//創(chuàng)建一個File對象
File directory = new File(saveFile);
System.out.print(directory.getPath());
}
}
});
Button b3 = new Button ( shell,SWT.NONE);
b3.setText("文件對話框");
b3.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
//創(chuàng)建一個打開對話框,樣式設(shè)置為SWT.OPEN,其他也可以是SWT.SAVE、SWT.MULTI
FileDialog dialog = new FileDialog(shell,SWT.OPEN);
//設(shè)置打開默認(rèn)的路徑
dialog.setFilterPath(System.getProperty("java.home"));
//設(shè)置所打開文件的擴展名
dialog.setFilterExtensions(new String[] {"*.txt", "*.*"});
//設(shè)置顯示到下拉框中的擴展名的名稱
dialog.setFilterNames( new String[]{"Text Files (*.txt)", "All Files (*.*)"});
//打開窗口,返回用戶所選的文件目錄
String file = dialog.open();
if ( file != null )
{
System.out.print(file);
}
}
});
Button b4 = new Button ( shell,SWT.NONE);
b4.setText("顏色對話框");
b4.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
//創(chuàng)建一個顏色對話框
ColorDialog dialog = new ColorDialog(shell);
//設(shè)置默認(rèn)選中的顏色
dialog.setRGB( new RGB( 255 ,255 ,128));
//打開對話框,將選中的顏色返回給rgb對象
RGB rgb = dialog.open();
if ( rgb != null )
{
System.out.print(rgb);
//創(chuàng)建顏色對象
Color color = new Color( display , rgb );
//在使用完顏色對象后,釋放資源
color.dispose();
}
}
});
Button b5 = new Button ( shell,SWT.NONE);
b5.setText("字體對話框");
b5.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
//創(chuàng)建一個字體對話框
FontDialog dialog = new FontDialog (shell);
//設(shè)置默認(rèn)選中的顏色
dialog.setRGB( new RGB( 255 ,255 ,128));
//打開對話框,將選中的字體返回給fontData對象
FontData fontData = dialog.open();
if ( fontData != null )
{
System.out.print(fontData);
//創(chuàng)建顏色對象
Font font = new Font( display , fontData );
//在使用完字體對象后,釋放資源
font.dispose();
}
}
});
Button b6 = new Button ( shell,SWT.NONE);
b6.setText("打印對話框");
b6.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
//創(chuàng)建一個打印對話框
PrintDialog dialog = new PrintDialog (shell);
//打開對話框,將選中的字體返回給fontData對象
PrinterData printData = dialog.open();
if ( printData != null )
{
//創(chuàng)建打印對象
Printer printer = new Printer( printData );
//在使用打印對象后,釋放資源
printer.dispose();
}
}
});
//shell.setSize(200, 150);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
顯示效果:

消息提示框:

目錄選取對話框:

文件對話框:

顏色對話框:

字體對話框:

打印對話框:
