我在一个使用 git 的项目上工作,其中多台机器的编码可能不同。
如果我设置这个:
private JButton translationButton1 = new JButton("←");
translationButton1.addActionListener(this);
然后我设置监听器:
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
我知道可以通过使用 e.getActionCommand()
获得“←”,但我担心如果有人没有与我相同的编码(例如 Cp1252),我不确定会得到“←”。
可以在 actionPerformed 方法
中获取按钮的名称 (if(???.equals("translationButton1")
) ? (我不知道我不想使用匿名内部类型
,因为我有几个要设置的 actionListener)
谢谢
最佳答案
没有,但是你可以设置一个 Action 命令:
translationButton1.setActionCommand("translationButton1");
然后检查命令:
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand.equals("translationButton1") {
// do something
}
}
关于java - 获取按钮的变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16302767/