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.