当前位置: 首页 > 知识库问答 >
问题:

程序总是重复,即使我点击没有按钮

戚锦
2023-03-14

jframe中的一个考试制造者,在您回答完问题后,将弹出一个joptionpane并询问您是否要重考,我单击否,程序仍在运行。

package quizmaker;

import javax.swing.JOptionPane;

public class QuizMaker extends javax.swing.JFrame {

    private static QuestionBuilder Questions = new QuestionBuilder();
    private static String[] Question = new String[5];
    private static String[] Answer = new String[5];
    private static String[] Bogus1 = new String[5];
    private static String[] Bogus2 = new String[5];
    private static String[] Bogus3 = new String[5];
    private static int[] QuestionsIndex = {0, 1, 2, 3, 4};
    private static int Index=0, Score=0, QuestionNumber=0;

    public QuizMaker() {
        initComponents();
        goToNext();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        jLabel1.setText("jLabel1");

        jLabel2.setText("jLabel2");

        jTextField1.setEditable(false);
        jTextField1.setText("jTextField1");

        jButton1.setText("jButton1");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton2.setText("jButton2");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jButton3.setText("jButton3");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jButton4.setText("jButton4");
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel2))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
                            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        String YourAnswer = jButton1.getText();
        checkAnswer(YourAnswer);
        goToNext();
    }                                     

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        String YourAnswer = jButton2.getText();
        checkAnswer(YourAnswer);
        goToNext();
    }                                     

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        String YourAnswer = jButton3.getText();
        checkAnswer(YourAnswer);
        goToNext();
    }                                     

    private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        String YourAnswer = jButton4.getText();
        checkAnswer(YourAnswer);
        goToNext();
    }                                     

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(QuizMaker.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(QuizMaker.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(QuizMaker.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(QuizMaker.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        for (int x=0; x<=4; x++) {
            QuestionNumber = x + 1;
            Question[x] = JOptionPane.showInputDialog(null, "Enter Question # " + QuestionNumber, "Question # " + QuestionNumber);
            Answer[x] = JOptionPane.showInputDialog(null, "Enter Answer in Question # " + QuestionNumber, "Answer in Question # " + QuestionNumber);
            Bogus1[x] = JOptionPane.showInputDialog(null, "Enter Bogus 1 in Question # " + QuestionNumber, "Bogus 1 in Question # " + QuestionNumber);
            Bogus2[x] = JOptionPane.showInputDialog(null, "Enter Bogus 2 in Question # " + QuestionNumber, "Bogus 2 in Question # " + QuestionNumber);
            Bogus3[x] = JOptionPane.showInputDialog(null, "Enter Bogus 3 in Question # " + QuestionNumber, "Bogus 3 in Question # " + QuestionNumber);
        }
        QuestionNumber=1;
        for (int x=4; x>=0; x--) {
            int y=x+1;
            int w=(int)(Math.random()*y);
            int Container = QuestionsIndex[w];
            QuestionsIndex[w] = QuestionsIndex[x];
            QuestionsIndex[x] = Container;
        }
        Message("Let's start the quiz.");
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new QuizMaker().setVisible(true);
            }
        });
    }
    public static void goToNext() {
        if (QuestionNumber > 5) {
            Message("Your score is " + Score + " out of 5.");
            int ConfirmButton = JOptionPane.YES_NO_CANCEL_OPTION;
            JOptionPane.showConfirmDialog(null, "Do yo want to retake?", null, ConfirmButton);
            if (ConfirmButton == JOptionPane.YES_OPTION) {
                Score=0;
                QuestionNumber=1;
                for (int x=4; x>=0; x--) {
                    int y=x+1;
                    int w=(int)(Math.random()*y);
                    int Container = QuestionsIndex[w];
                    QuestionsIndex[w] = QuestionsIndex[x];
                    QuestionsIndex[x] = Container;
                }
                goToNext();
            }
            else {
                System.exit(0);
            }
        }
        else if (QuestionNumber <= 5) {
            jLabel1.setText("Question " + QuestionNumber + " of 5");
            jLabel2.setText("Score: " + Score);
            Questions.setQuestion(Question[QuestionsIndex[QuestionNumber-1]]);
            Questions.setAnswer(Answer[QuestionsIndex[QuestionNumber-1]]);
            Questions.setBogus1(Bogus1[QuestionsIndex[QuestionNumber-1]]);
            Questions.setBogus2(Bogus2[QuestionsIndex[QuestionNumber-1]]);
            Questions.setBogus3(Bogus3[QuestionsIndex[QuestionNumber-1]]);
            String[] Choices = new String[4];
            Choices[0] = Questions.getAnswer();
            Choices[1] = Questions.getBogus1();
            Choices[2] = Questions.getBogus2();
            Choices[3] = Questions.getBogus3();
            for (int x=3; x>=0; x--) {
                int y=x+1;
                int w=(int)(Math.random()*y);
                String Container = Choices[w];
                Choices[w] = Choices[x];
                Choices[x] = Container;
            }
            jTextField1.setText(Questions.getQuestion());
            jButton1.setText(Choices[0]);
            jButton2.setText(Choices[1]);
            jButton3.setText(Choices[2]);
            jButton4.setText(Choices[3]);
            QuestionNumber++;
        }
    }
    public static void checkAnswer(String YourAnswer) {
        if (YourAnswer.equals(Questions.getAnswer())) {
            Message("Correct!");
            Score++;
        }
        else {
            Message("Wrong!");
        }
    }
    public static void Message(Object Message) {
        JOptionPane.showMessageDialog(null, Message);
    }

    // Variables declaration - do not modify                     
    private static javax.swing.JButton jButton1;
    private static javax.swing.JButton jButton2;
    private static javax.swing.JButton jButton3;
    private static javax.swing.JButton jButton4;
    private static javax.swing.JLabel jLabel1;
    private static javax.swing.JLabel jLabel2;
    private static javax.swing.JTextField jTextField1;
    // End of variables declaration                   
}

