自定义弹出框

1 半自定义弹出框

用到MessageDialog类的构造方法。

MessageDialog customDialog = new MessageDialog(shell, title, titleImage
                , message, dialogImageType, buttonLabels, defaultButtonIndex);
        int index = customDialog.open();

其中title为标题栏的文字;titleImage是标题栏图片;message是弹出框的提示信息;dialogImageType是弹出框图片类型,是MessageDialog自带的图片类型;buttonLabels是定义按钮的个数以及按钮显示的信息;defaultButtonIndex是默认的选择按钮的索引。

2 完全自定义弹出框

继承SWT中的Dialog实现。

  • 自定义弹出框具体实现

public class ApgMessageDialog extends Dialog {
    protected Object result;
    protected Shell shell;
    private Image titleImage;
    private Image messageImage;
    private String[] buttonValue;
    private String message;
    private String titleMessage;
    private Rectangle rectangle;
    private int index;
    private int bWidth;
    private int bHeight;

    /**
     * Create the dialog.
     * @param parent
     * @param style
     */
    public ApgMessageDialog(Shell parent, int style) {
        super(parent, style);
    }

    public ApgMessageDialog(Shell parent, Rectangle rectangle, Image titleImage, Image messageImage, String[] buttonValue, String message,
            String titleMessage) {
        super(parent);
        this.rectangle = rectangle;
        this.titleImage = titleImage;
        this.messageImage = messageImage;
        this.buttonValue = buttonValue;
        this.message = message;
        this.titleMessage = titleMessage;
    }

    /**
     * Open the dialog.
     * @return the result
     */
    public Object open() {
        createContents();
        shell.open();
        shell.layout();
        Display display = getParent().getDisplay();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        return result;
    }

    /**
     * Create contents of the dialog.
     */
    private void createContents() {
        shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.BORDER);
       
        shell.setText(titleMessage);
        shell.setImage(titleImage);
       
        //提示图片
        Label imageLabel = new Label(shell, SWT.NONE);
        imageLabel.setImage(messageImage);
        //图片的像素
        Rectangle rec=messageImage.getBounds();
        //控制图片大小范围
        if(rec.width>250){
            rec.width=250;
        }
        if(rec.height>70){
            rec.height=70;
        }
        imageLabel.setBounds(20,20, rec.width, rec.height);
       
        //提示信息
        Label messageLabel = new Label(shell, SWT.NONE);
        messageLabel.setText(message);
        int mlWidth=getStringWidth(message, messageLabel);
        int mlHeight=getStringHeight(message, messageLabel);
        int mlX=20+rec.width+10;
        int mlY=20+rec.height/2;
        //判断消息是否超出边框的宽度
        if(mlX+mlWidth>rectangle.width){
            rectangle.width=rectangle.width+((mlX+mlWidth)-rectangle.width)+20;
        }
        messageLabel.setBounds(mlX , mlY , mlWidth, mlHeight);
       
        //按钮
        int j=-buttonValue.length;
        for (int i = 0; i < buttonValue.length; i++) {
            final String iStr = String.valueOf(i);
            Button button = new Button(shell, SWT.NONE);
            button.setText(buttonValue[i]);
            bWidth=getStringWidth(buttonValue[i], button);
            bHeight=getStringHeight(buttonValue[i], button);
            //判断按钮是否超出边框的高度
            if(20+rec.height+30+bHeight+20>rectangle.height){
                rectangle.height=rectangle.height+(20+rec.height+30+bHeight+20)-rectangle.height+20;
            }   
            button.setBounds((rectangle.width-20)+j*(2*bWidth+10), (rectangle.height-20)-2*bHeight , 2*bWidth, bHeight);
            button.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    //设置按钮索引
                    setIndex(Integer.parseInt(iStr));
                    shell.dispose();
                }

            });
            j++;
        }
        shell.setBounds(rectangle);
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
   
    /**
     *
      * getStringWidth(取得字符串像素宽度)
      * @param str
      * @param label
      * @return
     */
    public int getStringWidth(String str, Control con) {
        int width = 0;
        GC gc = new GC(con);
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            width += gc.getAdvanceWidth(c);
        }
        gc.dispose();
        return width;
    }
   
    /**
     *
      * getStringHeight(得到字符串的高度)
      * @param str
      * @param label
      * @return
     */
    public int getStringHeight(String str,Control con){
        int height = 0;
        int width=0;
        GC gc = new GC(con);
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            width = gc.getAdvanceWidth(c);
            if(height<width){
                height=2*width;
            }
        }
        gc.dispose();
        return height;
    }
}

  • 调用
  •  Rectangle frameSize = shell.getBounds();
     final Rectangle rec=new Rectangle((frameSize.width-400)/ 2,
                    (frameSize.height-200) / 2,400 ,200);
            ApgMessageDialog amd=new ApgMessageDialog(shell, rec,
                        new Image(Display.getCurrent(),this.getClass().getResourceAsStream("/icons/uiComponent.png")),
                        new Image(Display.getCurrent(),this.getClass().getResourceAsStream("/icons/1.jpg")),
                        new String[]{"保存","取消"}, "完全自定义提示框", button.getText());
                
           amd.open();

 

 

你可能感兴趣的:(自定义,RCP,弹出框)