在GUI中生成非重复随机方法

时间:2013-03-10 11:07:04

标签: java swing user-interface random actionlistener

我花了这么长时间看着我的电脑显示器,因为当我点击开始按钮时,我真的不知道如何防止我的程序中的帧同时出现。< / p>

这是我的主要课程:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.*;

public class PopQuizDemo {
    public static void main (String args[]){
        PopQuizDemo();
    }

        public static void PopQuizDemo(){
        final SimpleFrame frame = new SimpleFrame();

        final JPanel main = new JPanel();
        main.setSize(400,75);
        main.setLayout(new GridLayout(3,1));
        frame.add(main);

        JLabel l1 = new JLabel("Welcome to POP Quiz!");
        main.add(l1);

        JLabel l2 = new JLabel("Enter your name:");
        main.add(l2);

        final JTextField name = new JTextField ();
        main.add(name);

        final JPanel panel = new JPanel();
        panel.setSize(400,50);
        panel.setLocation(0,225);
        frame.add(panel);

        JButton start = new JButton ("Start");
        panel.add(start);

        frame.setVisible(true);

        start.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
                 frame.setVisible(false);
                 randomize();
             }
        });
    }

    public static void randomize(){
        Questions q = new Questions();
        int a=0;
        Random randnum = new Random (System.currentTimeMillis());
        java.util.HashSet<Integer> myset = new java.util.HashSet<>();
        for (int count = 1; count <= 3; count++){
            while (true) {
                a = randnum.nextInt (3);
                if(!myset.contains(a)) { myset.add(new Integer(a)); break;}
            }
            if(a==0){
                q.one();
            }
            else if(a==1){
                q.two();
            }
            else if(a==2){
                q.three();
            }
            else{
                break;
            }
        }
    }
}

这是类问题,我得到方法one(),two()和three():

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Questions {
public static void one(){
    final SimpleFrame frame = new SimpleFrame();

    JPanel p1 = new JPanel();
    p1.setSize(400,100);
    frame.add(p1);

    JLabel qu1 = new JLabel();
    qu1.setText("In computers, what is the smallest and basic unit of");
    JLabel qu2 = new JLabel();
    qu2.setText("information storage?");
    p1.add(qu1);
    p1.add(qu2);

    JPanel p2 = new JPanel();
    p2.setSize(400,175);
    p2.setLocation(0,100);
    p2.setLayout(new GridLayout(2,4));
    frame.add(p2);

    JButton a = new JButton("a. Bit");
    p2.add(a);

    JButton b = new JButton("b. Byte");
    p2.add(b);

    JButton c = new JButton("c. Data");
    p2.add(c);        

    JButton d = new JButton("d. Newton");
    p2.add(d);

    frame.setVisible(true);

    final PopQuizDemo demo = new PopQuizDemo();

    a.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             frame.setVisible(false);
             demo.randomize();
         }
    });
    b.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             frame.setVisible(false);
             demo.randomize();
         }
    });
    c.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             frame.setVisible(false);
             demo.randomize();
         }
    });
    d.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             frame.setVisible(false);
             demo.randomize();
         }
    });
}//end of method

public static void two(){
    final SimpleFrame frame = new SimpleFrame();

    JPanel p1 = new JPanel();
    p1.setSize(400,100);
    frame.add(p1);

    JLabel qu1 = new JLabel();
    qu1.setText("Machine language is also known as __________.");
    p1.add(qu1);

    JPanel p2 = new JPanel();
    p2.setSize(400,175);
    p2.setLocation(0,100);
    p2.setLayout(new GridLayout(2,4));
    frame.add(p2);

    JButton a = new JButton("a. Low level language");
    p2.add(a);

    JButton b = new JButton("b. Assembly language");
    p2.add(b);

    JButton c = new JButton("c. High level language");
    p2.add(c);        

    JButton d = new JButton("d. Source code");
    p2.add(d);

    frame.setVisible(true);

    final PopQuizDemo demo = new PopQuizDemo();

    a.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             frame.setVisible(false);
             demo.randomize();
         }
    });
    b.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             frame.setVisible(false);
             demo.randomize();
         }
    });
    c.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             frame.setVisible(false);
             demo.randomize();
         }
    });
    d.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             frame.setVisible(false);
             demo.randomize();
         }
    });
}//end of method

public static void three(){
    final SimpleFrame frame = new SimpleFrame();

    JPanel p1 = new JPanel();
    p1.setSize(400,100);
    frame.add(p1);

    JLabel qu1 = new JLabel();
    qu1.setText("What is the shortcut key of printing a document for");
    JLabel qu2 = new JLabel();
    qu2.setText("computers using Windows?");
    p1.add(qu1);
    p1.add(qu2);

    JPanel p2 = new JPanel();
    p2.setSize(400,175);
    p2.setLocation(0,100);
    p2.setLayout(new GridLayout(2,4));
    frame.add(p2);

    JButton a = new JButton("a. Ctrl + P");
    p2.add(a);

    JButton b = new JButton("b. Shift + P");
    p2.add(b);

    JButton c = new JButton("c. Shift + PP");
    p2.add(c);        

    JButton d = new JButton("d. Alt + P");
    p2.add(d);

    frame.setVisible(true);

    final PopQuizDemo demo = new PopQuizDemo();

    a.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             frame.setVisible(false);
             demo.randomize();
         }
    });
    b.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             frame.setVisible(false);
             demo.randomize();
         }
    });
    c.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             frame.setVisible(false);
             demo.randomize();
         }
    });
    d.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             frame.setVisible(false);
             demo.randomize();
         }
    });
}//end of method
}

这里唯一的问题是,当我在开始按钮中的动作侦听器中调用randomize()方法时,它会显示所有帧。是的,他们没有重复,但它同时显示。我不知道问题出在哪里。这是方法随机化,循环,问题?有人能帮我吗?请?非常感谢。

PS: 这是SimpleFrame类

import javax.swing.JFrame;
public class SimpleFrame extends JFrame{
    public SimpleFrame(){
        setSize(400,300);
        setTitle("Pop Quiz!");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);
        setResizable(false);
    }
}

0 个答案:

没有答案