所以我试图为一个界面创建两个按钮,我尝试实现 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());
}
}