Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用GridLayout将“确定”和“取消”按钮居中_Java_Layout_Swt - Fatal编程技术网

Java 使用GridLayout将“确定”和“取消”按钮居中

Java 使用GridLayout将“确定”和“取消”按钮居中,java,layout,swt,Java,Layout,Swt,我用一些组件构建了自己的“查找”对话框来配置搜索,并希望使用GridLayout布局控件。 但我无法放置“确定”和“取消”按钮。我想将这些按钮居中,使窗口左边缘和ok按钮之间的边距、ok按钮和cancel按钮之间的边距以及cancel按钮和窗口右边缘之间的边距相同 这是我当前的代码: public class Main { public static void main(String[] args) { Display display = new Display(); She

我用一些组件构建了自己的“查找”对话框来配置搜索,并希望使用GridLayout布局控件。
但我无法放置“确定”和“取消”按钮。我想将这些按钮居中,使窗口左边缘和ok按钮之间的边距、ok按钮和cancel按钮之间的边距以及cancel按钮和窗口右边缘之间的边距相同

这是我当前的代码:

public class Main {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    GridLayout gridLayout = new GridLayout();
    gridLayout.horizontalSpacing = 0;
    gridLayout.makeColumnsEqualWidth = true;
    gridLayout.marginHeight = 10;
    gridLayout.marginWidth = 10;
    gridLayout.numColumns = 5;
    gridLayout.verticalSpacing = 10;
    shell.setLayout(gridLayout);
    shell.setText("Find");

    GridData gridData = new GridData();
    gridData.horizontalSpan = 5;
    Label label1 = new Label(shell, SWT.NONE);
    label1.setLayoutData(gridData);
    label1.setText("Please enter the find text in the field below:");

    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalSpan = 5;
    Text text1 = new Text(shell, SWT.BORDER | SWT.H_SCROLL | SWT.SINGLE);
    text1.setLayoutData(gridData);

    gridData = new GridData();
    gridData.horizontalSpan = 5;
    Button check1 = new Button(shell, SWT.CHECK);
    check1.setLayoutData(gridData);
    check1.setText("Match case sensitivity");

    new Label(shell, SWT.NONE);

    Button okButton = new Button(shell, SWT.PUSH);
    okButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
    okButton.setText("&OK");
    shell.setDefaultButton(okButton);

    new Label(shell, SWT.NONE);

    Button cancelButton = new Button(shell, SWT.PUSH);
    cancelButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
    cancelButton.setText("&Cancel");

    new Label(shell, SWT.NONE);

    shell.pack();
    shell.open();

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}
我的问题是“取消”按钮似乎居中,但“确定”按钮仍在左侧对齐。
我想要的是一个位置,例如:左边缘,50像素,ok按钮,50像素,cancel按钮,50像素,right edge

应该是这样的:

+++编辑+++

好的,我找到了解决办法。它并不完美,但以令人满意的方式符合我的要求:

我现在使用的是两个复合控件,一个用于“主”控件,另一个用于按钮。
我坐在一个网格布局上,对着窗户一圈,把合成材料放在彼此之间。
按钮组合也有一个GridLayout,但有两列表示ok和cancel。然后,我可以将窗口中的按钮与GridData对象放在一起。
我唯一的问题是按钮之间的水平间距。我必须将它设置为一个绝对值,但最好有一个动态值(可能取决于窗口的整个宽度)。但是我还没有意识到这一点。
以下是我构建的:

public class Main {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));
    shell.setText("Find");

    Composite mainComp = new Composite(shell, SWT.NONE);
    mainComp.setLayout(new GridLayout(1, false));
    mainComp.setLayoutData(new GridData());

    Label label1 = new Label(mainComp, SWT.NONE);
    label1.setLayoutData(new GridData());
    label1.setText("Please enter the find text in the field below:");

    Text text1 = new Text(mainComp, SWT.BORDER | SWT.H_SCROLL | SWT.SINGLE);
    text1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    Button check1 = new Button(mainComp, SWT.CHECK);
    check1.setLayoutData(new GridData());
    check1.setText("Match case sensitivity");

    Composite buttonComp = new Composite(shell, SWT.NONE);
    GridLayout buttonCompLayout = new GridLayout();
    buttonCompLayout.horizontalSpacing = 50;
    buttonCompLayout.numColumns = 2;
    buttonComp.setLayout(buttonCompLayout);
    buttonComp.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));

    Button okButton = new Button(buttonComp, SWT.PUSH);
    okButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
    okButton.setText("&OK");
    shell.setDefaultButton(okButton);

    Button cancelButton = new Button(buttonComp, SWT.PUSH);
    cancelButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
    cancelButton.setText("&Cancel");

    shell.pack();
    shell.open();

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}

如果您想根据窗口的宽度确定间距,您可以收听
SWT.Resize
,然后更新
GridLayout#horizontalspacking
。然后,在父级上调用
layout()

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout());
    shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    final Composite comp = new Composite(shell, SWT.BORDER);

    final GridLayout layout = new GridLayout(2, false);
    layout.horizontalSpacing = 50;

    comp.setLayout(layout);
    comp.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false));

    Button first = new Button(comp, SWT.PUSH);
    first.setText("First");
    first.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

    Button second = new Button(comp, SWT.PUSH);
    second.setText("Second");
    second.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

    shell.addListener(SWT.Resize, new Listener()
    {
        @Override
        public void handleEvent(Event event)
        {
            int width = shell.getSize().x;
            layout.horizontalSpacing = (int) Math.round(0.1 * width);
            shell.layout(true, true);
        }
    });

    shell.pack();
    shell.open();

    shell.forceFocus();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
看起来像这样:


你的想法很酷,但现在我遇到了一些奇怪的问题:窗口突然变大了一倍(380像素,而不是以前的190像素),我的文本字段是400像素(比窗口本身宽),并与窗口的右边缘重叠。我不知道GridLayout在这里计算了什么…@altralaser如果没有你的最终代码,我也不知道。@altralaser更新了我的答案。它不再依赖
标签了。首先,感谢您的帮助!不幸的是,代码的结果与我的第一个示例中的结果相同。唯一的区别是现在左边和右边的边距变宽了。但是按钮没有在文本字段或复选框中居中,并且在“确定”和“取消”之间没有“良好”边距。请问您在对话框中使用什么来放置“确定”和“取消”按钮?有没有类似于非官方标准的东西?@altralaser请发布一张图片,展示你想要它的样子,因为我不知道你想要什么。