我正在寻找一种方法来将变量、字符串或其他内容传递到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
为全局变量,但我不喜欢这种方法使其工作。有更好的替代方案吗?