我喜欢在我的程序中添加一个随机函数,但不知道如何

时间:2013-11-17 11:53:20

标签: java

该程序运行一个问题,一个答案,我想在同一个按钮和答案字段中提出更多问题和答案。 另一件事是,我将能够随机提出更多答案。

到目前为止的代码在这里:

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

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Secret Inspiration
 */
public class IdealeManGUI extends javax.swing.JFrame {

    /**
     * Creates new form VerdwaaldGUI
     */
    public IdealeManGUI() {
        initComponents();
        this.setIconImage(new ImageIcon(getClass().getResource("Vraagteken.png")).getImage());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        VerdwaaldText = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        BevestigButton = new javax.swing.JButton();
        AntwoordField = new javax.swing.JTextField();
        HoverBar = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        VerdwaaldText.setEditable(false);
        VerdwaaldText.setBackground(new java.awt.Color(255, 255, 204));
        VerdwaaldText.setColumns(20);
        VerdwaaldText.setFont(new java.awt.Font("Lucida Calligraphy", 0, 13)); // NOI18N
        VerdwaaldText.setLineWrap(true);
        VerdwaaldText.setRows(5);
        VerdwaaldText.setText("De Ideale man vragen:\n\nHallo ........., hoe gaat het met je?\nHeb je plannen vandaag?\nWaarom ga je dat doen?\nHoe voel jij je nu?\nWaarom?\nKan ik je ergens mee helpen?\nHeb je nog een advies voor mij?\nHeb je nog iets leuks te vertellen?");
        VerdwaaldText.setWrapStyleWord(true);
        jScrollPane1.setViewportView(VerdwaaldText);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(28, 23, 441, 201));

