java - 如何使用 SWT 组合按钮和文本字段

标签 java swt textfield

如何使用 SWT 组合按钮和文本字段,并为按钮提供自定义图标。

许多人建议使用 Combo,但它提供了下拉选项,但我不希望它成为下拉图标。有人可以在这里提供帮助并提出实现策略吗?

最佳答案

如果我正确理解您的问题,那么最好的方法是使用 CompositeTextLabel 对象进行分组。

Text 对象显然是您的文本字段,通过 Label 您可以设置图像以提供漂亮的无边框按钮。

enter image description here

根据您的需要,可以轻松扩展以允许多行文本区域,但我将坚持使用单行作为示例

public class TextWithButtonExample {

    /**
     * Simple class to accomplish what you want by wrapping
     * the Text and Label objects with a Composite.
     */
    public class TextWithButton {

        public TextWithButton(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);
            text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

            // Get whatever image you want from wherever
            // (in this case the web so it can be run easily!)
            Image buttonImage;
            try {
                buttonImage = ImageDescriptor.createFromURL(new URL("http://eclipse-icons.i24.cc/ovr16/clear.gif")).createImage();
            } catch (final MalformedURLException e) {
                buttonImage = null;
            }

            final Label button = new Label(baseComposite, SWT.NONE);
            button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
            button.setImage(buttonImage);
            button.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseUp(final MouseEvent e) {
                    // Do whatever you want when the 'button' is clicked
                    text.setText("");
                }
            });
        }

    }

    final Display display;
    final Shell shell;

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

        new TextWithButton(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 TextWithButtonExample().run();
    }

}

关于java - 如何使用 SWT 组合按钮和文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41459920/

相关文章:

java - mysql-connector-java 升级到 8.0.11 会更改从数据库中检索到的所有日期值

swift - Swift 上的文本字段第一响应者到下一个响应者

java - 解压 Android 中的文件夹及其子文件夹

java - 处理包含的异常

java - 在应用程序运行时,在 Java SWT 应用程序中添加/删除按钮。

java - 如何将包含换行符的字符串添加到 swt 列表

java - 为什么 SWT Composite 有时需要调用 resize() 才能正确布局?

java - 带有 TextFormatter 和/或 UnaryOperator 的 JavaFX 8 中用于整数的数字文本字段

javascript - 选择框更改时的动态文本字段

java - Spring:检查异常的自动回滚