Java-SWT-TitleAreaDialog

我正在尝试使用SWT和JFace创建一个基本对话框类:

 

 

 public class AplotBaseDialog extends TitleAreaDialog

我对如何布局对话框感到困惑?

在Swing中进行操作,我将拥有一个createDialog方法.然后在该方法中,我将添加作为JPanel方法的Components.然后将组件添加到我的centerPanel中.这是基本对话框.每个Panel方法都有自己的布局.

这是一个非常简单的示例(伪代码)

 

public void createDialog() {
   Component selectionsPanel = createTableArea();
   Component buttonPanel = OKCancelButtons();
   JPanel centerPanel = new JPanel();
   centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
   centerPanel.add(selectionsPanel);
   centerPanel.add(buttonPanel);
   getContentPane().add(centerPanel);
   this.pack();
   centerPanel.setVisible(true); 
}

private JPanel OKCancelButtons() {

  submitButton = new JButton("Send");
  etc... etc..
  JPanel p = new JPanel();
  p.setLayout(new BoxLayout(p, BoxLayout.LINE_AXIS));
  p.add(submitButton);
  return p;
}

private JPanel createTableArea() {
  JPanel p = new JPanel();
  similar to above but a Table;
  return p;
}

您可以看到我如何在方法中创建面板,而不是将它们作为组件添加到基本面板中.

您将如何使用TitleAreaDialog做到这一点?

最佳答案

我还没有使用TitleAreaDialog,但这是我自己使用的一个简单Dialog.它应该使您了解对话框的内部工作原理.它基本上只是一个带有一些错误消息和复选框的对话框:

 

 

public class CheckboxDialog extends Dialog {

    private String message = "";
    private String checkboxMessage = "";
    private boolean checkValue;

    private Button checkButton;

    /* Constructor, set shell style and set block on open (rest of gui is blocked until closed) */
    public CheckboxDialog(Shell parentShell) {
        super(parentShell);
        setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.OK | SWT.APPLICATION_MODAL);
        setBlockOnOpen(true);
    }

    /* creates the content of the dialog */
    protected Control createDialogArea(Composite parent) {
        Composite composite = (Composite) super.createDialogArea(parent);

        /* set the layout for the content (gridlayout with 1 column)*/
        GridLayout layout = new GridLayout(1, false);
        layout.marginHeight = 15;
        layout.marginWidth = 30;
        composite.setLayout(layout);

        /* add a label with some text */
        final Label content = new Label(composite, SWT.NONE);
        content.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
        content.setText(message);

        /* add a checkbox button */
        checkButton = new Button(composite, SWT.CHECK);
        checkButton.setText(checkboxMessage);
        checkButton.setSelection(true);
        checkButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

        return composite;
    }

    /* create the dialog buttons (in this case, only an OK button) */
    protected void createButtonsForButtonBar(Composite parent)
    {
        createButton(parent, IDialogConstants.OK_ID, "OK", true);
    }

    /* configure the dialog's shell (set title) */
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setText("Error");
    }

    /* this method is executed if the OK button is pressed */
    public void okPressed()
    {
        checkValue = checkButton.getSelection();
        close();
    }

    /* getter and setter methods */
    public void setMessage(String message) {
        this.message = message;
    }

    public void setCheckboxMessage(String checkboxMessage) {
        this.checkboxMessage = checkboxMessage;
    }

    public boolean getCheckBoxValue()
    {
        return checkValue;
    }
}

如您所见,SWT中没有add方法.您只需在每个小部件的构造函数中指定父级即可.

此外,here是Vogella的一个非常好的教程,它详细解释了如何创建JFace对话框. Here是有关如何使用TitleAreaDialog的另一个示例.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值