【问题标题】:New Composites not showing in Dialog对话框中未显示新复合材料
【发布时间】:2012-08-24 15:33:49
【问题描述】:

我正在尝试将新的合成添加到我的对话框中。我正在创建一个简单的按钮,我想把它放在对话框中。我首先创建了一个方法来创建 Button 区域。此方法正在调用创建简单按钮的方法。新按钮未显示在对话框中。

这是完整的代码

public class AplotBaseDialog extends TitleAreaDialog {

   private TCSession session= null;
   public AplotBaseDialog(Shell parentShell, TCSession theSession) {
       super(parentShell);
       session = theSession;
   }

   @Override
   protected Control createDialogArea(Composite parent) {
   Composite composite = (Composite) super.createDialogArea(parent);
   GridLayout layout = new GridLayout();
   layout.numColumns = 1;
   composite.setLayout(layout);

   // The text fields will grow with the size of the dialog
   GridData gridData = new GridData();
   gridData.grabExcessHorizontalSpace = true;
   gridData.horizontalAlignment = GridData.BEGINNING;
   Label label1 = new Label(composite, SWT.NONE);
   label1.setText("First Name");
   label1.setLayoutData(gridData);

   gridData = new GridData();
   gridData.grabExcessHorizontalSpace = true;
   gridData.horizontalAlignment = GridData.CENTER;
   Label label2 = new Label(composite, SWT.NONE);
   label2.setText("Last Name");
   label2.setLayoutData(gridData);

   gridData = new GridData();
   gridData.grabExcessHorizontalSpace = true;
   gridData.horizontalAlignment = GridData.END;
   Button button1 = new Button(composite, SWT.PUSH);
   button1.setText("Button 5");
   button1.setLayoutData(gridData);

   return composite;
}

protected void createButtonArea(Composite parent){
  GridData gridData = new GridData();
  gridData.horizontalAlignment = SWT.CENTER;
  parent.setLayoutData(gridData);
  createOkButton(parent);

 }
 protected Button createOkButton(Composite parent) {
  // increment the number of columns in the button bar
  ((GridLayout) parent.getLayout()).numColumns++;
  Button button = new Button(parent, SWT.PUSH);
  button.setText("Test Button");
  setButtonLayoutData(button);
  return button;
 }
}

为什么对话框中没有显示测试按钮?

这样的事情在 SWT/Jface 中是否可行?

有人可以告诉我执行对话框之类的方法的布局吗?

【问题讨论】:

  • 你永远不会打电话给createButtonArea(Composite parent)...

标签: java swt jface


【解决方案1】:

也许它会更简洁,而不是您创建的 createButtonArea() 方法来实际覆盖 createButtonsForButtonBar 方法。它会更加 SWT 友好。

/**
 * Create contents of the button bar.
 * @param parent
 */
@Override
protected void createButtonsForButtonBar(Composite parent) {
    createButton(parent, IDialogConstants.OK_ID, "Test Button",
            true);
    createButton(parent, IDialogConstants.CANCEL_ID,
            IDialogConstants.CANCEL_LABEL, false);
}

【讨论】:

  • 我同意你的看法,如果他真的想创建“默认”对话框按钮的话。但我无法从他的问题中看出。也可能是一些随机按钮...
  • 我只是想学习如何将不同的组件添加到我的对话框中。我很难理解如何使用 SWT 将按钮、标签、表格添加到对话框中。我认为您不必在 createDialogArea 方法中添加所有组件。如果我想在对话框顶部并排放置 2 个按钮,然后在第二行中间有一个表格,然后在对话框底部(第三行)并排放置按钮。
  • @jkteater 那么你真的应该阅读layouts 上的 Eclipse 教程。例子太多了……
  • 另一种想法是尝试添加工具栏或找到支持工具栏的 TitleAreaDialog 的替代品。 SWT 与 Swing 有很大不同,学习的资源要少得多。我建议你拿一本 Eclipse RCP 书或一本 SWT 书。
  • 为您的按钮栏调用 createButton() 的最大原因是它获得了正确的顺序。如果您在 Windows(和较旧的 GNOME)上,平台外观是您的取消(或否)按钮在最右边,确定(或是)按钮在最左边。在 Mac OS 和最近的 GNOME 上,这是翻转的(确定按钮在最右边。)具有正确按钮 ID 的 createButton() (正如上面@Dan 指出的那样)将始终正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-30
  • 2021-05-09
  • 2015-12-03
相关资源
最近更新 更多