在 Java SWT 窗口中启用滚动条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10145547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enabling scroll bars in a Java SWT window
提问by Aaron
How will I enable scroll bars to display on the custom.StyledText? It displays the scroll bars but does not allow the custom.StyledText to scroll?
我将如何启用滚动条以在 custom.StyledText 上显示?它显示滚动条但不允许 custom.StyledText 滚动?
package app;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.ScrolledComposite;
public class myapp {
protected Shell shlWhois;
private Text text;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
myapp window = new myapp();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shlWhois.open();
shlWhois.layout();
while (!shlWhois.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shlWhois = new Shell();
shlWhois.setSize(450, 300);
shlWhois.setText("Whois");
Label lblDomain = new Label(shlWhois, SWT.NONE);
lblDomain.setBounds(10, 10, 55, 15);
lblDomain.setText("Domain");
text = new Text(shlWhois, SWT.BORDER);
text.setBounds(72, 4, 260, 21);
final ScrolledComposite scrolledComposite_1 = new ScrolledComposite(shlWhois, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
scrolledComposite_1.setAlwaysShowScrollBars(true);
final Composite c = new Composite(scrolledComposite_1, SWT.NONE);
scrolledComposite_1.setMinHeight(1000);
scrolledComposite_1.setExpandVertical(true);
scrolledComposite_1.setExpandHorizontal(true);
scrolledComposite_1.setBounds(10, 31, 403, 221);
final StyledText styledText = new StyledText(scrolledComposite_1, SWT.BORDER | SWT.WRAP);
scrolledComposite_1.setContent(styledText);
scrolledComposite_1.setMinSize(styledText.computeSize(SWT.DEFAULT, SWT.DEFAULT));
Button btnWhois = new Button(shlWhois, SWT.NONE);
btnWhois.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent arg0) {
}
});
btnWhois.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent arg0) {
}
});
btnWhois.setBounds(338, 2, 75, 25);
btnWhois.setText("Search");
}
}
回答by Baldrick
You don't need the ScrolledComposite
, use SWT.V_SCROLL
style on the StyledText
. Here is an example, with layoutsinstead of the setBounds
:
您不需要ScrolledComposite
,请SWT.V_SCROLL
在StyledText
. 这是一个示例,使用布局而不是setBounds
:
/**
* Create contents of the window.
*/
protected void createContents() {
shlWhois = new Shell();
shlWhois.setSize(450, 300);
shlWhois.setText("Whois");
shlWhois.setLayout(new GridLayout(2, false));
Label lblDomain = new Label(shlWhois, SWT.NONE);
lblDomain.setLayoutData(GridDataFactory.fillDefaults().create());
lblDomain.setText("Domain");
text = new Text(shlWhois, SWT.BORDER);
text.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());
final StyledText styledText = new StyledText(shlWhois, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI | SWT.WRAP);
styledText.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).span(2, 1).create());
Button btnWhois = new Button(shlWhois, SWT.NONE);
btnWhois.setText("Search");
}
You can use the styles SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI
if you also want an horizontal scrollbar.
SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI
如果您还需要水平滚动条,则可以使用这些样式。