getActionCommand()vs getSource()

时间:2015-08-02 01:37:08

标签: java swing event-handling jbutton action

我在getActionCommand个对象上调用ActionEvent来检索来自某些JButton的信息的时间最长,但随着我的程序变得越来越复杂,我想发送几个信息通过setActionCommand,即有一个像" r3"向Action Listener表示我想 r JFrame内的面板中移出 3 rd按钮。最后,我厌倦了解析字符串并提取我想要使用的信息,而是开始使用getSource。 (我想知道哪一个更好用于检索信息)

另外,我创建了一个名为JButton的{​​{1}}子类,它有两个实例字段:一个OperationButton ID和一个int op(Operation是一个自定义枚举类型,其值为ADD,REMOVE,SWITCH等)。我想知道以下方法是否比仅使用Operation更有效/更好的做法,或者是否有第三种方法来处理我尚未想到的事件。

getActionCommand

(OperationButtons是我程序中的唯一按钮)

同样,我想要检索信息而不必设置public void actionPerformed(ActionEvent e) { OperationButton opButton = (OperationButton) e.getSource(); int ID = opButton.getID(); Operation op = opButton.getOperation(); switch (op) { case ADD: //adds a custom panel to frame break; case REMOVE: //removes a button and a custom panel with the specified ID break; case SWITCH: //highlights a button with the specified ID and //displays a custom panel with the specified ID //... } } 的动作命令,但我不完全确定这是否是正确的方法。此外,这种方法对于我可能希望发送超过2条信息的未来程序是否可行?

0 个答案:

没有答案
相关问题