Java SWT-网格数据和按钮
我有一个创建两个按钮的方法。这些按钮与“确定”、“关闭”按钮不同。单击时,它们将执行不同的操作。我想要两个按钮并排在我的基本组合的顶部。它使用GridLayout。我希望能够并排放置按钮 这是我正在添加方法的createDialogAreaJava SWT-网格数据和按钮,java,layout,swt,Java,Layout,Swt,我有一个创建两个按钮的方法。这些按钮与“确定”、“关闭”按钮不同。单击时,它们将执行不同的操作。我想要两个按钮并排在我的基本组合的顶部。它使用GridLayout。我希望能够并排放置按钮 这是我正在添加方法的createDialogArea protected Control createDialogArea(Composite parent) { final Composite area = new Composite(parent, SWT.NONE); final GridLa
protected Control createDialogArea(Composite parent) {
final Composite area = new Composite(parent, SWT.NONE);
final GridLayout gridLayout = new GridLayout();
gridLayout.marginWidth = 15;
gridLayout.marginHeight = 10;
area.setLayout(gridLayout);
createTopButtons(area);
createTableViewer(area);
return area;
}
下面是按钮方法
protected void createTopButtons(Composite parent) {
Button pdfButton = new Button(parent, SWT.PUSH);
pdfButton.setText("Create PDF");
pdfButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
close();
}
});
Button plotButton = new Button(parent, SWT.PUSH);
plotButton.setText("Plot");
plotButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
close();
}
});
}
是否需要向createTopButtons添加Gridlayout?如果是这样的话,我怎样才能让它们并排而不是一个在另一个之上呢 同样在createDialogArea中,我可以使用gridLayout排列我的组件吗 示例-我希望createTopButtons位于左上方,然后我希望createTableViewer居中 您是否使用正在创建的组合中的布局(如createTopButtons)来排列项目/按钮/标签。然后使用createDialogArea中的布局来排列由方法创建的合成?Q&a 我是否需要向createTopButtons添加Gridlayout 不,您需要将按钮放在另一个组合中,这个子组合应该有一个包含两列的gridlayout 同样在createDialogArea中,我可以使用 网格布局?示例-我希望createTopButtons位于左上方,然后我希望createTableViewer居中 当然,为什么不呢。但是您还需要一个名为
GridData
的东西,并将其设置为area.setLayoutData(GridData)代码>。对于居中,您的griddata可能如下所示griddata=newGridData(SWT.CENTER,SWT.CENTER,true,false)代码>
您是否使用内部布局来排列项目/按钮/标签
您正在创建的组合像createTopButtons一样。然后在中使用布局
createDialogArea来排列由方法创建的组合
对。虽然我无法得到由方法创建的下半部分合成
代码
代码输出
进一步阅读
这是理解SWT中布局的一个好方法。您为我回答了这么多问题。非常感谢您以代码为例进行演示。学习一个例子要容易得多。所有的问题都应该这样回答!
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
public class SideBySide {
public static void main(String[] args) {
new SideBySide().start();
}
private Shell shell;
public void start()
{
Display display = new Display();
shell = new Shell(display);
shell.setLayout(new GridLayout(1, true));
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
shell.setLayoutData(gridData);
shell.setText("Side By Side");
createDialogArea(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
protected Control createDialogArea(Composite parent)
{
final Composite area = new Composite(parent, SWT.NONE);
final GridLayout gridLayout = new GridLayout();
gridLayout.marginWidth = 15;
gridLayout.marginHeight = 10;
area.setLayout(gridLayout);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
shell.setLayoutData(gridData);
area.setLayoutData(gridData);
createTopButtons(area);
createTableViewer(area);
return area;
}
private void createTableViewer(Composite area)
{
Table table = new Table(area, SWT.BORDER|SWT.V_SCROLL|SWT.FULL_SELECTION);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
table.setLayoutData(gridData);
table.setLinesVisible(true);
table.setHeaderVisible(true);
TableColumn column = new TableColumn(table, SWT.LEFT);
column.setWidth(320);
column.setText("Column 1");
column = new TableColumn(table, SWT.LEFT);
column.setWidth(320);
column.setText("Column 2");
}
protected void createTopButtons(Composite parent)
{
Composite composite = new Composite(parent, SWT.NONE);
GridLayout gridLayout = new GridLayout(2, false);
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.verticalSpacing = 0;
gridLayout.horizontalSpacing = 0;
composite.setLayout(gridLayout);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
composite.setLayoutData(gridData);
gridData = new GridData(SWT.DEFAULT, SWT.FILL, false, false);
Button pdfButton = new Button(composite, SWT.PUSH);
pdfButton.setText("Create PDF");
pdfButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.close();
}
});
pdfButton.setLayoutData(gridData);
Button plotButton = new Button(composite, SWT.PUSH);
plotButton.setText("Plot");
plotButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.close();
}
});
plotButton.setLayoutData(gridData);
}
}