无法转换为 (Java swing ActionListener)
cannot be cast to (Java swing ActionListener)
我犯了一个我无法修复的错误。
代码:
public class Learn extends HelloActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Hello");
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLayout(new BorderLayout());
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new GridLayout(2, 2));
HelloActionListener listen = new HelloActionListener();
JButton b1 = new JButton("TOP");
b1.addActionListener((ActionListener) listen);
JButton b2 = new JButton("LEFT");
JButton b3 = new JButton("RIGHT");
JButton b4 = new JButton("BOTTOM");
JButton b5 = new JButton("1");
JButton b6 = new JButton("2");
JButton b7 = new JButton("3");
JButton b8 = new JButton("4");
buttonsPanel.add(b5);
buttonsPanel.add(b6);
buttonsPanel.add(b7);
buttonsPanel.add(b8);
frame.add(b1, BorderLayout.PAGE_START);
frame.add(b2, BorderLayout.LINE_START);
frame.add(b3, BorderLayout.LINE_END);
frame.add(b4, BorderLayout.PAGE_END);
frame.add(buttonsPanel, BorderLayout.CENTER);
frame.pack();
}
}
和
public class HelloActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Hello Folks");
}
}
错误:
Exception in thread "main" java.lang.ClassCastException:
learn.HelloActionListener cannot be cast to
java.awt.event.ActionListener at learn.Learn.main(Learn.java:35)
有人知道如何解决这个问题吗?
将侦听器直接添加到按钮会更容易一些。
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello Folks");
}
});
class "Learn" 不应扩展 ActionListener。它与成为 ActionListener 无关。它只是一个创建 GUI 的 class。它不需要扩展任何内容。
无需将 "HelloActionListener" 对象转换为 ActionListener。
只需使用:
b1.addActionListener( listen );
我犯了一个我无法修复的错误。
代码:
public class Learn extends HelloActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Hello");
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLayout(new BorderLayout());
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new GridLayout(2, 2));
HelloActionListener listen = new HelloActionListener();
JButton b1 = new JButton("TOP");
b1.addActionListener((ActionListener) listen);
JButton b2 = new JButton("LEFT");
JButton b3 = new JButton("RIGHT");
JButton b4 = new JButton("BOTTOM");
JButton b5 = new JButton("1");
JButton b6 = new JButton("2");
JButton b7 = new JButton("3");
JButton b8 = new JButton("4");
buttonsPanel.add(b5);
buttonsPanel.add(b6);
buttonsPanel.add(b7);
buttonsPanel.add(b8);
frame.add(b1, BorderLayout.PAGE_START);
frame.add(b2, BorderLayout.LINE_START);
frame.add(b3, BorderLayout.LINE_END);
frame.add(b4, BorderLayout.PAGE_END);
frame.add(buttonsPanel, BorderLayout.CENTER);
frame.pack();
}
}
和
public class HelloActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Hello Folks");
}
}
错误:
Exception in thread "main" java.lang.ClassCastException: learn.HelloActionListener cannot be cast to java.awt.event.ActionListener at learn.Learn.main(Learn.java:35)
有人知道如何解决这个问题吗?
将侦听器直接添加到按钮会更容易一些。
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello Folks");
}
});
class "Learn" 不应扩展 ActionListener。它与成为 ActionListener 无关。它只是一个创建 GUI 的 class。它不需要扩展任何内容。
无需将 "HelloActionListener" 对象转换为 ActionListener。
只需使用:
b1.addActionListener( listen );