ActionListener代码触发了两次

ActionListener code triggered twice

我制作了 jbutton 并向其添加了 ActionListner 但是 public void actionPerformed(ActionEvent e){} 中的代码执行 twice.It 打印测试两次,而不仅仅是 once.Here 是我的代码

JButton testbut=new JButton("Test");
 ListenForButton2 l=new ListenForButton2();
 testbut.addActionListener(l);
private class ListenForButton2 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == testbut){System.out.println("tEST");}       
    }
}

这是完整的代码,如果这不是 eneough.Looks 问题是向我的按钮添加两个动作侦听器。"if (testbut.getActionListeners().length<1)) testbut.addActionListener(l); "已修复 it.But 你能找到我添加两个动作的位置吗听众测试按钮

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Arc2D;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Game extends JFrame {

    JButton but1 = new JButton("MakeMoney");
    JButton button1 = new JButton("Current money"); 
    JButton testbut=new JButton("Make money");
    int money = 0;
    double currentMoney;
    String moneyString = "";
    String box1txt="Text for box one";  
    String[] boxtext={"Newspaper Delivery","Car wash","Pizza Delivery","Donut shop","Shrimp boat","Hockey team","Movie Studio","Bank","Oil Company"};
    String[] prices={"60.00","720.00","8,640.00","103,680.00","1,244,160.00","14,929,920.00","179,159,040.00","2.149 billion","25.798 billion"};
    public static void main(String[] args) {
        new Game();
    }

    public Game() {

        try {
            this.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Users/TPC/workspace/ProjectMoney/Resources/backgroundForApp.png")))));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        this.setLayout(new BorderLayout());

        button1.setContentAreaFilled(false);

        ListenForButton lforButton = new ListenForButton();
        but1.addActionListener(lforButton);

        JPanel thePanel = new JPanel();
        thePanel.add(button1);
        thePanel.add(but1);
        //thePanel.add(testbut);


        this.setSize(1042, 617);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Project Money");
        this.add(thePanel, BorderLayout.NORTH);
        this.add(new DrawStuff(), BorderLayout.CENTER);
        this.setResizable(true);
        this.setVisible(true);

        Sound sound1 = new Sound();
        String sound = "file:C:/Users/TPC/Downloads/sound.wav";
        sound1.playMusic(sound);

    }

    private class ListenForButton implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            if (e.getSource() == but1) {
                money += 10;
                moneyString = Integer.toString(money);
                button1.setText("$" + moneyString);

            }

        }
    }
private class ListenForButton2 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == testbut){System.out.println("tEST");}

    }



}

    private class DrawStuff extends JComponent {

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D graph2 = (Graphics2D) g;
            graph2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);










            Shape drawRoundRec = new RoundRectangle2D.Double(170,40,250,66,45, 45);

           //first num xpos,secound num ypost,third num sirina,fourht num duzina
int x1=170; int y1=40; int x2=250; int y2=66; int def=45;
            Shape drawRoundRec2=new RoundRectangle2D.Double(x1,(y1+80),x2,y2,def,def);
            Shape drawRoundRec3=new RoundRectangle2D.Double(x1,(y1+80+80),x2,y2,def,def);
            Shape drawRoundRec4=new RoundRectangle2D.Double(x1,(y1+80+80+80),x2,y2,def,def);
            Shape drawRoundRec5=new RoundRectangle2D.Double(x1,(y1+80+80+80+80),x2,y2,def,def);
            Shape drawRoundRect6=new RoundRectangle2D.Double(x1+330,40,250,66,def, def);
            Shape drawRoundRect7=new RoundRectangle2D.Double(x1+330,(y1+80),250,66,def, def);
            Shape drawRoundRect8=new RoundRectangle2D.Double(x1+330,(y1+80+80),250,66,def, def);   
            Shape drawRoundRect9=new RoundRectangle2D.Double(x1+330,(y1+80+80+80),250,66,def, def);
            Shape drawRoundRect10=new RoundRectangle2D.Double(x1+330,(y1+80+80+80+80),250,66,def, def);

            graph2.setPaint(Color.BLACK);



            graph2.setColor(Color.LIGHT_GRAY);

            graph2.fill(drawRoundRec); graph2.fill(drawRoundRec2);
            graph2.fill(drawRoundRec3); graph2.fill(drawRoundRec4); graph2.fill(drawRoundRec5); graph2.fill(drawRoundRect6);
            graph2.fill(drawRoundRect7); graph2.fill(drawRoundRect8); graph2.fill(drawRoundRect9); graph2.fill(drawRoundRect10);
            graph2.setPaint(Color.BLACK);

             graph2.draw(drawRoundRec2); graph2.draw(drawRoundRec3); graph2.draw(drawRoundRec4);
            graph2.draw(drawRoundRec5);  graph2.draw(drawRoundRect6); graph2.draw(drawRoundRect7);  graph2.draw(drawRoundRect8); 
            graph2.draw(drawRoundRect9); graph2.draw(drawRoundRect10);
            Font font=new Font("Serif",Font.PLAIN,30);
            g.setFont(font); g.drawString(box1txt,190,80); g.drawString(boxtext[0],190,150); g.drawString(boxtext[1],190,150+80); g.drawString(boxtext[2],190,150+90+70);
            g.drawString(boxtext[3],190,150+90+70+80);

            g.drawString(boxtext[4],520,80); g.drawString(boxtext[5],520,150); g.drawString(boxtext[6],520,150+80); g.drawString(boxtext[7],520,150+90+70);
            g.drawString(boxtext[8],520,150+90+70+80);



             g.drawString(prices[0],190,150+30); g.drawString(prices[1],190,150+80+30); g.drawString(prices[2],190,150+90+70+30);
            g.drawString(prices[3],190,150+90+70+80+30);

            g.drawString(prices[4],520,80+25); g.drawString(prices[5],520,150+30); g.drawString(prices[6],520,150+80+30); g.drawString(prices[7],520,150+90+70+30);
            g.drawString(prices[8],520,150+90+70+80+30);
            testbut.setLocation(180,70);
            Dimension d = new Dimension(100,40);
            testbut.setSize(d);
           ListenForButton2 l=new ListenForButton2();
           if (testbut.getActionListeners().length<1) testbut.addActionListener(l);







            this.add(testbut);
        }
    }
}

确保只添加一次动作侦听器。

您可以通过 testbut.getActionListeners().length 获得听众数。


更新 - 多次调用

在扩展 JComponent 的 DrawStuff class 的 paintComponent 中,您只需添加一次侦听器。与第一个听众相同。

区别在于,第一个是在构造函数中添加的。而JComponent的paintComponent方法可以多次调用

至于调用的方式和时间,this answer可能会对您有所帮助。