你能把一个动作监听器放在一个动作监听器里面吗?

Can you put an action listener inside an action listener?

当我按下标记为“one”的按钮时,我的弹出窗口 window 冻结了,我认为这是因为我试图将一个带有动作侦听器的按钮放入另一个按钮的动作侦听器中。这可能吗?

//code...

    one = new JButton("Customize Race");
    one.setBounds(30,200,200,75);
    one.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed( ActionEvent e ) {
            one.setVisible(false);
            
            Boolean pic = true;
            
            String Player1 = "Player1";
           
            while (pic == true)
            {
                p1 = new JButton(Player1);
                p1.setBounds(50, 50, 200, 100); 
                p1.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed( ActionEvent e ) {
                        // code that will pull up menu to 
                           customize string value of Player1 
                    }
                });
                
                
                next1 = new JButton("Next =>");
                next1.setBounds(50, 375, 450, 50); 
                next1.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed( ActionEvent e ) {
                        Boolean pic = false;
                    }
                });
                
                
                panel.add(p1);
                panel.add(next1);
                
            }              
                p1.setVisible(false);
        }
    });
    panel.add(one);

    frame.setVisible(true);

Can you put an action listener inside an action listener?

是的,您可以在 ActionListener 中创建一个组件并将 ActionListener 添加到新组件。

不是这个问题。

my popup window freezes up,

while (pic == true)

问题是你有一个 while 循环继续执行。

您的 ActionListener 不断创建组件并将它们添加到框架中,循环永无止境。

去掉 while 循环!!!

此外,当您将组件添加到可见框架时,代码应为:

panel.add(....);
panel.revalidate();
panel.repaint();

revalidate() 调用布局管理器,以便组件可以正确定位,repaint() 只是确保所有组件都被重新绘制。