• Eclipse JFace 对话框显示中文(弹出消息提示框)


        JFace中的对话框是通过SWT中的Shell 窗口封装来的,调用的是SWT中的button控件对象,查看MessageDialog类的源代码可以发现设置按钮文字是通过字符常量来设定的,默认为英文。若要显示中文提示,需加入中文语言包。
        在《Eclipse从入门到精通》(第二版)中,是以Eclipse 3.2.1版本进行说明的。但从Eclipse官网上看,似乎语言包也只支持到3.2,后续就没有更新了。幸亏我不需汉化整个Eclipse,而今需让JFace 的对话框显示中文提示即可。

    一、源码
    以InputDialog为例:

    InputDialog dialog = new InputDialog(shell, "标题", "请输入值", "1", null);
    if (dialog.open() == InputDialog.OK) {
      String valueStr = dialog.getValue();
      MessageDialog.openInformation(shell, "标题", "输入的值为:"+valueStr);
    }
    ---------------------------------------------------------------------------------------
    //“取消”按钮单击事件
        boolean call = MessageDialog.openConfirm(Display.getCurrent().getActiveShell(),"确认","确实要保退出吗?");
        if (call){ //退出
         System.exit(0);
        }

    二、默认状态
    默认显示为英文:

    三、加入中文包
    1、下载

    虽然Eclipse 3.2版本之后就没有继续提供多语言版本,幸好对于RCP程序来说,该语言包还是有用的。
    下载地址为:点击
    中文包在NLpack1 里面。对于RCP程序来说,只需下载NLpack1-eclipse-RCP-3.2-win32.zip即可。
    下载后,从压缩包中提取出org.eclipse.jface.nl1_3.2.0.v200606220026.jar文件,并放入项目的目录中。

    2、设置项目的库引用
    打开Eclipse,右键点击项目名称,选择“Properties”,点击“Java Build Path” 下的“Libraries”选项卡:
    然后用“Add JARs...”从项目目录中把上述库加入,结果如下



     其它:

    16.1 JFace对话框概述
    ◆ ErrorDialog:可根据错误级别来显示错误信息,一般用于Eclipse工作台。
    ◆ MessageDialog:可显示提示信息的对话框,类似于SWT的对话框,但比SWT功能强大。
    ◆ MessageDialogWithToggle:一般在保存首选项所设置的值时显示是否保存的提示信息。
    ◆ ProgressMonitorDialog:可以显示后台线程进度的对话框。
    ◆ InputDialog:用于输入信息的对话框,同时可以验证用户的输入,并可以将验证的信息显示在对话框中。
    ◆ PreferenceDialog:专门为设置首选项所使用的对话框。
    ◆ TitleAreaDialog:带有标题、图标和描述信息的对话框。
    ◆ WizardDialog:向导式对话框。

    16.2 信息提示对话框(MessageDialog)
    各种类型的信息提示对话框示例:

    /** 
     * <p>类功能简述</p> 
     * 类的目的介绍等注释
     * 
     * 
    @author  Jif  liaojifeng@163.com
     * 
    @version v1.0
     * @Timer   2011-10-18 上午10:23:30
     * ----------------------------------------------------------------------------
     * Copyright (c) 2011
     * Company   
     * ----------------------------------------------------------------------------
    */ 
    package cn.net.comsys.window;

    import org.eclipse.jface.action.StatusLineManager;
    import org.eclipse.jface.dialogs.MessageDialog;
    import org.eclipse.jface.resource.ImageDescriptor;
    import org.eclipse.jface.window.ApplicationWindow;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.PaintEvent;
    import org.eclipse.swt.events.PaintListener;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.graphics.Image;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Control;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Text;
    import org.eclipse.swt.browser.Browser;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Canvas;
    import org.eclipse.swt.events.MouseAdapter;
    import org.eclipse.swt.events.MouseEvent;

    public class MessageDialogTest extends ApplicationWindow  {
        public MessageDialogTest() {
               super(null);
            }

            protected void configureShell(Shell shell) {
               super.configureShell(shell);
               shell.setSize(550,300);
               shell.setText("消息对话框示例");
            }

            protected Control createContents(Composite parent) {
               //窗口中的区域由一个文本框做为消息输出台和几个按钮分别打开不同的对话框
               Composite composite = new Composite( parent ,SWT.NONE);
               GridLayout gridLayout = new GridLayout (6,false);
               composite.setLayout( gridLayout );
               final Text console = new Text ( composite ,SWT.NONE|SWT.READ_ONLY|SWT.V_SCROLL);
               GridData data = new GridData(GridData.FILL_BOTH);
               data.horizontalSpan=6;
               console.setLayoutData( data);
               Button openError = new Button( composite ,SWT.NONE);
               openError.setText("错误消息对话框");
               openError.addSelectionListener( new SelectionAdapter(){
                public void widgetSelected(SelectionEvent e) {
                MessageDialog.openError(Display.getCurrent().getActiveShell(),"错误消息对话框","读取文件发生错误!");
                 console.append("\n openError对话框,返回void");
                } 
               });
               Button openConfirm = new Button( composite ,SWT.NONE);
               openConfirm.setText("确认消息对话框");
               openConfirm.addSelectionListener( new SelectionAdapter(){
                public void widgetSelected(SelectionEvent e) {
                 boolean b = MessageDialog.openConfirm(Display.getCurrent().getActiveShell(),
                   "确认消息对话框",
                   "确实要保存文件吗?");
                 console.append("\n openConfirm对话框,返回"+b);
                }
               });
               Button openInformation = new Button( composite ,SWT.NONE);
               openInformation.setText("消息对话框");
               openInformation.addSelectionListener( new SelectionAdapter(){
                public void widgetSelected(SelectionEvent e) {
                 MessageDialog.openInformation(Display.getCurrent().getActiveShell(),"消息对话框","确实要保存文件吗?");
                 console.append("\n openInformation对话框,返回void");
                }
               });
               Button openQuestion = new Button( composite ,SWT.NONE);
               openQuestion.setText("询问对话框");
               openQuestion.addSelectionListener( new SelectionAdapter(){
                public void widgetSelected(SelectionEvent e) {
                 boolean b = MessageDialog.openQuestion(Display.getCurrent().getActiveShell(),"询问对话框","确实要保存文件吗!");
                 console.append("\n openQuestion对话框,返回"+b);
                }
               });
               Button openWarning = new Button( composite ,SWT.NONE);
               openWarning.setText("警告对话框");
               openWarning.addSelectionListener( new SelectionAdapter(){
                public void widgetSelected(SelectionEvent e) {
                 MessageDialog.openWarning(Display.getCurrent().getActiveShell(),"警告对话框","确实要保存文件吗!");
                 console.append("\n openWarning对话框,返回void");
                }
               });
               Button customMessageDig = new Button( composite ,SWT.NONE);
               customMessageDig.setText("自定义对话框");
               customMessageDig.addSelectionListener( new SelectionAdapter(){
                public void widgetSelected(SelectionEvent e) {
                 MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(),//shell窗口
                   "这是对话框的标题",//标题
                   null,//对话框的图标,为null则不显示图标
                   "这是一个自定义的对话框,可以改变按钮的设置",//对话框中的提示信息
                   MessageDialog.INFORMATION,//提示信息的图标
                   new String[]{"查看","保存","确认"},//显示三个按钮
                   1);//默认选择的按钮的索引值,这里为1,表示默认选中第二个按钮,也就是保存按钮
                 int i = dialog.open();//返回值为按钮
                 console.append("\n 自定义对话框,返回按钮的索引值"+i);
                }
               });
               return parent;
            }
            public static void main(String[] args) {
               MessageDialogTest test = new MessageDialogTest();
               test.setBlockOnOpen( true );
               test.open();
               Display.getCurrent().dispose();
            }


    }


     

  • 相关阅读:
    2017-2018-1 20155320加分项目——pwd的实现
    2017-2018-1 20155306 20155320 20155326《信息安全技术》实验三——数字证书应用
    2017-2018-1 20155320 《信息安全系统设计基础》第八周学习总结
    2017-2018-1 20155320第八周课堂实践总结+课下作业
    2017-2018-1 20155320 《信息安全系统设计基础》第七周学习总结
    2017-2018-1 20155320 20155326 实验二 固件程序设计
    2017-2018-1 20155320第六周课堂实践总结
    2017-2018-1 20155320缓冲区溢出漏洞实验
    2017-2018-1 20155320 《信息安全系统设计基础》第六周学习总结
    2017-2018-1 20155318 实验三 实时系统报告
  • 原文地址:https://www.cnblogs.com/jifeng/p/2213615.html
Copyright © 2020-2023  润新知