gpt4 book ai didi

java - 在 java swt 中向复合布局添加滚动条不起作用

转载 作者:行者123 更新时间:2023-12-01 22:12:54 25 4
gpt4 key购买 nike

我的问题是我想向组合添加一个滚动条

基本上我有一个带有按钮的组合。当您按下按钮时,新内容将添加到合成中(标签和文本)。一段时间后,内容溢出复合 Material 。我想添加一个滚动条,以便您在添加它们时可以看到所有文本。

我已经尝试过 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();
}
}
}
}

我的目标是当内容超出复合时可以看到滚动条。滚动后应该可以看到整个内容

最佳答案

谢谢格雷格和洛里斯的回答,我现在设法添加滚动复合 Material ,因此我也使用了像洛里斯所说的网格布局并改变了我的结构。它几乎可以工作,但现在我遇到了一个小错误,我还无法修复。

每当我按下按钮时,它都不会直接添加新行内容。它仅在我调整整个应用程序窗口的大小时添加它。

滚动条按照我想要的方式正常工作,但就像我说的,它只在调整 shell 大小时添加内容,但我希望它自动添加内容。我怎么能做到这一点。

这是新代码:

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 );
}
}

关于java - 在 java swt 中向复合布局添加滚动条不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58644497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com