在RTP GUI中实现Actionlistener

时间:2013-04-14 04:35:40

标签: java swing actionlistener java-2d ready

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

1 个答案:

答案 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

一些小评论:

  1. 您可能希望向按钮添加动作侦听器,而不是JPanelJPanel不接受动作侦听器。

  2. 您可以使用:

    ,而不是将窗口侦听器添加到框架以在关闭时退出应用程序

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  3. 而不是在setSize(544, 416);中使用getPrefferedSize()实施Picture设置框架尺寸,并在框架上调用pack();

  4. Picture课程中,请勿覆盖paint(),覆盖paintComponent()。另外,不要忘记在实施中致电super.paintComponent()。见Performing Custom Painting

  5. 请务必使用invokeLater()事件调度线程上创建UI组件。见Initial Threads

  6. 正如上面评论中已经提到的,代码可读性非常重要。有关详细信息和示例,请参阅Code Conventions for the Java Programming Language

  7. 编辑:基于在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);
            }
        }
    }