【问题标题】:Java SWT create listener to change text of a label and return dataJava SWT 创建侦听器以更改标签的文本并返回数据
【发布时间】:2016-09-16 16:29:34
【问题描述】:

我的问题如下。我想这是一个相当容易的问题。然而,在谷歌上花了几个小时搜索解决方案后,我仍然没有答案。

我有一个函数createGui,我给它一个字符串变量text。此函数创建一个 GUI,用户可以在其中单击按钮。每当他单击按钮时,我都想修改变量text。我还想将标签label 设置为text 的值。最后,我想返回这么修改的变量text

你能告诉我,如何实现我的目标吗?

 public String createGui(String text)
  {
    Display display = new Display();
    Shell shell = new Shell( display );
    shell.setLayout( new GridLayout( 1, false ) );

    Label label = new Label( shell, SWT.NONE );
    label.setText( "abc" );

    Button b = new Button( shell, SWT.CHECK );
    b.setText( "check" );

    b.addSelectionListener( new SelectionAdapter()
    {
      @Override
      public void widgetSelected( SelectionEvent e )
      {
        // modify text

        // change label to text

      }

    } );

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

    return text;
  }

【问题讨论】:

    标签: java events swt listener


    【解决方案1】:

    您不能将匿名内部类的值返回给包含方法的调用者。

    但是,您可以传入一个回调并在完成后调用此回调的方法:

    public void createGui(String text, Callback callback)
    {
        [...]
    
        b.addListener(SWT.Selection, (e) -> {
            String modifiedText = // Manipulate the text
            label.setText(modifiedText);
    
            callback.onChange(modifiedText);
        });
    
        [...]
    }
    
    private static interface Callback
    {
        void onChange(String newValue); 
    }
    
    public static void main(String[] args)
    {
        createGui("InitialText", (s) -> {
            // Do something with the string here.
        });
    }
    

    这是Java8。这是 Java 7 版本:

    public void createGui(String text, final Callback callback)
    {
        [...]
    
        b.addListener(SWT.Selection, new Listener()
        {
            @Override
            public void handleEvent(Event event)
            {
                String modifiedText = // Manipulate the text
                // label.setText(modifiedText);
    
                callback.onChange(modifiedText);
            }
        });
    
        [...]
    }
    
    private interface Callback
    {
        void onChange(String newValue);
    }
    
    public static void main(String[] args)
    {
        createGui("InitialText", new Callback() 
        {
            @Override
            void onChange(String newValue)
            {
                // Do something with the string here.
            }
        });
    }
    

    【讨论】:

    • 感谢您的帮助。事实证明,我使用的是 Java 7,但我不知道这个箭头是什么意思。但是,我使用了一种我认为与您相似的方法。我为我的 GUI 定义了一个新类,我想更改的每个变量都定义为类变量。所以我可以通过匿名内部类访问它们。还是谢谢你!
    • @Skyy2010 如果有用的话,我可以添加我的答案的 Java 7 版本。
    • 是的,我很想知道如何使用 Java 7 做到这一点。谢谢!
    • @Skyy2010 我添加了 Java 7 版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 2014-01-14
    • 2010-10-04
    • 2012-12-19
    • 1970-01-01
    相关资源
    最近更新 更多