java—如何使用swt组合按钮和文本字段

gj3fmq9x  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(349)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

三年前关门了。
改进这个问题
如何使用swt组合按钮和文本字段,并为按钮提供自定义图标。
许多人建议使用组合,但它提供了下拉选项,但我不希望它是一个下拉图标。有人能帮上忙并提出一个实施策略吗?

piv4azn7

piv4azn71#

如果我正确理解你的问题,那么最好的方法就是使用 Composite 到组 Text 以及 Label 物体。
这个 Text 对象显然是您的文本字段,并且 Label 你可以设置一个图像给一个漂亮的无边框按钮。

根据您的需要,这可以很容易地扩展为允许多行文本区域,但我将坚持使用一行作为示例

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

}

相关问题