我正在寻找一种将变量或字符串或任何内容传递到 JButton 的匿名 actionlistener (或显式 actionlistener )中的方法。这是我所拥有的:
public class Tool {
...
public static void addDialog() {
JButton addButton = new JButton( "Add" );
JTextField entry = new JTextField( "Entry Text", 20 );
...
addButton.addActionListener( new ActionListener( ) {
public void actionPerformed( ActionEvent e )
{
System.out.println( entry.getText() );
}
});
...
}
}
现在我只是声明entry
成为一个全局变量,但我讨厌这种工作方式。有更好的选择吗?
- 创建一个类来实现
ActionListener
界面。
- 提供一个构造函数,该构造函数具有
JTextField
争论。
例子 -
class Foo implements ActionListener{
private final JTextField textField;
Foo(final JTextField textField){
super();
this.textField = textField;
}
.
.
.
}
Problem?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)