Java 同一外壳上的表和工具栏
我需要帮助如何将工具栏和表放在同一个shell SWT窗口上 我遇到的问题是,如果我把它们都放在表格上,它们中的每一个都覆盖了外壳窗口的一半:Java 同一外壳上的表和工具栏,java,eclipse,shell,toolbar,Java,Eclipse,Shell,Toolbar,我需要帮助如何将工具栏和表放在同一个shell SWT窗口上 我遇到的问题是,如果我把它们都放在表格上,它们中的每一个都覆盖了外壳窗口的一半: import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Menu
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.ToolItem;
public class MainLayout {
protected Shell shell;
private Table table;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
MainLayout window = new MainLayout();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
shell.setLayout(new FillLayout(SWT.HORIZONTAL));
Menu menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu);
MenuItem menuItem = new MenuItem(menu, SWT.CASCADE);
menuItem.setText("New SubMenu");
Menu menu_2 = new Menu(menuItem);
menuItem.setMenu(menu_2);
table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
table.setLinesVisible(true);
//ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT);
ToolBar toolBar = new ToolBar(shell, SWT.HORIZONTAL);
ToolItem item = new ToolItem(toolBar, SWT.PUSH);
item.setText("Button One");
}
}
你能帮我一下吗?改用网格布局。您还需要设置GridData以自定义小部件布局 示例代码:
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
shell.setLayout(new GridLayout(1,false)); // change
Menu menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu);
MenuItem menuItem = new MenuItem(menu, SWT.CASCADE);
menuItem.setText("New SubMenu");
Menu menu_2 = new Menu(menuItem);
menuItem.setMenu(menu_2);
table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION );
table.setHeaderVisible(true);
table.setLinesVisible(true);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); // change
//ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT);
ToolBar toolBar = new ToolBar(shell, SWT.HORIZONTAL);
ToolItem item = new ToolItem(toolBar, SWT.PUSH);
item.setText("Button One");
}
参考资料: