import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SimpleUI extends JFrame implements ActionListener { JTextField field; JLabel answerlbl; public SimpleUI () { this.setSize(400, 400); this.setTitle("Simple UI"); setupButtonsPanel(); setupResponsePanel(); setupQuestionPanel(); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); } void setupButtonsPanel() { JButton b1 = new JButton("Clear"); JButton b2 = new JButton("Submit"); b1.addActionListener(this); b2.addActionListener(this); JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.add(b1); p.add(b2); getContentPane().add(p, BorderLayout.SOUTH); } void setupResponsePanel() { JLabel lbl = new JLabel("Answer: "); field = new JTextField(12); JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.add(lbl); p.add(field); this.getContentPane().add(p, BorderLayout.CENTER); } void setupQuestionPanel() { JPanel p = new JPanel(); p.setLayout(new GridLayout(2, 1, 20, 20)); JLabel qlbl = new JLabel("What day of the week is it today?"); answerlbl = new JLabel("Your answer is: "); p.add(qlbl); p.add(answerlbl); getContentPane().add(p, BorderLayout.NORTH); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Submit")) { String answerText = field.getText(); if (answerText.equalsIgnoreCase("Friday")) answerlbl.setText("Your answer is correct"); else answerlbl.setText("Your answer is wrong"); } else if (e.getActionCommand().equals("Clear")) { field.setText(""); } } public static void main(String[] args) { new SimpleUI(); } }