-1

在java中,我有一个面板和两个按钮,分别命名为b1和b2。当我复制面板并将其放在同一帧中时,按钮名称变为 b3 和 b4 但我在 b1 中编写的代码不会转移到 b3 ?我该怎么做,即当创建面板的副本时,b1 中的代码应该在 b3 中实现,也可以假设我在 b1 actionperformed 代码中有“b2.doClick()”变成“b4.doClick” ()' 在我复制面板时执行 b3 ActionPerformed 吗?我正在使用 netbeans(如果这有帮助)

4

1 回答 1

1

获得b1b3做同样事情的一种方法是让每个人都采取同样的行动

JButton b1 = new JButton(new SomeAction());
JButton b3 = new JButton(new SomeAction());

class SomeAction extends AbstractAction {
    public void actionPerformed(ActionEvent e) {
        // do something
        // call some other action
    }
}
于 2012-09-12T12:57:37.067 回答