Java JOptionPane似乎使JPanel消失了
我目前正在开发一个测验程序,如果所有问题都没有得到回答,就会出现一个JOptionpane确认对话框,询问用户是否要继续。起初,一切正常,然后我决定在整个程序中使用paintComponent()方法。在这个实现之后,如果这个JOptionpane弹出,包含的Jpanel将消失(将所有显示的问题都带走),那么只有背景仍然可见。我整天都在做这件事。以下是出现问题的代码:Java JOptionPane似乎使JPanel消失了,java,swing,paintcomponent,joptionpane,Java,Swing,Paintcomponent,Joptionpane,我目前正在开发一个测验程序,如果所有问题都没有得到回答,就会出现一个JOptionpane确认对话框,询问用户是否要继续。起初,一切正常,然后我决定在整个程序中使用paintComponent()方法。在这个实现之后,如果这个JOptionpane弹出,包含的Jpanel将消失(将所有显示的问题都带走),那么只有背景仍然可见。我整天都在做这件事。以下是出现问题的代码: /* * To change this license header, choose License Headers in P
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package operator;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.swing.JOptionPane;
import static operator.Rules.examTimer;
import static operator.Welcome.image;
/**
*
* @author Hendy
*/
public class QuestionExams extends javax.swing.JPanel {
QuestionCall on = new QuestionCall();
int sBtn,nums = on.number();
/**
* Creates new form QuestionExams
*/
public QuestionExams() {
randomQuestion();
initComponents();
if (control == 0)
previousButton.setEnabled(false);
group.add(a);
group.add(b);
group.add(c);
group.add(d);
setSelected();
if (counter>=(maximum()-1) && control >= (maximum()-1))
{nextButton.setText("Finish");
finish.setVisible(false);}
if (counter== maximum() && control == maximum())
{
determinant();
}
}
public void determinant(){
if (checkAdd()<maximum())
{
{
sBtn = JOptionPane.showConfirmDialog(null, " Do you want to finish the exam?\n "
+ "Some questions are still unanswered",
"Proceed?",
JOptionPane.YES_NO_OPTION);
if (sBtn == JOptionPane.YES_OPTION)
{ wrongsAdd();
nextPage();
System.out.println("this shows the question key:"+wrongs.get(0));
System.out.println("this shows the question number:"+wrongNos.get(0));}
else
{
on.numberBack();
selection();
--control;
--key;
removeAll();
setLayout(new FlowLayout());
setLayout( new BorderLayout() );
invalidate();
QuestionExams oan = new QuestionExams();
add(oan,BorderLayout.NORTH );
revalidate();
System.out.println("counter:"+counter +" and "+"control:" +control);
}
}
}
else
{
wrongsAdd();
nextPage();
}
}
public void paintComponent(Graphics g)
{
g.drawImage(image, 0, 0, null);
repaint();
}
@SuppressWarnings("unchecked")
private void initComponents() {
group = new javax.swing.ButtonGroup();
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
examTiming = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
nameQ = new javax.swing.JTextArea();
jPanel4 = new javax.swing.JPanel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
a = new javax.swing.JRadioButton();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
b = new javax.swing.JRadioButton();
c = new javax.swing.JRadioButton();
d = new javax.swing.JRadioButton();
jScrollPane1 = new javax.swing.JScrollPane();
optA = new javax.swing.JTextArea();
jScrollPane3 = new javax.swing.JScrollPane();
optB = new javax.swing.JTextArea();
jScrollPane4 = new javax.swing.JScrollPane();
optC = new javax.swing.JTextArea();
jScrollPane5 = new javax.swing.JScrollPane();
optD = new javax.swing.JTextArea();
quitButton = new javax.swing.JButton();
previousButton = new javax.swing.JButton();
nextButton = new javax.swing.JButton();
finish = new javax.swing.JButton();
private void finishActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
removeAll();
setLayout(new FlowLayout());
setLayout( new BorderLayout() );
invalidate();
AnswerCheck an = new AnswerCheck();
add(an, BorderLayout.NORTH );
revalidate();
wrongsAdd();
}
private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
{
selection();
if(control==counter){
{newAnswer();
{if ((QuestionCall.display6E(balance.get(key+1))+ "").equals(objective.get(control)))
scores=1;
else
scores =0;}
newScore();
if ("nothing".equals(value)){
check=0;
newCheck();
}
}
++counter;}
if (control<counter){
replaceAnswer();
{if ((QuestionCall.display6E(balance.get(key+1))+ "").equals(objective.get(control)))
scores=1;
else
scores =0;}
changeScore();
if ("nothing".equals(value)){
check=0;
validateCheck();
}
}
++control;
System.out.println(QuestionCall.display6E(balance.get(key+1)));
++key;
removeAll();
setLayout( new BorderLayout() );
invalidate();
QuestionExams oan = new QuestionExams();
add(oan,BorderLayout.NORTH );
revalidate();
System.out.println(key);
System.out.println("counter:"+counter +" and "+"control:" +control+" position: "+position);
}
}
private void previousButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
on.numberBack();
selection();
--control;
--key;
removeAll();
setLayout(new FlowLayout());
setLayout( new BorderLayout() );
invalidate();
QuestionExams oan = new QuestionExams();
add(oan,BorderLayout.NORTH );
revalidate();
System.out.println(QuestionCall.display6E(balance.get(key+1)));
System.out.println(key);
System.out.println("counter:"+counter +" and "+"control:" +control);
}
private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int select = JOptionPane.showConfirmDialog(null, "Do you really want to quit the test?",
"Quit?",
JOptionPane.YES_NO_OPTION);
if (select == JOptionPane.YES_OPTION)
// to
{
examTimer.stop();
on.clearFramework();
removeAll();
setLayout(new FlowLayout());
setLayout( new BorderLayout() );
invalidate();
oktest an = new oktest();
add(an, BorderLayout.NORTH );
revalidate();}
}
}
private javax.swing.JRadioButton a;
private javax.swing.JRadioButton b;
private javax.swing.JRadioButton c;
private javax.swing.JRadioButton d;
public static javax.swing.JLabel examTiming;
private javax.swing.JButton finish;
private javax.swing.ButtonGroup group;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JPanel jPanel1;
public javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JScrollPane jScrollPane5;
private javax.swing.JTextArea nameQ;
private javax.swing.JButton nextButton;
private javax.swing.JTextArea optA;
private javax.swing.JTextArea optB;
private javax.swing.JTextArea optC;
private javax.swing.JTextArea optD;
private javax.swing.JButton previousButton;
private javax.swing.JButton quitButton;
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装操作员;
导入java.awt.BorderLayout;
导入java.awt.FlowLayout;
导入java.awt.Graphics;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.LinkedHashSet;
导入java.util.Set;
导入javax.swing.JOptionPane;
导入static operator.Rules.examTimer;
导入static operator.Welcome.image;
/**
*
*@作者亨迪
*/
公共类问题扩展javax.swing.JPanel{
QuestionCall on=新的QuestionCall();
int sBtn,nums=on.number();
/**
*创建新的表单
*/
公开考试(){
随机问题();
初始化组件();
如果(控制==0)
previousButton.setEnabled(错误);
添加(a)组;
添加(b)组;
添加(c)组;
添加(d)组;
设置已选();
如果(计数器>=(最大值()-1)和控制>=(最大值()-1))
{nextButton.setText(“Finish”);
finish.setVisible(false);}
如果(计数器==最大值()&&control==最大值())
{
行列式();
}
}
公共空间行列式(){
if(选中add()不太确定,但尝试在另一个线程中完全启动弹出窗口。这可能会起作用。解决了它。我不太确定错误来自何处,所以我决定创建自己的“是非”对话框。起初它工作正常,但当我将其设置为模态时,它与JOptionPane存在相同的问题,这使我得出结论,它是导致该事件的JOptionPane的模态属性。但通过将JOptionPane操作移动到下一步按钮(而不是构造函数),事件再次正常发生。-1所有Swing组件都应在事件调度线程(EDT)上创建/修改。