私のアプリケーションではJRadioButton、「選択」にグループ化された2つを作成しましたButtonGroup。ActionListener「SelectionListener」という名前を追加しました。RadioButtonを使用して自分が選択されているかどうかを確認isSelected()すると、選択がに渡されていないようActionListenerです。
public static void main(String[] args) {
JRadioButton monthlyRadioButton = new JRadioButton("Monthly Payment", true);
JRadioButton loanAmountButton = new JRadioButton("Loan Amount");
ButtonGroup selection = new ButtonGroup();
selection.add(monthlyRadioButton);
selection.add(loanAmountButton);
monthlyRadioButton.addActionListener(new SelectionListener());
loanAmountButton.addActionListener(new SelectionListener());
}
SelectionListener.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class SelectionListener implements ActionListener {
public void actionPerformed(ActionEvent event){
if(event.getSource() == monthlyRadioButton)
System.out.println("You clicked Monthly");
else
System.out.println("You clicked Loan Amount");
}
}
パラメータがクラスmonthlyRadioButtonに渡されていません。SelectionListener解決されていないというエラーが表示されます。
monthlyRadioButtonmainメソッドのをSelectionListenerクラスに渡すにはどうすればよいですか?