![](/img/trans.png)
[英]Cannot find symbol even though class (Font) & method (font) are declared?
[英]Unable to find object even though it is declared in same class
我目前正在学习如何在Java中构建GUI,并且遇到以下错误“找不到符号-类监听器”。
以下代码创建单选按钮,然后将它们与JLabel和操作侦听器配对。 但是,即使我将监听器作为button的参数提供,编译器仍在告诉我找不到监听器类。 有人知道为什么是这样吗?
在按钮方法的参数行上发生错误。 非常感谢您的协助。
private void build(Stadium stadium)
{
Listener listener = new Listener();
add(button("Front", listener));
add(button("Middle", listener));
add(button("Back", listener));
}
private JRadioButton button(String label, Listener listener)
{ JRadioButton button = new JRadioButton(label);
button.addActionListener(listener);
group.add(button);
return button; }
使用ActionListener
而不是Listener
,它们与GUI中的Listener
。
private void build(Stadium stadium)
{
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//Here you implement what you want your listener to execute on button click;
}
};
add(button("Front", listener));
add(button("Middle", listener));
add(button("Back", listener));
}
private JRadioButton button(String label, Listener listener)
{ JRadioButton button = new JRadioButton(label);
button.addActionListener(listener);
group.add(button);
return button; }
您需要一个ActionListener,而不是Listener。 您的类应该实现ActionListener( public class GUI implements ActionListener {
)
然后实现actionPerformed方法。
将此类的实例添加为侦听器。
您可能需要导入Listener
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.