0
public class QuizPlay {
    JFrame frame;
    JPanel mainPanel;
    JButton button;
    GridBagConstraints gbc;
    ArrayList<String> questions;
    ArrayList<String> answers;
    boolean isShown;
    Iterator<String> ita;
    Iterator<String> itq;
    JTextArea textArea;

    public static void main(String[] args) {
        QuizPlay quiz = new QuizPlay();
        quiz.loadLists(quiz.chooseFile());
        quiz.setupGui();

    }

    public QuizPlay() {
        frame = new JFrame();
        mainPanel = new JPanel(new GridBagLayout());
        questions = new ArrayList<>();
        answers = new ArrayList<>();
        gbc = new GridBagConstraints();
        textArea = new JTextArea();

    }

    public void setupGui() {
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        frame.setVisible(true);
        frame.add(mainPanel);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

        button = new JButton("Begin");
        button.setPreferredSize(new Dimension(150, 70));
        button.addActionListener(buttonListener);
        gbc.gridx = 0;
        gbc.gridy = 2;
        mainPanel.add(button, gbc);

        gbc.gridy = 1;
        mainPanel.add(Box.createRigidArea(new Dimension(10, 10)), gbc);

        Font font = new Font("sanserif", Font.BOLD, 24);
        textArea.setFont(font);
        textArea.setForeground(Color.BLACK);
        textArea.setEnabled(false);
        textArea.setPreferredSize(new Dimension(400, 400));
        gbc.weighty = 1;
        gbc.anchor = GridBagConstraints.PAGE_START;
        gbc.gridx = 0;
        gbc.gridy = 0;
        mainPanel.add(textArea, gbc);

        frame.pack();
    }

    public void loadLists(File file) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;
            while ((line = reader.readLine()) != null) {
                questions.add(line.split("/")[0]);
                answers.add(line.split("/")[1]);
                ita = answers.iterator();
                itq = questions.iterator();
            }
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public File chooseFile() {
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(frame);
        return chooser.getSelectedFile();
    }

    ActionListener buttonListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            if (isShown == false && itq.hasNext()) {
                textArea.setText(itq.next());
                button.setText("Show Answer");
                isShown = true;

            }

            else if (isShown == true && ita.hasNext()) {
                textArea.append("\n" + ita.next());
                button.setText("Next");
                isShown = false;

            }

            else
                button.setText("Done!");

        }

    };

}

I have the following program that reads questions and answers from a file and display them on the screen to test the users knowledge (Kind of like Quiz Cards). but for some reason when ever this program starts, the frame turns all black for about 2 seconda for some reason. can someone point to what is causing this weird bug.

aterai
  • 9,438
  • 4
  • 33
  • 43
GamefanA
  • 1,434
  • 2
  • 15
  • 22
  • 4
    Imagine you're a painter/artist. And a gallery wants to _display_ your canvas/artwork _before_ you even painted anything. Basically that's what you're trying to do by setting the frame visible before adding the component – Paul Samsotha Jul 13 '14 at 03:20
  • Duhh!! Thank you very much sir that fixed the problem. – GamefanA Jul 13 '14 at 03:38

0 Answers0