[英]Swing application freezes
I am pretty new to the AWT/Swing programming, so I do not see the problem. 我对AWT / Swing编程很陌生,所以看不到问题。 When the event is called, the window freezes even before removing the components and I have to close it manually.
调用该事件时,即使在删除组件之前,窗口也会冻结,我必须手动将其关闭。
So, what's wrong with this code? 那么,这段代码有什么问题呢?
@Override
public void actionPerformed(ActionEvent event)
{
if(((JButton)event.getSource()).getName() == "start")
{
for(Component c : QuizShow.frame.getContentPane().getComponents())
{
if(c.getName() == "wrapper")
{
final JPanel wrapper = (JPanel) c;
SwingUtilities.invokeLater
(
new Runnable()
{
public void run() {
wrapper.removeAll();
QuizPanel qp = new MainQuizPanel();
qp.setup();
wrapper.add(qp);
}
}
);
break;
}
}
}
System.out.println(event.getSource());
}
Edit: This is the qp.setup() void: 编辑:这是qp.setup()无效:
public void setup()
{
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 6; j++)
{
questions[i*6+j] = new JButton();
questions[i*6+j].setText(""+i*10);;
add(questions[i*6+j]);
}
}
}
This is the main(String[] args) class: 这是main(String [] args)类:
SwingUtilities.invokeLater
(
new Runnable()
{
public void run() {
frame = new QuizShowFrame("Quiz Show");
frame.setSize(800, 600);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100, 100);
frame.setVisible(true);
}
}
);
And the QuizShowFrame class: 还有QuizShowFrame类:
public class QuizShowFrame extends JFrame
{
private static final long serialVersionUID = 1L;
public QuizPanel introPanel = new Intro();
public JPanel p = new JPanel();
public QuizShowFrame(String name)
{
super(name);
this.setLayout(new BorderLayout());
Container c = this.getContentPane();
p.setBorder(new EmptyBorder(5,5,5,5));
p.setName("wrapper");
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
c.add(p);
p.add(introPanel);
try
{
introPanel.setup();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
And finally in the Intro class: 最后是Intro课程:
public void setup()
{
//...skipped JTextPanes...
JButton gameStart = new JButton("Start the quiz show");
gameStart.setName("start");
try {
gameStart.addActionListener(Listener.class.newInstance());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
add(gameStart);
}
I got it: 我知道了:
@Override
public void actionPerformed(final ActionEvent e)
{
if(((JButton)e.getSource()).getName().equals("start"))
{
for(final Component c : QuizShow.frame.getContentPane().getComponents())
{
if(c.getName().equals("wrapper"))
{
((JPanel) c).removeAll();
c.repaint();
QuizPanel qp = new MainQuizPanel();
((JPanel) c).add(qp);
qp.setup();
c.validate();
break;
}
}
}
}
This helped me. 这对我有帮助。 It seems the .validate() solved it.
看来.validate()解决了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.