swt - 如何将 SWT 对象绑定到应用程序窗口的中心?
问题描述
我正在使用 SWT 创建应用程序 GUI,并且我并不真的需要调整组件的大小,但是当窗口最大化时,组件保持左对齐确实让我感到困扰。有没有办法用 SWT 解决这个问题,还是我需要使用一组不同的 GUI 工具?
提前致谢。我为此应用程序使用 SWT 4.8。
编辑:图片
最大化: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,因为它不会在您调整应用程序大小时调整组件的大小。使用布局,例如下面的例子,我使用了 GridLayout,当调整大小发生时Shell
,Composite
它会正确排列 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);
}
}
推荐阅读
- python - Python,“[i]”附近的Sqlite:语法错误错误
- javascript - 这个 JavaScript 表达式是什么意思?
- reactjs - 使用 ReactJS 和 useEffect 单击时无法暂停间隔
- twilio - Twilio 在使用循环重复语音通话时有暂停期
- postgresql - 设置 Kubernetes 集群并运行数据库
- javascript - 我们不能在 React.js 的数组中迭代 material-ui 吗?
- c++ - 如何打印 NXN 框的对角线?检查我的代码?
- reactjs - 使用 useEffect 清理先前组件的 React Redux 双挂载
- python - 为 Django 组中的用户分配权限
- python - 避免 Zipfile.write 中的日期更改