2012-11-20 1 views
2

동일한 ActionPerformed 함수를 사용하여 다른 JButton에 다른 기능을 부여하는 방법은 무엇입니까? JButtons에 다른 기능을 부여하는 방법은 무엇입니까?

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).

누구나 내가 뭘 잘못하고 있는지, 내가 뭘해야 하는지를 알고 있습니까?

+2

작품 (일부 포맷 후) 잘. 'if-else'를 사용 하겠지만 실제로는 ['Action' API] (http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html)를 대신 사용 하겠지만 저입니다. – MadProgrammer

답변

1

각 버튼의 setActionCommand()를 사용하여 actionPerformed에서 각각의 getActionCommand를 호출 할 수 있습니다. 행동을 정의하기 위해 상수를 사용하는 것이 제 생각에는 좋은 접근 방법이 될 것입니다. 다음과 같은 작은 샘플; 대신이에

e.getSource() == Aster 

변화가 할 수있는의

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()); 
} 
} 
+0

오타가 있습니다. 'btnOne.setActionCommand'가 두 번입니다. – durron597

+0

Thx 많이 있습니다. 게시물을 편집했습니다. – dinukadev

관련 문제