所以我试图为一个界面创建两个按钮,我尝试实现 ActionListener 来使其中一个按钮能够打印字符串,但它给了我一个错误,指出“actionlistener 没有在 BorderDemo 类中实现” ”
我做错了什么?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferStrategy;
class BorderDemo
implements ActionListener
{
public static void main (String[] args)
{
JFrame F = new JFrame("Buttons");
F.addWindowListener
(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}});
F.setSize(544,416);
JPanel pane = (JPanel) F.getContentPane();
pane.add(new Picture(),BorderLayout.CENTER);
pane.add(new JButton("Start"),BorderLayout.WEST);
pane.addActionListener(this);
pane.add(new JButton("Stop"),BorderLayout.EAST);
F.setVisible(true);
F.setResizable(false);
}
}
class Picture extends JComponent
{
public Picture ()
{
repaint();
}
public void paint (Graphics g)
{
g.setColor(Color.yellow);
g.fillOval(getWidth()/4,getHeight()/4,
getWidth()/2,getHeight()/3);
g.setColor(Color.black);
g.fillOval(getWidth()/2,getHeight()/4,
getWidth()/17,getHeight()/3);
g.setColor(Color.black);
g.fillOval(getWidth()/3,getHeight()/4,
getWidth()/17,getHeight()/3);
g.setColor(Color.white);
g.fillOval(getWidth()/5,getHeight()/5,
getWidth()/5,getHeight()/7);
g.setColor(Color.white);
g.fillOval(getWidth()/3,getHeight()/8,
getWidth()/5,getHeight()/7);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Item clicked: "+e.getActionCommand());
}
}
最佳答案
看起来 actionPerformed
已添加到 Picture
类而不是 BorderDemo
中。因此,如果您将其移至 BorderDemo
中,它应该可以解决上述错误。
错误的原因是 BorderDemo
被声明为实现 ActionListener
接口(interface):
class BorderDemo implements ActionListener
但是,它并没有实现。添加ActionListener
中定义的actionPerformed
方法,即:
@Override
public void actionPerformed(ActionEvent arg0) {
}
看看Implementing an Interface教程。另请参阅How to Write an Action Listener 。
一些小评论:
您可能希望向按钮添加操作监听器,而不是
JPanel
。JPanel
不接受操作监听器。您可以使用以下方法,而不是向框架添加窗口监听器以在关闭时退出应用程序:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
不要使用
setSize(544, 416);
设置帧大小,而是在Picture
中实现getPrefferedSize()
并调用pack();
在框架上。在
Picture
类中,不要重写paint()
,而是重写paintComponent()
。另外,不要忘记在您的实现中调用super.paintComponent()
。请参阅Performing Custom Painting 。请务必使用
invokeLater()
在事件调度线程上创建 UI 组件。请参阅Initial Threads 。正如上面评论中已经提到的,代码的可读性非常重要。请参阅Code Conventions for the Java Programming Language了解详细信息和示例。
编辑:基于在 RTP 1.7 中运行正常的已发布代码的示例
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BorderDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
public static void createAndShowUI() {
JFrame frame = new JFrame("Buttons");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton startButton = new JButton("Start");
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Start clicked");
}
});
JButton stopButton = new JButton("Stop");
stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Stop clicked");
}
});
Container contentPane = frame.getContentPane();
contentPane.add(new Picture(), BorderLayout.CENTER);
contentPane.add(startButton, BorderLayout.WEST);
contentPane.add(stopButton, BorderLayout.EAST);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
}
static class Picture extends JComponent {
public Picture() {
}
public Dimension getPreferredSize() {
return new Dimension(544, 416);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.yellow);
g.fillOval(getWidth() / 4, getHeight() / 4, getWidth() / 2,
getHeight() / 3);
g.setColor(Color.black);
g.fillOval(getWidth() / 2, getHeight() / 4, getWidth() / 17,
getHeight() / 3);
g.setColor(Color.black);
g.fillOval(getWidth() / 3, getHeight() / 4, getWidth() / 17,
getHeight() / 3);
g.setColor(Color.white);
g.fillOval(getWidth() / 5, getHeight() / 5, getWidth() / 5,
getHeight() / 7);
g.setColor(Color.white);
g.fillOval(getWidth() / 3, getHeight() / 8, getWidth() / 5,
getHeight() / 7);
}
}
}
关于java - 在 RTP GUI 中实现 Actionlistener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15995840/