gpt4 book ai didi

java - 如何创建以非 Activity 文本作为后缀的 SWT 文本字段?

转载 作者:行者123 更新时间:2023-12-01 19:40:49 24 4
gpt4 key购买 nike

我正在使用 Java 的 SWT 工具包创建带有文本字段输入的 GUI。这些输入字段需要输入数字并分配有单位。我正在尝试创建一种奇特的方法来将字段内的单位集成为文本的固定后缀,以便用户只能编辑数字部分。我还希望后缀显示为灰色,以便用户知道它已被禁用 - 如下所示:

A SWT text field with greyed suffix not accessible by the user

在搜索时,我看到了一些来自 Swing 的掩码格式化程序的解决方案,可能可以解决问题,但我有点希望 SWT 可能有一些默认设置。关于如何实现这项工作有什么建议吗?

该字段是矩阵的一部分,因此我不能简单地将单位添加到标题标签中。我想我可以在文本字段之后创建另一列,该列可以提供单位作为标签,但我想要更直观和美观的东西。

有什么建议吗?

最佳答案

一种选择是将TextLabel小部件分组在同一个组合中,并将Label上的文本设置为所需的后缀:

enter image description here

后缀左侧的区域是单行文本字段,可以编辑,后缀是禁用的标签

<小时/>
public class TextWithSuffixExample {

public class TextWithSuffix {

public TextWithSuffix(final Composite parent) {
// The border gives the appearance of a single component
final Composite baseComposite = new Composite(parent, SWT.BORDER);
baseComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
final GridLayout baseCompositeGridLayout = new GridLayout(2, false);
baseCompositeGridLayout.marginHeight = 0;
baseCompositeGridLayout.marginWidth = 0;
baseComposite.setLayout(baseCompositeGridLayout);

// You can set the background color and force it on
// the children (the Text and Label objects) to add
// to the illusion of a single component
baseComposite.setBackground(new Color(parent.getDisplay(), new RGB(255, 255, 255)));
baseComposite.setBackgroundMode(SWT.INHERIT_FORCE);

final Text text = new Text(baseComposite, SWT.SINGLE | SWT.RIGHT);
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

final Label label = new Label(baseComposite, SWT.NONE);
label.setEnabled(false);
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, true));
label.setText("kg/m^3");
}

}

final Display display;
final Shell shell;

public TextWithSuffixExample() {
display = new Display();
shell = new Shell(display);
shell.setLayout(new GridLayout());
shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

new TextWithSuffix(shell);
}

public void run() {
shell.setSize(200, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}

public static void main(final String[] args) {
new TextWithSuffixExample().run();
}

}

关于java - 如何创建以非 Activity 文本作为后缀的 SWT 文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55462398/

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