我正在尝试制作用于聊天的GUI,并且我连续放置了三个SWT控件.
不幸的是,我无法为他们找到漂亮的路线.
以下代码
Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout(3,false)); Label firstLabel = new Label(shell,SWT.NONE); firstLabel.setText("PROMPT:"); firstLabel.setLayoutData(new GridData(SWT.BEGINNING,SWT.CENTER,false,false)); Text firstText = new Text(shell,SWT.NONE); firstText.setText("hello"); firstText.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false)); Button firstButton = new Button(shell,SWT.PUSH); firstButton.setText("Say"); firstButton.setLayoutData(new GridData(SWT.BEGINNING,false)); shell.pack(); shell.open(); shell.setSize(400,300); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose();
给
即文本字段与按钮一样高,但文本不在其内部垂直居中.
如果以下方式编写文本代码
Text firstText = new Text(shell,false));
我会得到
最美丽的结果将由
Text firstText = new Text(shell,SWT.BORDER); firstText.setText("hello"); firstText.setLayoutData(new GridData(SWT.FILL,false));
是的
但是按钮再次比文本略高.
UPDATE
人们说他们在非Windows平台上有很好的结果,但问题可能是如何控制SWT控件的填充/边距.
更新2
即使在最好的情况下,文本字段的高度也小于Windows上的按钮,结果与平台有关.
这是一个代码示例,可以让您了解如何解决您的问题:
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout(3,false)); Label secondLabel = new Label(shell,SWT.NONE); secondLabel.setText("PROMPT:"); secondLabel.setLayoutData(new GridData(SWT.BEGINNING,true)); Text secondText = new Text(shell,SWT.BORDER | SWT.MULTI | SWT.WRAP); secondText.setText("hello"); secondText.setLayoutData(new GridData(SWT.FILL,true)); Button secondButton = new Button(shell,SWT.PUSH); secondButton.setText("Say"); secondButton.setLayoutData(new GridData(SWT.BEGINNING,true)); shell.pack(); shell.open(); shell.setSize(400,300); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
看起来像这样:
没有SWT.BORDER: