2

如何使用相同的 ActionPerformed 函数为不同的 JButton 提供不同的功能?

public class ToolBarExample extends JPanel implements ActionListener  {

public JTextPane pane;
public JMenuBar menuBar;
public JToolBar toolBar;

public JButton Aster;
public JButton Azaleas;
public JButton ChristFern;
public JButton JapBarberry;

public ToolBarExample() {

    toolBar = new JToolBar("Formatting", JToolBar.VERTICAL);

    this.Aster = new JButton(new ImageIcon("images/PlantIcons/[Blueprint]_Aster.png"));
    this.toolBar.add(Aster);    
    this.Aster.addActionListener(this);
    this.Azaleas = new JButton(new ImageIcon("images/PlantIcons/[Blueprint]_Azaleas.png"));
    this.toolBar.add(Azaleas);
    this.Azaleas.addActionListener(this);
    this.ChristFern = new JButton(new ImageIcon("images/PlantIcons/[Blueprint]_ChristmasFern.png"));
    this.toolBar.add(ChristFern);
    this.ChristFern.addActionListener(this);
    this.JapBarberry = new JButton(new ImageIcon("images/PlantIcons/[Blueprint]_JapaneseBarberry.png"));
    this.toolBar.add(JapBarberry);
    this.JapBarberry.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
        try {
            if(e.getSource() == Aster) {
                NativePlant plant1 = new NativePlant(4, 5, 600, "test", "images/[Blueprint]_Aster.png", 200, 600);
                Game.setNatives(plant1);
            }

            if(e.getSource() == Azaleas) {
                NativePlant plant1 = new NativePlant(4, 5, 600, "test", "images/[Blueprint]_Azaleas.png", 100, 600);
                Game.setNatives(plant1);
            }
            if(e.getSource() == ChristFern) {
                NativePlant plant1 = new NativePlant(4, 5, 600, "test", "images/[Blueprint]_ChristmasFern.png", 300, 600);
                Game.setNatives(plant1);
            }
            if(e.getSource() == JapBarberry) {
                NativePlant plant1 = new NativePlant(4, 5, 600, "test", "images/[Blueprint]_JapaneseBarberry.png", 400, 600);
                Game.setNatives(plant1);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

现在我正在尝试使用 e.getSource 找出正在按下的按钮,然后为每个按钮执行不同的操作。但是,现在唯一起作用的按钮是第一个按钮(Aster)。

有谁知道我做错了什么,或者我应该做什么?

4

2 回答 2

1

您可以使用每个按钮的 setActionCommand() 并从 actionPerformed 调用相应的 getActionCommand。在我看来,使用常量来定义动作将是一个好方法。一个小样本如下;

public class SwingTest extends JFrame implements ActionListener{

public SwingTest()
{
    JButton btnOne = new JButton("test one");
    btnOne.addActionListener(this);
    btnOne.setActionCommand("TestOne");
    JButton btnTwo = new JButton("test two");
    btnTwo.addActionListener(this);
    btnTwo.setActionCommand("TestTwo");

    this.setLayout(new FlowLayout());
    this.getContentPane().add(btnOne);
    this.getContentPane().add(btnTwo);
}

public static void main(String[] args) {
    SwingTest t = new SwingTest();
    t.pack();
    t.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {

    System.out.println(e.getActionCommand());
}
}
于 2012-11-20T02:47:31.367 回答
0

而不是让它成为

e.getSource() == Aster

将其更改为:

e.getSource().equals(Aster)

它看起来是一样的,但它会给你你正在寻找的结果......因为 .equals() 正在检查对象的状态,而 '==' 正在检查实际实例。

http://www.javabeat.net/qna/13-what-is-difference-between-equals-and-/

于 2012-11-20T03:11:25.477 回答