public double giveTest()
{
int correct = 0;
int incorrect = 0;
JFrame frame = new JFrame("Test: " + stuName);
frame.setSize(700, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for(int x = 0; x < doneTest.length; x ++)
{
frame.setVisible(false);
int pressed;
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel question = new JLabel(doneTest[x][0]);
JButton button1 = new JButton();
button1.setText(doneTest[x][1]);
JButton button2 = new JButton();
button2.setText(doneTest[x][2]);
JButton button3 = new JButton();
button3.setText(doneTest[x][3]);
JButton button4 = new JButton();
button4.setText(doneTest[x][4]);
question.updateUI();
panel.add(question);
button1.updateUI();
panel.add(button1);
button2.updateUI();
panel.add(button2);
button3.updateUI();
panel.add(button3);
button4.updateUI();
panel.add(button4);
panel.revalidate();
panel.repaint();
frame.remove(panel);
frame.add(panel);
frame.revalidate();
frame.repaint();
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
while(!button1.getModel().isPressed() || !button2.getModel().isPressed() || !button3.getModel().isPressed() || !button4.getModel().isPressed())
{
if(button1.getModel().isPressed())
{
System.out.println("Bt1");
pressed = 1;
break;
}
if(button2.getModel().isPressed())
{
System.out.println("Bt2");
pressed = 2;
break;
}
if(button3.getModel().isPressed())
{
System.out.println("Bt3");
pressed = 3;
break;
}
if(button4.getModel().isPressed())
{
System.out.println("Bt4");
pressed = 4;
break;
}
}
button1.setVisible(false);
button2.setVisible(false);
button3.setVisible(false);
button4.setVisible(false);
question.setVisible(false);
}
return 999.99;
}
这是我使用JPanel / JFrame的全班。我的问题是每次进入下一个问题(for loop
中的下一次迭代)时,它都不会重新绘制按钮直到鼠标悬停,它也不会绘制新的JLabel。这是我的代码问题还是JPanel本身的问题?对不起,我的代码非常混乱,我一直试图找到解决这个问题并放弃清洁直到它完成。
谢谢你们的帮助。