GUI에서 작업 중이며 다른 버튼을 사용하여 다른 작업을 수행하려고합니다.여러 JButton에 여러 actionListener 추가
현재 각 버튼은 동일한 ActionListener로 연결됩니다. 내가 ActionEvent를 쓸
public class GUIController {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(3,3));
JLabel leight =new JLabel("8");
frame.getContentPane().add(leight);
JLabel lfive =new JLabel("0");
frame.getContentPane().add(lfive);
JLabel lthree =new JLabel("0");
frame.getContentPane().add(lthree);
JButton beight =new JButton("Jug 8");
frame.getContentPane().add(beight);
JButton bfive =new JButton("Jug 5");
frame.getContentPane().add(bfive);
JButton bthree =new JButton("Jug 3");
frame.getContentPane().add(bthree);
LISTN ccal = new LISTN (leight,lfive,lthree);
beight.addActionListener(ccal);
bfive.addActionListener(ccal);
bthree.addActionListener(ccal);
frame.pack();
frame.setVisible(true);
}
}
내 된 ActionListener 파일
public class JugPuzzleGUILISTN implements ActionListener {
JLabel leight;
JLabel lfive;
JLabel lthree;
JugPuzzleGUILISTN(JLabel leight,JLabel lfive, JLabel lthree){
this.leight = leight;
this.lfive = lfive;
this.lthree = lthree;
}
public void actionPerformed(ActionEvent e) {
}
}
}
뭐든지 내가 각 버튼은 자신의 기능을 갖도록 할 수있는 방법, 세 개의 버튼에 적용? 정말 고마워요!
'어떻게 각 버튼마다 고유 한 기능을 만들 수 있습니까? '- 각 버튼마다 다른 ActionListener를 추가하십시오. – camickr
단일 리스너를 사용하여'actionCommand' 또는'source' 속성을 사용할 수 있습니다. 당신은'clientProperty'를 사용할 수 있습니다; 각 버튼마다 별도의 리스너를 사용할 수 있습니다. 당신은'Action' API를 사용할 수 있습니다. 잘 정리 된 많은 아이디어들 – MadProgrammer