gpt4 book ai didi

java - 按钮中的 SWT SWT.WRAP

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:38 25 4
gpt4 key购买 nike

我想用 SWT.WRAP 创建一个复选框按钮。当我调整窗口大小时,文本应相应换行。

我创建了一个带有 2 个复选框的代码段:

1) 文字不换行

2) 文本自动换行,但在调整窗口大小时不会更新

public class GridLayoutSample {
Display display = new Display();
Shell shell = new Shell(display);

private static final String lorem = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ips";

public GridLayoutSample() {

GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 1;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout(gridLayout);

Button button = new Button(shell, SWT.CHECK | SWT.WRAP);
button.setText(lorem);
button.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL));

RowLayout layout = new RowLayout();
Composite row = new Composite(shell, SWT.NONE);
row.setLayout(layout);
createButton(row, lorem, SWT.CHECK | SWT.WRAP);

shell.pack();
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}

display.dispose();
}
public void createButton(Composite parent, String text, int style) {
RowData layoutData = new RowData();
layoutData.height = 175;
layoutData.width = 175;
Button testButton = new Button(parent, style);
testButton.setText(text);
testButton.setLayoutData(layoutData);
}

public static void main(String[] args) {
new GridLayoutSample();
}
}

你能帮帮我吗?

最佳答案

应该这样做:

Display                     display = new Display();
Shell shell = new Shell(display);

private static final String lorem = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ips";

public GridLayoutSample()
{
shell.setLayout(new GridLayout(1, false));
Composite row = new Composite(shell, SWT.NONE);
row.setLayout(new GridLayout(1, false));
row.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

createButton(row, lorem, SWT.CHECK | SWT.WRAP);

shell.pack();
shell.open();

while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}

display.dispose();
}

public void createButton(final Composite parent, String text, int style)
{
final GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
layoutData.widthHint = 175;
final Button testButton = new Button(parent, style);
testButton.setText(text);
testButton.setLayoutData(layoutData);

parent.getParent().addListener(SWT.Resize, new Listener()
{
@Override
public void handleEvent(Event arg0)
{
layoutData.widthHint = parent.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
parent.pack();
}
});
}

public static void main(String[] args)
{
new GridLayoutSample();
}

它将根据父级的大小调整 Button 的大小。

这是启动后的样子:

enter image description here

调整大小后:

enter image description here

关于java - 按钮中的 SWT SWT.WRAP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21779290/

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