java - 在 Java 中为 CustomButton 创建 ActionEvent 对象

标签 java swing action awt

对于硬件作业,我们应该创建一个自定义按钮来熟悉 swing 和响应事件。我们还打算让这个按钮成为一个事件源,这让我很困惑。我有一个 ArrayList 来跟踪将注册以监听我的 CustomButton 的监听器。我感到困惑的是如何通知听众。我的老师暗示我尝试做一个通知和重写 actionPerformed ,但后来我不确定如何创建一个 ActionEvent 对象查看构造函数文档。来源、id、字符串都让我困惑。任何帮助,将不胜感激。

代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;

public class CustomButton 
{
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                CustomButtonFrame frame = new CustomButtonFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }

    public void addActionListener(ActionListener al)
    {
        listenerList.add(al);
    }

    public void removeActionListener(ActionListener al)
    {
        listenerList.remove(al);
    }

    public void actionPerformed(ActionEvent e) 
    {
        System.out.println("Button Clicked!");
    }

    private void notifyListeners()
    {
        ActionEvent event = new ActionEvent(CONFUSED HERE!!!!;
        for (ActionListener action : listenerList) {
            action.actionPerfomed(event);
        }
    }

    List<ActionListener> listenerList = new ArrayList<ActionListener>();
}

class CustomButtonFrame extends JFrame
{
    // constructor for CustomButtonFrame
    public CustomButtonFrame()
    {
        setTitle("Custom Button");
        CustomButtonSetup buttonSetup = new CustomButtonSetup();
        this.add(buttonSetup);
        this.pack();
    }
}

class CustomButtonSetup extends JComponent 
{
    public CustomButtonSetup()
    {
        ButtonAction buttonClicked = new ButtonAction();
        this.addMouseListener(buttonClicked);
    }

    // because frame includes borders and insets, use this method
    public Dimension getPreferredSize()
    {
        return new Dimension(200, 200);
    }

    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;

        // first triangle coords
        int x[] = new int[TRIANGLE_SIDES];
        int y[] = new int[TRIANGLE_SIDES];
        x[0] = 0;   y[0] = 0;
        x[1] = 200; y[1] = 0;
        x[2] = 0;   y[2] = 200;
        Polygon firstTriangle = new Polygon(x, y, TRIANGLE_SIDES);

        // second triangle coords
        x[0] = 0;   y[0] = 200;     
        x[1] = 200; y[1] = 200;
        x[2] = 200; y[2] = 0;
        Polygon secondTriangle = new Polygon(x, y, TRIANGLE_SIDES);

        g2.drawPolygon(firstTriangle);
        g2.setColor(firstColor);
        g2.fillPolygon(firstTriangle);

        g2.drawPolygon(secondTriangle);
        g2.setColor(secondColor);
        g2.fillPolygon(secondTriangle);

        // draw rectangle 10 pixels off border
        int s1[] = new int[RECT_SIDES];
        int s2[] = new int[RECT_SIDES];
        s1[0] = 5;    s2[0] = 5;
        s1[1] = 195;  s2[1] = 5;
        s1[2] = 195;  s2[2] = 195;
        s1[3] = 5;    s2[3] = 195;
        Polygon rectangle = new Polygon(s1, s2, RECT_SIDES);
        g2.drawPolygon(rectangle);
        g2.setColor(thirdColor);
        g2.fillPolygon(rectangle);  
    }

    private class ButtonAction implements MouseListener {
        public void mousePressed(MouseEvent e)
        {
            System.out.println("Click!");
            firstColor = Color.GRAY;
            secondColor = Color.WHITE;

            repaint();
        }

        public void mouseReleased(MouseEvent e)
        {
            System.out.println("Released!");
            firstColor = Color.WHITE;
            secondColor = Color.GRAY;
            repaint();
        }

        public void mouseEntered(MouseEvent e)
        {}

        public void mouseExited(MouseEvent e)
        {}

        public void mouseClicked(MouseEvent e)
        {}
    }

    public static final int TRIANGLE_SIDES = 3;
    public static final int RECT_SIDES = 4;
    private Color firstColor = Color.WHITE;
    private Color secondColor = Color.DARK_GRAY;
    private Color thirdColor = Color.LIGHT_GRAY;
}

最佳答案

阅读有关 ActionEvent 的文档。有一节是关于它的构造函数的,请阅读每个参数的含义。

对于您的情况,适用的代码将如下所示:

int uniqueId = System.currentTimeMillis().intValue();
String commandName = ""; //it can be like "show" or "hide" or whatever else; 
                         //you can get this string with getActionCommand() method
                         //and make some actions based on its value
                         //... but you don't need it now
ActionEvent event = new ActionEvent(this, uniqueId, commandName);

关于java - 在 Java 中为 CustomButton 创建 ActionEvent 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2919828/

相关文章:

java - 添加 xml 验证文件后没有为操作定义结果

java - 从 panel2 中的按钮添加到 panel1 中的文本字段

java - 有没有办法检查两个对象在 Java 中是否有别名?

java - JPA 对嵌套实体的唯一约束

Java Swing 使用箭头按钮发送导航命令

安卓 : Cancel Notification after click on Action (Like Call)

java - append 到文件时如何关闭 JavaFX FileChooser 的覆盖警告?

java - ServerSocketChannel.open() 从未返回

java - 检查 JTextField 的内容,然后存储在 String 中以便从另一个类访问

java - JTextfield字体和属性问题