所以我试图为一个接口创建两个按钮,我已经尝试实现ActionListener来启用其中一个按钮来打印一个字符串,但是它给了我一个错误,它说“在BorderDemo类中没有实现actionlistener” “
我做错了什么?
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());
}
}
答案 0 :(得分:1)
看起来actionPerformed
已添加到Picture
课程而不是BorderDemo
。因此,如果将其移至BorderDemo
,则应解决上述错误。
错误的原因是BorderDemo
被声明为实现ActionListener
接口:
class BorderDemo implements ActionListener
但是,它没有实现它。添加actionPerformed
中定义的ActionListener
方法,即:
@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);
中使用getPrefferedSize()
实施Picture
设置框架尺寸,并在框架上调用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);
}
}
}