java - 向复合布局添加滚动条在 java swt 中不起作用
问题描述
我的问题是我想将 a 添加Scrollbar
到复合材料中。
基本上我有一个带按钮的复合材料。当您按下按钮时,新内容将添加到合成中(标签和文本)。一段时间后,内容溢出复合材料。我想添加一个滚动条,以便您在添加它们时可以看到所有文本。
我已经尝试过ScrolledComposite
,但不知何故它不会滚动我认为因为它不承认复合的内容超过
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
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.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Slider;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.layout.RowData;
import swing2swt.layout.BoxLayout;
public class test {
protected static String _intentId;
static Text intentNameText;
static Text utterance;
static Text pattern;
static int increaser=0;
static int counterNumb=1;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell( display );
shell.setSize(602,474);
Composite container = new Composite(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.FILL);
container.setBounds(21, 10, 553, 407);
Label lblCreateAnIntent = new Label(container, SWT.NONE);
lblCreateAnIntent.setBounds(36, 30, 193, 20);
lblCreateAnIntent.setText("Create an Intent");
final Composite innerComposite = new Composite(container, SWT.NONE);
innerComposite.setBounds(59, 54, 475, 319);;
//innerComposite.layout();
Label lblIntentName = new Label(innerComposite, SWT.NONE);
lblIntentName.setBounds(10, 23, 97, 20);
lblIntentName.setText("Intent Name:");
intentNameText = new Text(innerComposite, SWT.BORDER);
intentNameText.setBounds(113, 23, 78, 26);
Label lblUtterances = new Label(innerComposite, SWT.NONE);
lblUtterances.setText("Utterances");
lblUtterances.setBounds(41, 83, 120, 20);
Label lblPattern = new Label(innerComposite, SWT.NONE);
lblPattern.setText("Pattern");
lblPattern.setBounds(220, 84, 120, 20);
utterance = new Text(innerComposite, SWT.BORDER);
utterance.setBounds(41, 109, 150, 26);
pattern = new Text(innerComposite, SWT.BORDER);
pattern.setBounds(220, 110, 150, 26);
final Button btnNewButton = new Button(innerComposite, SWT.NONE);
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
increaser=increaser+40;
Label number0 = new Label(innerComposite, SWT.NONE);
number0.setText(Integer.toString(counterNumb));
number0.setBounds(20, 112, 15, 20);
counterNumb=counterNumb+1;
Label number = new Label(innerComposite, SWT.NONE);
number.setText(Integer.toString(0+counterNumb) + ".");
number.setBounds(20, 112+increaser, 15, 20);
utterance = new Text(innerComposite, SWT.BORDER);
utterance.setBounds(41, 109+increaser, 150, 26);
pattern = new Text(innerComposite, SWT.BORDER);
pattern.setBounds(220, 110+increaser, 150, 26);
btnNewButton.setBounds(41, 150+increaser, 121, 30);
}
});
btnNewButton.setBounds(41, 150, 121, 30);
btnNewButton.setText("Add Utterance");
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
我的目标是当内容超过合成时可以看到滚动条。滚动后应该看到整个内容
解决方案
谢谢 greg 和 loris 的回答,我现在设法添加了一个滚动合成,因此我也使用了 loris 说的网格布局并改变了我的结构。它几乎可以工作,但现在我遇到了一个我还无法修复的小错误。
每当我按下按钮时,它都不会直接添加新的内容行。它仅在我调整整个应用程序窗口的大小时添加。
滚动条按我的意愿正常工作,但就像我说的它只在调整外壳大小时添加内容,但我希望它自动添加它。我怎么能这样。
这是新代码:
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
/**
* Shows an expanding ScrolledComposite that scrolls only vertically.
*/
public class vertform {
public static void main( String[] args ) {
new vertform().run();
}
private Shell shell;
private ScrolledComposite scrolledComposite;
private Text text_1;
private Composite result;
void run() {
createControls();
layoutControls();
openShell();
runEventLoop();
}
private void createControls() {
shell = new Shell( new Display() );
shell.setSize(577, 391);
shell.setText( "Contact Details Form" );
shell.addListener( SWT.Dispose, event -> event.display.dispose() );
Label lblIntentName = new Label(shell, SWT.NONE);
lblIntentName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblIntentName.setText("some text");
text_1 = new Text(shell, SWT.BORDER);
new Label(shell, SWT.NONE);
Composite composite = new Composite(shell, SWT.NONE);
GridData gd_composite = new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1);
gd_composite.heightHint = 52;
gd_composite.widthHint = 448;
composite.setLayoutData(gd_composite);
Label lblNewLabel_1 = new Label(composite, SWT.NONE);
lblNewLabel_1.setBounds(10, 34, 70, 20);
lblNewLabel_1.setText("some Text x");
Label lblPattern = new Label(composite, SWT.NONE);
lblPattern.setText("some text y");
lblPattern.setBounds(231, 34, 70, 20);
new Label(shell, SWT.NONE);
scrolledComposite = new ScrolledComposite( shell, SWT.V_SCROLL );
Composite parent = createForm();
scrolledComposite.setContent( parent );
scrolledComposite.setExpandVertical( true );
scrolledComposite.setExpandHorizontal( true );
scrolledComposite.addListener( SWT.Resize,
event -> {
int width = scrolledComposite.getClientArea().width;
scrolledComposite.setMinSize( parent.computeSize( width, SWT.DEFAULT ) );
} );
}
private void layoutControls() {
shell.setLayout( new GridLayout( 2, false ) );
GridData gridData = new GridData( SWT.FILL, SWT.FILL, true, true );
gridData.heightHint = computePreferredHeight();
scrolledComposite.setLayoutData( gridData );
}
private void openShell() {
shell.pack();
shell.open();
}
private void runEventLoop() {
while( !shell.isDisposed() ) {
if( !shell.getDisplay().readAndDispatch() ) {
shell.getDisplay().sleep();
}
}
}
private int computePreferredHeight() {
int numberOfLines = 1;
int defaultHorizontalSpacing = 5;
new Label(shell, SWT.NONE);
Button btnNewButton = new Button(shell, SWT.NONE);
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
createField(result);
}
});
btnNewButton.setText("Add content");
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
Text text = new Text( shell, SWT.BORDER );
GridData gd_text = new GridData(SWT.LEFT, SWT.BOTTOM, false, false, 1, 1);
gd_text.heightHint = 138;
gd_text.widthHint = 587;
text.setLayoutData(gd_text);
text.setText( "X" );
Point preferredSize = text.computeSize( SWT.DEFAULT, SWT.DEFAULT );
text.dispose();
return numberOfLines * ( preferredSize.y + defaultHorizontalSpacing );
}
private Composite createForm() {
result=new Composite( scrolledComposite, SWT.NONE );
result.setLayout( new GridLayout( 2, false ) );
createField( result);
return result;
}
private static void createField( Composite parent) {
Text text1=new Text(parent, SWT.BORDER);
Text text = new Text( parent, SWT.BORDER );
GridData gd_text = new GridData( SWT.LEFT, SWT.CENTER, false, false );
gd_text.widthHint = 208;
text.setLayoutData( gd_text );
GridData gd_text1 = new GridData( SWT.LEFT, SWT.CENTER, false, false );
gd_text1.widthHint = 203;
text1.setLayoutData( gd_text1 );
}
}
推荐阅读
- amazon-web-services - 您可以使用代理集成将阶段变量传递给 lambda 吗?
- android - Android - 从堆栈中间删除特定片段不起作用
- pandas - 更新通过 DataFrame.groupby.apply 调用的函数内部的 pandas 数据框
- php - 在 codeigniter 上的同一函数中获取 num_rows 和 result() 数据
- r - 数据框根据结果集创建多行
- python - 在python中拆分不明显的混乱字符串
- javascript - 当您向下移动鼠标单击时,下拉菜单会消失
- html - 全球化文化如何影响 MVC 日期
- java - 实现 X509TrustManager
- amazon-web-services - user_data template_file 中的 aws_autoscaling_group 和 count.index