zoukankan      html  css  js  c++  java
  • 关于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 }

     

  • 相关阅读:
    【Difference Between Primes HDU
    【Pet HDU
    《Java程序设计实验》 软件工程18-1,3 OO实验2
    【数据结构作业】-【带头结点的单链表就地逆置】
    【Miscalculation UVALive
    【Bit String Reordering UVALive
    【Bazinga HDU
    (转载)博弈汇总【巴什博奕,威佐夫博弈,尼姆博弈,斐波那契博弈】
    【Audiophobia UVA
    【Calling Circles UVA
  • 原文地址:https://www.cnblogs.com/DreamDrive/p/4167598.html
Copyright © 2011-2022 走看看