[英]Unable to find object even though it is declared in same class
I'm currently learning how to build GUI's in Java and I'm encountering the following error "cannot find symbol - Class Listener". 我目前正在学习如何在Java中构建GUI,并且遇到以下错误“找不到符号-类监听器”。
The following code creates radio buttons and then pairs them with a JLabel and an action listener. 以下代码创建单选按钮,然后将它们与JLabel和操作侦听器配对。 However even though I provide listener as an argument to button, the compiler is telling me it cannot find the listener class.
但是,即使我将监听器作为button的参数提供,编译器仍在告诉我找不到监听器类。 Does anyone know why this is?
有人知道为什么是这样吗?
The error is occuring on the parameter line of the button method. 在按钮方法的参数行上发生错误。 Any assistance is greatly appreciated.
非常感谢您的协助。
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; }
Use ActionListener
not Listener
, their is nothing like Listener
in GUI. 使用
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; }
You need an ActionListener, not a Listener. 您需要一个ActionListener,而不是Listener。 Your class should implement ActionListener (
public class GUI implements ActionListener {
) 您的类应该实现ActionListener(
public class GUI implements ActionListener {
)
and then implement the actionPerformed method. 然后实现actionPerformed方法。
add the instance of this class as listener. 将此类的实例添加为侦听器。
您可能需要导入Listener
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.