对于我的应用程序,我创建了两个JRadioButton
分组为“选择” ButtonGroup
。我添加了一个ActionListener
名为“SelectionListener”。当我检查是否RadioButton
使用isSelected()
选择了 my 时,似乎我的选择没有传递给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());
}
选择监听器.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
班级。我收到一个错误,它没有得到解决。
如何将我的monthlyRadioButton
主要方法传递给我的SelectionListener
班级?