gpt4 book ai didi

swt - 如何将 SWT 对象绑定(bind)到应用程序窗口的中心?

转载 作者:行者123 更新时间:2023-12-01 11:15:52 25 4
gpt4 key购买 nike

我正在使用 SWT 创建一个应用程序 GUI,我真的不需要调整组件的大小,但是当窗口最大化时,组件保持左对齐确实让我感到困扰。有没有办法用 SWT 解决这个问题,还是我需要使用一组不同的 GUI 工具?

提前致谢。我为此应用程序使用 SWT 4.8。

编辑:图片

小:https://imgur.com/CPbAlaZ

最大化:https://imgur.com/4d6YXcl

提供的图像是使用以下代码的基本应用程序

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

public class TestWindow {

protected Shell shlSwtApplicationExample;
private Text text;

/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
TestWindow window = new TestWindow();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shlSwtApplicationExample.open();
shlSwtApplicationExample.layout();
while (!shlSwtApplicationExample.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}

/**
* Create contents of the window.
*/
protected void createContents() {
shlSwtApplicationExample = new Shell();
shlSwtApplicationExample.setSize(705, 529);
shlSwtApplicationExample.setText("SWT Application Example");

Composite composite = new Composite(shlSwtApplicationExample, SWT.NONE);
composite.setBounds(10, 10, 669, 465);

text = new Text(composite, SWT.BORDER);
text.setBounds(22, 10, 334, 295);

Button btnNewButton = new Button(composite, SWT.NONE);
btnNewButton.setBounds(49, 384, 137, 26);
btnNewButton.setText("New Button");

Button button = new Button(composite, SWT.NONE);
button.setText("New Button");
button.setBounds(300, 384, 137, 26);

}
}

最佳答案

我不建议使用 setBounds,因为它不会在您调整应用程序大小时调整组件的大小。使用布局,如下例所示,我为 ShellComposite 使用了 GridLayout,这将在调整大小时正确安排 UI。

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

public class TestWindow {

protected Shell shlSwtApplicationExample;
private Text text;

/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
TestWindow window = new TestWindow();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents(display);
shlSwtApplicationExample.open();
shlSwtApplicationExample.layout();
shlSwtApplicationExample.setLayout(new GridLayout(1, false));
while (!shlSwtApplicationExample.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}

/**
* Create contents of the window.
* @param display
*/
protected void createContents(Display display) {
shlSwtApplicationExample = new Shell(display);
shlSwtApplicationExample.setLayout(new GridLayout(1, false));

Composite txtcomposite = new Composite(shlSwtApplicationExample, SWT.NONE);
txtcomposite.setLayout(new GridLayout(1, false));
txtcomposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

Composite btncomposite = new Composite(shlSwtApplicationExample, SWT.NONE);
btncomposite.setLayout(new GridLayout(2, false));
btncomposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

text = new Text(txtcomposite, SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

Button btnNewButton = new Button(btncomposite, SWT.NONE);
btnNewButton.setText("New Button");

Button button = new Button(btncomposite, SWT.NONE);
button.setText("New Button");

shlSwtApplicationExample.setText("SWT Application Example");
//shlSwtApplicationExample.setSize(705, 529);

}
}

关于swt - 如何将 SWT 对象绑定(bind)到应用程序窗口的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51442612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com