共有1个答案

公西光华
2023-03-14

用户选择的选项是ShowConfirmDialog(...)方法的返回值。您必须这样做:

int selected = JOptionPane.showConfirmDialog(null, "Do yo want to retake?", null, JOptionPane.YES_NO_CANCEL_OPTION);
if(selected == JOptionPane.YES_OPTION){
   ...
}
 类似资料:
  • 我试图点击下面xpath提供的按钮。它显示以下错误: cmd中。

  • 我一直在自己编程一个应用程序,但每次我点击一个按钮,它就会遇到一些问题(因为问题出在它的OnCreate方法上)。我把它缩小到我认为是什么原因,似乎是这条线:

  • (对不起,我的英语很差,如果你没有理解我的问题,我很抱歉,你可以尝试运行代码->绘制矩形->单击撤消->在原点处得到一个点(这是我试图解释的问题))

  • 我的应用程序在点击按钮后崩溃。一些请帮助我制作的应用程序是用于表格的。但不幸的是,该应用程序在按钮点击强文本后崩溃 包裹通讯。实例CH1practicemulti;

  • 我想为我的一个项目测试多线程,试图开发一个解决方案,以防出现问题。 所以我做了一个小测试: (您必须修复导入,我还使用自定义类,尽管这只是的包装) 奇怪的是,有时它工作正常,有时则不正常(终止线程总数为9,尽管我可以清楚地看到它打印了“我做到了!”误差正好是10倍)。 任何修复?

  • 日志猫错误:我不知道这意味着什么,因为我是android新手,请帮助解决这个问题,因为它今晚到期LOL:( 带按钮的菜单页面(沙漠按钮进入沙漠菜单) 沙漠页面,应该像其他任何东西一样打开,但应用程序停止