Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 每次单击打开双倍数量的新帧_Java_Frames - Fatal编程技术网

Java 每次单击打开双倍数量的新帧

Java 每次单击打开双倍数量的新帧,java,frames,Java,Frames,有一个小问题,一些代码,我写了一些尝试。我做了一个只有一个按钮的框架。当我点击这个按钮时,一个新的框架会打开,它应该会打开。我关闭新的框架,然后再次点击按钮,看看它是否仍然有效。问题从这里开始,它不是打开一个新的帧,而是打开两个新帧。我第三次点击它会打开4帧,以此类推。我尝试了很多东西,但遗憾的是,我似乎找不到它打开更多帧的原因。请帮忙 package budget; import java.awt.event.*; import javax.swing.*; public class GU

有一个小问题,一些代码,我写了一些尝试。我做了一个只有一个按钮的框架。当我点击这个按钮时,一个新的框架会打开,它应该会打开。我关闭新的框架,然后再次点击按钮,看看它是否仍然有效。问题从这里开始,它不是打开一个新的帧,而是打开两个新帧。我第三次点击它会打开4帧,以此类推。我尝试了很多东西,但遗憾的是,我似乎找不到它打开更多帧的原因。请帮忙

package budget;

import java.awt.event.*;
import javax.swing.*;

public class GUI extends JFrame {

    String labelPrefix;
    JButton button;
    JButton button2;
    JLabel label;

    public static void main(String[] args) {
        JFrame f = new GUI();
        f.setExtendedState(f.MAXIMIZED_BOTH);
        f.setVisible(true);
    }

    public GUI() {
        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
        p.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        button = new JButton("Click Me");
        label = new JLabel(labelPrefix);
        p.add(button);
        this.setTitle("Try");
        getContentPane().add(p);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        button.addActionListener(new MyActionListener());
    }

    class MyActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            button.addActionListener(this);
            labelPrefix = "Try";
            JFrame f2 = new GUI(label, labelPrefix);
            f2.setExtendedState(f2.MAXIMIZED_BOTH);
            f2.setVisible(true);

        }
    }

    public GUI(JLabel label, String labelPrefix) {
        JPanel p2 = new JPanel();
        button2 = new JButton("Close");
        p2.add(label);
        p2.add(button2);
        this.setTitle("Try");
        getContentPane().add(p2);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        pack();
        button2.addActionListener(new MyActionListener2());
    }

    class MyActionListener2 implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            button2.addActionListener(this);
            dispose();
        }
    }
}

显然,问题在于:

button.addActionListener(this);
每次单击该按钮时,它都会为该按钮添加一次侦听器


只要删除该行,错误就会消失。一旦一个监听器被添加到一个按钮上,它就会停留在那里。触发后它不会被“消耗”。

检查
MyActionListener
actionPerformed
中的第一行,该行说明:

button.addActionListener(this);
这条线应该拆下

一个新的框架打开了,它应该“求同存异”。看见