java - 在 RTP GUI 中实现 Actionlistener

标签 java swing actionlistener java-2d ready

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

一些小评论:

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

  2. 您可以使用以下方法,而不是向框架添加窗口监听器以在关闭时退出应用程序:

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  3. 不要使用 setSize(544, 416); 设置帧大小,而是在 Picture 中实现 getPrefferedSize() 并调用 pack(); 在框架上。

  4. Picture类中,不要重写paint(),而是重写paintComponent()。另外,不要忘记在您的实现中调用 super.paintComponent()。请参阅Performing Custom Painting

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

  6. 正如上面评论中已经提到的,代码的可读性非常重要。请参阅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/

相关文章:

java - 如何在 Spring 中使用 applicationcontext.xml 文件访问属性文件字段?

Android - 检查一个 EditText 输入框,每次输入一个字符

java - JTextField无法在if语句java中使用输入

java - JodaTime:获取与 utc 相比具有偏移 xyz 的 DateTimeZone

java - 两个 JLabel 在鼠标悬停时相互复制背景和内容

java - 无法使用 iText PdfDiv 绝对位置

java - 将图像转换为按钮数组

java.lang.UnsupportedOperationException : Not supported yet

java - 使用 SwingWorker java 在线程池中等待任务

Java:如何将 "bundle"GUI 元素分组并干净地收听多个组?