在javaswt中,向复合布局添加滚动条是不起作用的



我的问题是要将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 );
}
}

最新更新