-1

我一直在看我的电脑显示器,因为当我单击“开始”按钮时,我真的不知道该怎么做才能防止我的程序上的帧同时出现。

这是我的主要课程:

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;
            }
        }
    }
}

这是类问题,我在其中获得方法一(),二()和三():

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);
    }
}
4

1 回答 1

1

您可以使用模型对话框使用 for 循环一个接一个地显示,如下所示:

public static void main(String[] args) {
        JFrame m = new JFrame("Hello");
        m.setSize(200,200);
        m.setVisible(true);
        for(int i=0;i<3;i++) {
            JDialog dlg = new JDialog(m,"Dialog",true);
            dlg.setSize(100,100);
            dlg.show();
        }
    }
于 2013-03-10T11:29:05.573 回答