[英]Can you put an action listener inside an action listener?
当我按下标有“一个”的按钮时,我的弹出窗口 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);
你能把一个动作监听器放在一个动作监听器中吗?
是的,您可以在 ActionListener 中创建一个组件并将一个 ActionListener 添加到新组件中。
那不是问题。
我的弹出窗口 window 冻结了,
while (pic == true)
问题是你有一个继续执行的while循环。
您的 ActionListener 不断创建组件并将它们添加到框架中,并且循环永远不会结束。
摆脱while循环!!!
此外,当您将组件添加到可见框架时,代码应该是:
panel.add(....);
panel.revalidate();
panel.repaint();
revalidate() 调用布局管理器,以便可以正确定位组件,而 repaint() 只是确保重新绘制所有组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.