        jLabel1.setFont(new java.awt.Font("Lucida Calligraphy", 0, 13)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Wat is je vraag?");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(128, 242, 263, -1));

        BevestigButton.setBackground(new java.awt.Color(204, 255, 204));
        BevestigButton.setFont(new java.awt.Font("Lucida Calligraphy", 0, 13)); // NOI18N
        BevestigButton.setText("Bevestig je antwoord");
        BevestigButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BevestigButtonActionPerformed(evt);
            }
        });
        getContentPane().add(BevestigButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 340, 263, -1));

        AntwoordField.setFont(new java.awt.Font("Lucida Calligraphy", 1, 13)); // NOI18N
        AntwoordField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        AntwoordField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AntwoordFieldActionPerformed(evt);
            }
        });
        getContentPane().add(AntwoordField, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 290, 263, -1));

        HoverBar.setEditable(false);
        HoverBar.setFont(new java.awt.Font("Lucida Calligraphy", 0, 13)); // NOI18N
        HoverBar.setForeground(new java.awt.Color(140, 140, 239));
        HoverBar.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        HoverBar.setBorder(null);
        HoverBar.setOpaque(false);
        HoverBar.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                HoverBarMouseMoved(evt);
            }
        });
        getContentPane().add(HoverBar, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 270, 500, 20));

        jLabel2.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                jLabel2MouseMoved(evt);
            }
        });
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 500, 390));

        jMenu1.setText("Sluiten");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0));
        jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IdealeMan/ExitIcon.gif"))); // NOI18N
        jMenuItem1.setText("Sluiten");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Help");

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Handleiding");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

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

    private void BevestigButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
        if (AntwoordField.getText().equalsIgnoreCase("Waarom ga je dat doen"))
        {
            JOptionPane.showMessageDialog(null,"Omdat ik er zin in heb");
        }
        else
        {
            JOptionPane.showMessageDialog(null,"Formuleer de vraag anders");
    }                                              
    }
    private void AntwoordFieldActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
    }                                             

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        new HandleidingGUI().setVisible(true);
    }                                          

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        System.exit(0);
    }                                          

    private void jLabel2MouseMoved(java.awt.event.MouseEvent evt) {                                   
        // TODO add your handling code here:
    }                                  

    private void HoverBarMouseMoved(java.awt.event.MouseEvent evt) {                                    
        HoverBar.setText("Stel de juiste vraag");
    }                                   

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<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 ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IdealeManGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(IdealeManGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(IdealeManGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(IdealeManGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new IdealeManGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JTextField AntwoordField;
    private javax.swing.JButton BevestigButton;
    private javax.swing.JTextField HoverBar;
    private javax.swing.JTextArea VerdwaaldText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration                   
}

这是另一个课程:

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

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Secret Inspiration
 */
public class IdealeManGUI extends javax.swing.JFrame {

    /**
     * Creates new form VerdwaaldGUI
     */
    public IdealeManGUI() {
        initComponents();
        this.setIconImage(new ImageIcon(getClass().getResource("Vraagteken.png")).getImage());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        VerdwaaldText = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        BevestigButton = new javax.swing.JButton();
        AntwoordField = new javax.swing.JTextField();
        HoverBar = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        VerdwaaldText.setEditable(false);
        VerdwaaldText.setBackground(new java.awt.Color(255, 255, 204));
        VerdwaaldText.setColumns(20);
        VerdwaaldText.setFont(new java.awt.Font("Lucida Calligraphy", 0, 13)); // NOI18N
        VerdwaaldText.setLineWrap(true);
        VerdwaaldText.setRows(5);
        VerdwaaldText.setText("De Ideale man vragen:\n\nHallo ........., hoe gaat het met je?\nHeb je plannen vandaag?\nWaarom ga je dat doen?\nHoe voel jij je nu?\nWaarom?\nKan ik je ergens mee helpen?\nHeb je nog een advies voor mij?\nHeb je nog iets leuks te vertellen?");
        VerdwaaldText.setWrapStyleWord(true);
        jScrollPane1.setViewportView(VerdwaaldText);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(28, 23, 441, 201));

        jLabel1.setFont(new java.awt.Font("Lucida Calligraphy", 0, 13)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Wat is je vraag?");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(128, 242, 263, -1));

        BevestigButton.setBackground(new java.awt.Color(204, 255, 204));
        BevestigButton.setFont(new java.awt.Font("Lucida Calligraphy", 0, 13)); // NOI18N
        BevestigButton.setText("Bevestig je antwoord");
        BevestigButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BevestigButtonActionPerformed(evt);
            }
        });
        getContentPane().add(BevestigButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 340, 263, -1));

        AntwoordField.setFont(new java.awt.Font("Lucida Calligraphy", 1, 13)); // NOI18N
        AntwoordField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        AntwoordField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AntwoordFieldActionPerformed(evt);
            }
        });
        getContentPane().add(AntwoordField, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 290, 263, -1));

        HoverBar.setEditable(false);
        HoverBar.setFont(new java.awt.Font("Lucida Calligraphy", 0, 13)); // NOI18N
        HoverBar.setForeground(new java.awt.Color(140, 140, 239));
        HoverBar.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        HoverBar.setBorder(null);
        HoverBar.setOpaque(false);
        HoverBar.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                HoverBarMouseMoved(evt);
            }
        });
        getContentPane().add(HoverBar, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 270, 500, 20));

        jLabel2.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                jLabel2MouseMoved(evt);
            }
        });
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 500, 390));

        jMenu1.setText("Sluiten");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0));
        jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IdealeMan/ExitIcon.gif"))); // NOI18N
        jMenuItem1.setText("Sluiten");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Help");

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Handleiding");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

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

    private void BevestigButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
        if (AntwoordField.getText().equalsIgnoreCase("Waarom ga je dat doen"))
        {
            JOptionPane.showMessageDialog(null,"Omdat ik er zin in heb");
        }
        else
        {
            JOptionPane.showMessageDialog(null,"Formuleer de vraag anders");
    }                                              
    }
    private void AntwoordFieldActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
    }                                             

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        new HandleidingGUI().setVisible(true);
    }                                          

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        System.exit(0);
    }                                          

    private void jLabel2MouseMoved(java.awt.event.MouseEvent evt) {                                   
        // TODO add your handling code here:
    }                                  

    private void HoverBarMouseMoved(java.awt.event.MouseEvent evt) {                                    
        HoverBar.setText("Stel de juiste vraag");
    }                                   

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<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 ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IdealeManGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(IdealeManGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(IdealeManGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(IdealeManGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new IdealeManGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JTextField AntwoordField;
    private javax.swing.JButton BevestigButton;
    private javax.swing.JTextField HoverBar;
    private javax.swing.JTextArea VerdwaaldText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration                   
}

我希望有人可以帮助我,我是初学者并与Netbeans合作

1 个答案:

答案 0 :(得分:0)

我认为你需要的是:

List<String> questions = new ArrayList<String>();
// add questions to the List.
// ...

int randomPos = new Random().nextInt(questions.size());
// randomPos contains the index your are using to get a question from the List
// fulfilling randomPost >= 0 && randomPos < questions.size()

String question = questions.get(randomPos);
// question contains a random question from your questions List.
BevestigButton.setText(question);