For loop (Java)如何让程序在按下Jbutton时只创建一个帧,而不管按下多少次?

For loop (Java)如何让程序在按下Jbutton时只创建一个帧,而不管按下多少次?,for-loop,jframe,actionlistener,For Loop,Jframe,Actionlistener,按下JButton后,如何阻止程序生成JFrames?我试图让它在第一次按下按钮后停止生成新的JFrames,但遗憾的是,这没有发生。任何帮助都将不胜感激。多谢各位 list.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent e) { for (int counter=0; counter<1; counter++){

按下JButton后,如何阻止程序生成JFrames?我试图让它在第一次按下按钮后停止生成新的JFrames,但遗憾的是,这没有发生。任何帮助都将不胜感激。多谢各位

list.addActionListener(new ActionListener() {
    public void actionPerformed (ActionEvent e) {
        for (int counter=0; counter<1; counter++){
                    if (counter<1){
                        Newbox fresh = new Newbox();
                            }
                                if (counter >1) {
                                break;
                    }
                }
            }
        });
list.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){

对于(int counter=0;counterTrx)添加布尔值

Boolean pressed = false;

list.addActionListener(new ActionListener() {
    public void actionPerformed (ActionEvent e)   {
        if (!pressed){
            Newbox fresh = new Newbox();
            pressed = true;
        }
    }
});

你可以使用一个布尔变量来阻止它创建一个新的框架

boolean pressed = false;
list.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
    for (int counter=0; counter<1; counter++){
                if (counter<1 && pressed == false){
                    Newbox fresh = new Newbox();
                    pressed = true;
                        }
                            if (counter >1) {
                            break;
                }
            }
        }
    });
boolean pressed=false;
addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){

对于(int counter=0;counter我尝试过使用布尔值,但这种方法效果更好,所以作为记录,这是解决这个问题的一种方法。谢谢大家的反馈

           list.addActionListener(new ActionListener() {
                    public void actionPerformed (ActionEvent e) {
                        Newbox fresh = new Newbox();
                            ((AbstractButton)e.getSource()).setEnabled(false);

                    } 
                });

注意:布尔值必须在方法之外,一个全局变量。我认为for循环可以删除,因为它无论如何只运行一次,我认为OP试图像这样解决他的问题。