java自动布局,java – 布局问题:自动增长标签(SWT)

我正在使用GridLayout尝试制作标签自动增长而不隐藏其内容的任何内容.这是一个简单的测试代码:每次按下按钮时标签文本都会变大,但只有在我水平调整窗口大小后才会得到正确的布局.

有没有办法解决这个问题,而无需调整窗口大小?

我想我已经尝试过每一处房产,但我仍然无法使其工作,这让我疯狂!

这就是我得到的

这应该是这样的

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.SelectionAdapter;

import org.eclipse.swt.events.SelectionEvent;

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.Label;

import org.eclipse.swt.widgets.List;

import org.eclipse.swt.widgets.Shell;

public class UItest {

protected Shell shell;

private Label label;

public static void main(String[] args) {

try {

UItest window = new UItest();

window.open();

} catch (Exception e) {

e.printStackTrace();

}

}

public void open() {

Display display = Display.getDefault();

createContents();

shell.open();

shell.layout();

while (!shell.isDisposed()) {

if (!display.readAndDispatch()) {

display.sleep();

}

}

display.dispose();

}

protected void createContents() {

shell = new Shell();

shell.setSize(450, 300);

shell.setText("SWT Application");

shell.setLayout(new GridLayout(1, false));

Button button = new Button(shell, SWT.NONE);

button.addSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent arg0) {

label.setText(label.getText() + " " + label.getText());

}

});

button.setText("New Button");

label = new Label(shell, SWT.WRAP);

label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));

label.setText("New Label");

List list = new List(shell, SWT.BORDER);

list.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

}

protected Label getLabel() {

return label;

}

}

谢谢你的时间.

解决了这些变化:

Button button = new Button(shell, SWT.NONE);

button.addSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent arg0) {

label.setText(label.getText() + " " + label.getText());

shell.layout(); // ADDED THIS

}

});

button.setText("New Button");

label = new Label(shell, SWT.WRAP);

// SET HORIZONTAL GRAB ON LABEL (FIRST TRUE IN GridData CONSTRUCTOR)

label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));

label.setText("New Label");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值