【问题标题】:How to create a SWT text field with inactive text as a suffix?如何创建以非活动文本为后缀的 SWT 文本字段?
【发布时间】:2019-04-01 19:38:39
【问题描述】:

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

在搜索时,我看到了一些带有来自 Swing 的掩码格式化程序的解决方案,这可能会奏效,但我有点希望 SWT 可能有一些默认设置。关于如何使这项工作的任何建议?

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

有什么建议吗?

【问题讨论】:

    标签: java text swt


    【解决方案1】:

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

    后缀左侧区域为单行文本字段,可编辑,后缀为禁用Label


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

    【讨论】:

    • 自定义 MaskFormatter 怎么样?自从我使用 SWT 已经很长时间了,但是 Nebula 有这个格式化程序可用
    • @LppEdd 抱歉,我不熟悉 MaskFormatter(或者说真的是一般的 Nebula),所以我不会有太多帮助......
    • 有趣...这可能有效,尽管乍一看它看起来像是一个单一的文本字段,所以我不确定你如何能够在不做一些事情的情况下实现不可变后缀额外的跑腿。
    • 你绝对需要额外的工作;)我的只是一个提示/好奇心。
    猜你喜欢
    • 2019-09-15
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 2012-09-03
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多