對於我的應用程序,我創建了兩個分組爲「選擇」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
類?
爲了將來的參考,你應該在你的問題中包含確切的錯誤信息。我讀了整件事情,而且從來沒有清楚你在這裏有一個編譯器錯誤。包括這些信息將幫助我們更快地回答您的問題。 –