JOptionPane似乎导致JPanel消失

时间:2014-10-15 14:46:05

标签: java swing paintcomponent joptionpane

我正在开展一个测验计划,如果没有回答所有问题,会出现一个JOptionpane确认对话框,询问用户是否要继续。起初,一切都运行正常,然后我决定在整个程序中使用paintComponent()方法。在此实现之后,如果弹出此JOptionpane,则包含的Jpanel将消失(将所有问题显示出来),然后只有背景保持可见。一整天都在这。这是发生问题的代码:

/*
 * 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;

}

大多数布局代码都被遗漏了。为清晰起见,我还包括一些图像。

首先是最后一个问题页面 enter image description here

接下来是当我点击下一个按钮(现在标记为完成)时会发生什么 enter image description here

最后,这是原始的工作,在我做paintComponent()方法之前,不幸的是, 我没有这个的代码(我只有.exe文件) enter image description here

我不知道它是否是一个错误,因为Quit按钮也显示了它自己的选项窗格,并且显示完美。

2 个答案:

答案 0 :(得分:0)

解决了它。我不太确定错误来自哪里,所以我决定创建自己的yes-no对话框。它起初工作正常,但是当我将其设置为模态时,它与JOptionPane具有相同的问题,这使我得出结论,它是导致它的JOptionPane的模态属性。但是通过将JOptionPane操作移动到下一个按钮而不是构造函数,事件发生了,就像正常一样。

答案 1 :(得分:-1)

不太确定,但尝试完全在另一个线程中启动弹出窗口。这可能有用。