0
1부터 5까지의 값을 갖는 5 개의 RadionButton이 있습니다. 내 선택에 따라 값을 인쇄하고 싶습니다.그룹에서 JRadionButton을 선택하고 int 값을 출력하십시오.
JRadioButton rOneStar = new JRadioButton();
rOneStar.setActionCommand(Integer.toString(1));
JRadioButton rTwoStars = new JRadioButton();
rTwoStars.setActionCommand(Integer.toString(2));
JRadioButton rThreeStar s= new JRadioButton();
rThreeStars.setActionCommand(Integer.toString(3));
JRadioButton rFourStars = new JRadioButton();
rFourStars.setActionCommand(Integer.toString(4));
JRadioButton rFiveStars = new JRadioButton();
rFiveStars.setActionCommand(Integer.toString(5));
// group stars
ButtonGroup starGroup = new ButtonGroup();
starGroup.add(rOneStar);
starGroup.add(rTwoStars);
starGroup.add(rThreeStars);
starGroup.add(rFourStars);
starGroup.add(rFiveStars);
이제 값을 출력하는 ActionListeners가 여러 개 있습니다. 그러나 제 의견으로는 너무 많은 코드입니다.
rOneStar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
e.getActionCommand();
System.out.println(starGroup.getSelection().getActionCommand());
}
});
rTwoStars.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
e.getActionCommand();
System.out.println(starGroup.getSelection().getActionCommand());
}
});
... and so on
아무에게도 내 코드를 단축 할 수있는 방법을 알려주세요.
당신의 도움에 감사드립니다! :) – cosmohorst