동일한 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).
누구나 내가 뭘 잘못하고 있는지, 내가 뭘해야 하는지를 알고 있습니까?
작품 (일부 포맷 후) 잘. 'if-else'를 사용 하겠지만 실제로는 ['Action' API] (http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html)를 대신 사용 하겠지만 저입니다. – MadProgrammer