对于我的应用程序,我创建了两个分组为“选择”ButtonGroup
的JRadioButton
。我添加了一个名为“SelectionListener”的ActionListener
。当我检查是否使用选择了我的RadioButton
时,看起来我的选择没有传递到ActionListener
。将JRadioSelection传递给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
类。我收到一个错误,说它没有被解决。
如何将我的主要方法中的monthlyRadioButton
传递给我的SelectionListener
类?
为了将来的参考,你应该在你的问题中包含确切的错误信息。我读了整件事情,而且从来没有清楚你在这里有一个编译器错误。包括这些信息将帮助我们更快地回答您的问题。 –