import java.awt.Color;import java.awt.EventQueue;import java.awt.Font;import java.awt.SystemColor;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.InputStream;import java.util.Random;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.Clip;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.Timer;import javax.swing.border.EmptyBorder;public class Main extends JFrame {    private JPanel contentPane;    private JTextField txtFieldA;    private JTextField txtFieldB;    private JTextField txtFieldC;    private JTextField txtFieldD;    private JTextField txtFieldTimer;    private JTextArea textArea;    private JButton btnStart;    private JButton btnFinish;    private JButton btnA;    private JButton btnB;    private JButton btnC;    private JButton btnD;    private int timerVal = 0;    private JComboBox<String> comboBox;    private boolean clickA = false;    private boolean clickB = false;    private boolean clickC = false;    private boolean clickD = false;    private File correct = new File(C:\\Users\\Brian\\workspace\\VocabTester\\src\\correct.wav);    private File fail = new File(C:\\Users\\Brian\\workspace\\VocabTester\\src\\fail.wav);    private Timer t;    private Timer timer;    private Integer maxTime;    /**     * Launch the application.     */    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable() {            public void run() {                try {                    Main frame = new Main();                    frame.setVisible(true);                } catch (Exception e) {                    e.printStackTrace();                }            }        });    }    /**     * Create the frame.     */    public Main() {        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        setBounds(100, 100, 491, 465);        JMenuBar menuBar_1 = new JMenuBar();        setJMenuBar(menuBar_1);        JMenu mnFile = new JMenu(File);        menuBar_1.add(mnFile);        JMenuItem mntmExit = new JMenuItem(Exit);        mntmExit.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                System.exit(0);            }        });        mnFile.add(mntmExit);        JMenu mnHelp = new JMenu(Help);        menuBar_1.add(mnHelp);        JMenu mnSettings = new JMenu(Settings);        menuBar_1.add(mnSettings);        contentPane = new JPanel();        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));        setContentPane(contentPane);        contentPane.setLayout(null);        txtFieldA = new JTextField();        txtFieldA.setEditable(false);        txtFieldA.setBounds(96, 202, 232, 39);        contentPane.add(txtFieldA);        txtFieldA.setColumns(10);        txtFieldB = new JTextField();        txtFieldB.setEditable(false);        txtFieldB.setBounds(96, 252, 232, 40);        contentPane.add(txtFieldB);        txtFieldB.setColumns(10);        txtFieldC = new JTextField();        txtFieldC.setEditable(false);        txtFieldC.setBounds(96, 303, 232, 40);        contentPane.add(txtFieldC);        txtFieldC.setColumns(10);        txtFieldD = new JTextField();        txtFieldD.setEditable(false);        txtFieldD.setBounds(96, 354, 232, 40);        contentPane.add(txtFieldD);        txtFieldD.setColumns(10);        btnA = new JButton(A);        btnA.setEnabled(false);        btnA.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                setBtnsDisabled();                if (clickA) {                    playSound(correct);                    txtFieldA.setBackground(Color.green);                } else {                    playSound(fail);                    txtFieldA.setBackground(Color.red);                    showCorrectAnswer();                }                delayBeforeNextRound();            }        });        btnA.setBackground(SystemColor.control);        btnA.setBounds(10, 201, 76, 40);        contentPane.add(btnA);        btnB = new JButton(B);        btnB.setEnabled(false);        btnB.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                setBtnsDisabled();                if (clickB) {                    playSound(correct);                    txtFieldB.setBackground(Color.green);                } else {                    playSound(fail);                    txtFieldB.setBackground(Color.red);                    showCorrectAnswer();                }                delayBeforeNextRound();            }        });        btnB.setBackground(SystemColor.control);        btnB.setBounds(10, 252, 76, 40);        contentPane.add(btnB);        btnC = new JButton(C);        btnC.setEnabled(false);        btnC.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                setBtnsDisabled();                if (clickC) {                    playSound(correct);                    txtFieldC.setBackground(Color.green);                } else {                    playSound(fail);                    txtFieldC.setBackground(Color.red);                    showCorrectAnswer();                }                delayBeforeNextRound();            }        });        btnC.setBackground(SystemColor.control);        btnC.setBounds(10, 303, 76, 40);        contentPane.add(btnC);        btnD = new JButton(D);        btnD.setEnabled(false);        btnD.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                setBtnsDisabled();                if (clickD) {                    playSound(correct);                    txtFieldD.setBackground(Color.green);                } else {                    playSound(fail);                    txtFieldD.setBackground(Color.red);                    showCorrectAnswer();                }                delayBeforeNextRound();            }        });        btnD.setBackground(SystemColor.control);        btnD.setBounds(10, 354, 76, 40);        contentPane.add(btnD);        txtFieldTimer = new JTextField();        txtFieldTimer.setBackground(SystemColor.activeCaption);        txtFieldTimer.setHorizontalAlignment(SwingConstants.CENTER);        txtFieldTimer.setText(String.valueOf(timerVal));        txtFieldTimer.setFont(new Font(Tahoma, Font.BOLD, 35));        txtFieldTimer.setBounds(372, 312, 86, 82);        contentPane.add(txtFieldTimer);        txtFieldTimer.setColumns(10);        textArea = new JTextArea();        textArea.setEditable(false);        textArea.setLineWrap(true);        textArea.setBounds(10, 48, 448, 116);        contentPane.add(textArea);        btnStart = new JButton(START);        btnStart.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                setBtnsDisabled();                t = new Timer(500 * 1, new ActionListener() {                    public void actionPerformed(ActionEvent e) {                        startRound();                    }                });                t.start();            }        });        btnStart.setBackground(new Color(0, 204, 0));        btnStart.setBounds(372, 202, 86, 39);        contentPane.add(btnStart);        btnFinish = new JButton(FINISH);        btnFinish.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                t.stop();                timer.stop();                btnStart.setEnabled(true);                comboBox.setEnabled(true);                setBtnsDisabled();                txtFieldTimer.setText(0);                textArea.setText();                txtFieldA.setText();                txtFieldB.setText();                txtFieldC.setText();                txtFieldD.setText();                txtFieldA.setBackground(Color.getColor(240, 240, 240));                txtFieldB.setBackground(Color.getColor(240, 240, 240));                txtFieldC.setBackground(Color.getColor(240, 240, 240));                txtFieldD.setBackground(Color.getColor(240, 240, 240));            }        });        btnFinish.setBackground(new Color(204, 0, 0));        btnFinish.setBounds(372, 252, 89, 40);        contentPane.add(btnFinish);        comboBox = new JComboBox();        comboBox.setBounds(10, 171, 76, 20);        contentPane.add(comboBox);        comboBox.addItem(Java);        comboBox.addItem(Puppet);        JLabel lblVocabTester = new JLabel(Vocab Tester!);        lblVocabTester.setFont(new Font(Tahoma, Font.BOLD | Font.ITALIC, 28));        lblVocabTester.setBounds(10, 11, 216, 26);        contentPane.add(lblVocabTester);    }    public void startRound() {        btnStart.setEnabled(false);        comboBox.setEnabled(false);        if (t.isRunning()) {            t.stop();        }        txtFieldA.setBackground(Color.white);        txtFieldB.setBackground(Color.white);        txtFieldC.setBackground(Color.white);        txtFieldD.setBackground(Color.white);        setBtnsEnabled();        clickA = false;        clickB = false;        clickC = false;        clickD = false;        // turn timer for app on        maxTime = 20;        timer = new Timer(1000, new ActionListener() {            public void actionPerformed(ActionEvent e) {                txtFieldTimer.setText(maxTime.toString());                if (maxTime > 0) {                    maxTime -= 1;                } else {                    if (clickA) {                        txtFieldA.setBackground(Color.green);                    } else {                        txtFieldA.setBackground(Color.red);                    }                    if (clickB) {                        txtFieldB.setBackground(Color.green);                    } else {                        txtFieldB.setBackground(Color.red);                    }                    if (clickC) {                        txtFieldC.setBackground(Color.green);                    } else {                        txtFieldC.setBackground(Color.red);                    }                    if (clickD) {                        txtFieldD.setBackground(Color.green);                    } else {                        txtFieldD.setBackground(Color.red);                    }                    setBtnsDisabled();                    playSound(fail);                    delayBeforeNextRound();                }            }        });        timer.start();        // select random ids for the 4 textfields        Random rand = new Random();        int id1 = rand.nextInt(10) + 1;        int id2 = rand.nextInt(10) + 1;        int id3 = rand.nextInt(10) + 1;        int id4 = rand.nextInt(10) + 1;        // make sure that no ids match each other (otherwise their will be two        // of the same words in two differeen fields)        while (id4 == id3 || id4 == id2 || id4 == id1) {            id4 = rand.nextInt(10) + 1;        }        while (id3 == id4 || id3 == id2 || id3 == id1) {            id3 = rand.nextInt(10) + 1;        }        while (id2 == id4 || id2 == id3 || id2 == id1) {            id2 = rand.nextInt(10) + 1;        }        while (id1 == id4 || id1 == id3 || id1 == id2) {            id1 = rand.nextInt(10) + 1;        }        WordsDao dao = new WordsDao();        String word1 = null;        String word2 = null;        String word3 = null;        String word4 = null;        String vocabWords = ;        if (comboBox.getSelectedItem().equals(Java)) {            vocabWords = javawords;        } else if (comboBox.getSelectedItem().equals(Puppet)) {            vocabWords = puppetwords;        } else {            System.out.println(some other words!);        }        // nneed to -1 after ids, because the ids are one ahead due to the 0        // value        word1 = dao.getAllWords(vocabWords).get(id1 - 1).getWord();        word2 = dao.getAllWords(vocabWords).get(id2 - 1).getWord();        word3 = dao.getAllWords(vocabWords).get(id3 - 1).getWord();        word4 = dao.getAllWords(vocabWords).get(id4 - 1).getWord();        // Make an array of the 4 words, and choose one as the correct one        int[] arr = { id1, id2, id3, id4 };        Random random = new Random();        int correctId = random.nextInt(arr.length);        // get desc of correct word        Word desc = dao.getWordById(arr[correctId], vocabWords);        textArea.setText(desc.getDescription());        txtFieldA.setText(word1);        txtFieldB.setText(word2);        txtFieldC.setText(word3);        txtFieldD.setText(word4);        /// if an id of a word is the correct one        // then make appropriate btn (one next to it) correct if clicked        if (arr[correctId] == id1) {            clickA = true;        }        if (arr[correctId] == id2) {            clickB = true;        }        if (arr[correctId] == id3) {            clickC = true;        }        if (arr[correctId] == id4) {            clickD = true;        }    }    void delayBeforeNextRound() {        t = new Timer(1000 * 2, new ActionListener() {            public void actionPerformed(ActionEvent e) {                startRound();            }        });        t.start();    }    void setBtnsEnabled() {        btnA.setEnabled(true);        btnB.setEnabled(true);        btnC.setEnabled(true);        btnD.setEnabled(true);    }    void setBtnsDisabled() {        btnA.setEnabled(false);        btnB.setEnabled(false);        btnC.setEnabled(false);        btnD.setEnabled(false);    }    void playSound(File sound) {        try {            Clip clip = AudioSystem.getClip();            clip.open(AudioSystem.getAudioInputStream(sound));            clip.start();        } catch (Exception e) {            e.printStackTrace();        }        timer.stop();    }    void showCorrectAnswer() {        if(clickA){            txtFieldA.setBackground(Color.green);        }        if(clickB){            txtFieldB.setBackground(Color.green);        }        if(clickC){            txtFieldC.setBackground(Color.green);        }        if(clickD){            txtFieldD.setBackground(Color.green);        }    }}
Multiple-choice vocabulary tester
java;object oriented;swing;quiz
The code is very verbose; collapsing the four cases for each button and text field would make it easier to maintain, much easier to follow (and code review), and reduce the surface area for bugs to creep in.I'd recommend replacing the fields ending in A, B, C, and D with an array, or you can simply wrap them in one:private JButton[] choiceButtons = new JButton[4];private JTextField[] choiceText = new JTextField[4];For clickA through clickD, you could do this:private bool[] correctChoice = new bool[] {false, false, false, false};But since there's only one correct choice at a time, why not collapse it into a single index int instead:int correctChoice = 0;With that out of the way, the amount of code can be significantly reduced. To setup your text fields:int y = 202;for (int i = 0; i < 4; i++) {    choiceText[i] = new JTextField();    choiceText[i].setEditable(false);    choiceText[i].setBounds(96, y, 232, 40);    contentPane.add(choiceText[i]);    choiceText[i].setColumns(10);    y += 51;}Incidentally this makes all of your text fields the same width (I'm guessing having txtFieldA at a smaller width than the others wasn't intended). For buttons themselves, you only need a single ActionListener, and you can distinguish which was pressed by the source field in the event:ActionListener buttonListener = new ActionListener() {    public void actionPerformed(ActionEvent e) {        setBtnsDisabled();        for (int i = 0; i < 4; i++) {            if (e.source == choiceButtons[i]) {                // Found the button that was pressed                if (correctChoice == i) {                    playSound(correct);                    ((JTextField)choiceText[i]).setBackground(Color.green);                } else {                    ((JTextField)choiceText[i]).setBackground(Color.red);                    showCorrectAnswer();                }            }        }    }});String[] buttonLabels = new String[] {A, B, C, D};y = 201;for (int i = 0; i < 4; i++) {    choiceButtons[i] = new JButton(buttonLabels[i]);    choiceButtons[i].setEnabled(false);    choiceButtons[i].setBackground(SystemColor.control);    choiceButtons[i].setBounds(10, y, 76, 40);    contentPane.add(choiceButtons[i]);    y += 51;}The rest is just applying this paradigm other places.