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 に答える