SWT 菜单导致布局裁剪
我是第一次尝试 SWT,我不知道如何使用菜单栏以及任何填充空间的布局。组合的布局是在不考虑菜单的情况下计算的,因此一旦添加菜单,布局的底部就会被裁剪。
令人惊讶的是,当用户调整大小时,菜单会被考虑在内,并且布局也很好。但我不知道如何以编程方式修复它; shell.layout(true,true)
、shell.setSize(250,250)
、shell.pack()
不能解决问题。
package com.appspot.htmldoodads.pdfstuffs;
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.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class MenuLayout {
public static void main(String[] argv) {
final Display display = new Display();
final Shell shell = new Shell(display);
GridLayout shellLayout = new GridLayout();
shell.setLayout(shellLayout);
// Add a menu bar with File -> Open...
Menu bar = new Menu(shell, SWT.BAR);
shell.setMenuBar(bar);
MenuItem fileItem = new MenuItem(bar, SWT.CASCADE);
fileItem.setText("&File");
Menu subMenu = new Menu(shell, SWT.DROP_DOWN);
fileItem.setMenu(subMenu);
MenuItem openItem = new MenuItem(subMenu, SWT.CASCADE);
openItem.setText("&Open...");
// Add a button that fills the space.
Button big = new Button(shell, SWT.PUSH);
big.setText("Fill.");
GridData bigLayoutData = new GridData(GridData.FILL, GridData.FILL, true, true);
big.setLayoutData(bigLayoutData);
// Add a button that doesn't fill space.
new Button(shell, SWT.PUSH).setText("Regular");
shell.layout(true, true);
shell.setSize(250,250);
shell.open();
while ( ! shell.isDisposed ()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
I'm trying SWT for the first time, and I can't figure out how to use a Menu bar along with any space-filling layout. The Composite's layout is calculated without taking the Menu into account, so that once the Menu is added the bottom of the layout gets clipped.
Amazingly, when the user resizes, the Menu is taken into account and the layout is fine. But I can't figure out how to fix it programmatically; shell.layout(true,true)
, shell.setSize(250,250)
, shell.pack()
don't fix the problem.
package com.appspot.htmldoodads.pdfstuffs;
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.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class MenuLayout {
public static void main(String[] argv) {
final Display display = new Display();
final Shell shell = new Shell(display);
GridLayout shellLayout = new GridLayout();
shell.setLayout(shellLayout);
// Add a menu bar with File -> Open...
Menu bar = new Menu(shell, SWT.BAR);
shell.setMenuBar(bar);
MenuItem fileItem = new MenuItem(bar, SWT.CASCADE);
fileItem.setText("&File");
Menu subMenu = new Menu(shell, SWT.DROP_DOWN);
fileItem.setMenu(subMenu);
MenuItem openItem = new MenuItem(subMenu, SWT.CASCADE);
openItem.setText("&Open...");
// Add a button that fills the space.
Button big = new Button(shell, SWT.PUSH);
big.setText("Fill.");
GridData bigLayoutData = new GridData(GridData.FILL, GridData.FILL, true, true);
big.setLayoutData(bigLayoutData);
// Add a button that doesn't fill space.
new Button(shell, SWT.PUSH).setText("Regular");
shell.layout(true, true);
shell.setSize(250,250);
shell.open();
while ( ! shell.isDisposed ()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我完成这个问题时,我想到了一个解决方案。我需要调用
shell.layout()
aftershell.open()
,以便 GTK 可以在 SWT 计算可用空间之前渲染菜单用于布局。I figured out a solution just as I was finishing the question. I needed to call
shell.layout()
aftershell.open()
, so that GTK can render the Menu before SWT calculates the available space for layout.添加到 yonran 的答案:
如果使用 JFace 和 ApplicationWindow 作为您的主入口点,为您执行
shell.open()
。因此,您必须使用典型的 SWT 循环,而不是使用 JFace 阻塞applicationWindow.open()
方法调用。JFace 之前(不起作用,并且存在问题中描述的剪切问题):
JFace 之后(正确显示,没有任何剪切):
Adding to yonran's answer:
If using JFace and and a subclass of ApplicationWindow as your main entry point,
shell.open()
is performed for you. So instead of using the JFace blockingapplicationWindow.open()
method call, you have to use the typical SWT loop.JFace Before (Didn't work, and had clipping issue described in question):
JFace After (Correctly displays without any clipping):