Here's how we solve Homework Four. On Tuesday we reviewed these notes from the past: http://www.cs.indiana.edu/classes/a202-dger/spr2006/notes/One.html http://www.cs.indiana.edu/classes/a202-dger/spr2006/notes/Two.html http://www.cs.indiana.edu/classes/a202-dger/spr2006/notes/Three.html From the first set of notes we ended up with: import javax.swing.*; import java.awt.*; import java.awt.event.*; class One { public static void main(String[] args) { JFrame f = new JFrame(); Container c = f.getContentPane(); c.setLayout(null); JButton b = new JButton(); b.setBounds(20, 20, 120, 20); b.setText("Push me!"); c.add(b); JLabel output = new JLabel(); output.setBounds(50, 70, 220, 20); output.setText("0 clicks so far. Push the button."); c.add(output); Two referee = new Two(output); b.addActionListener(referee); f.setSize(400, 400); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class Two implements ActionListener { int count = 0; JLabel label; Two(JLabel label) { this.label = label; } public void actionPerformed(ActionEvent e) { this.count += 1; System.out.println("Ouch: " + this.count); this.label.setText(this.count + " click(s) so far and counting."); } } So this program builds a simple GUI that processes and reports events. From the third set of notes we learned how to develop a program like this: import javax.swing.*; import java.awt.*; import java.awt.event.*; class One { public static void main(String[] args) { JFrame f = new JFrame(); Container c = f.getContentPane(); c.setLayout(null); JTextField t = new JTextField(); t.setHorizontalAlignment(JTextField.CENTER); // t.setEditable(false); t.setBounds(200, 40, 40, 20); t.setText("0"); c.add(t); JButton b = new JButton(); b.setBounds(70, 40, 120, 20); b.setText("Add One"); c.add(b); Two referee = new Two(t); b.addActionListener(referee); f.setSize(400, 400); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class Two implements ActionListener { JTextField t; Two(JTextField t) { this.t = t; } public void actionPerformed(ActionEvent e) { String val = t.getText(); int num = Integer.parseInt(val); num += 1; t.setText(num + ""); } } This second program reads from a text field and updates it too. To finish Homework Four you need to put these two programs together. The question now is what style should we adopt? So the notes in the middle were trying to say this is another option: import javax.swing.*; import java.awt.*; import java.awt.event.*; class One extends JFrame { private JButton b, down; private JLabel output; One() { createUserInterface(); } private void createUserInterface() { Container c = getContentPane(); c.setLayout(null); b = new JButton(); b.setBounds(20, 20, 120, 20); b.setText("Push me!"); c.add(b); down = new JButton(); down.setBounds(20, 120, 180, 20); down.setText("Push to withdraw"); c.add(down); output = new JLabel(); output.setBounds(50, 70, 220, 20); output.setText("0 clicks so far. Push the button."); c.add(output); Two referee = new Two(b, down, output); b.addActionListener(referee); down.addActionListener(referee); setSize(400, 400); setVisible(true); } public static void main(String[] args) { One one = new One(); one.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class Two implements ActionListener { int count; JButton b, down; JLabel output; Two(JButton b, JButton down, JLabel output) { this.b = b; this.down = down; this.output = output; } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == b) { count += 1; } else if (source == down) { count -= 1; } else { System.out.println("What?!"); } System.out.println(this.count + " clicks recorded so far."); } } But we could combine the classes like we did when we wrote the applets: import javax.swing.*; import java.awt.*; import java.awt.event.*; class One extends JFrame implements ActionListener { private JButton b, down; private JLabel output; One() { createUserInterface(); } private void createUserInterface() { Container c = getContentPane(); c.setLayout(null); b = new JButton(); b.setBounds(20, 20, 120, 20); b.setText("Push me!"); c.add(b); down = new JButton(); down.setBounds(20, 120, 180, 20); down.setText("Push to withdraw"); c.add(down); output = new JLabel(); output.setBounds(50, 70, 220, 20); output.setText("0 clicks so far. Push the button."); c.add(output); Two referee = new Two(b, down, output); b.addActionListener(referee); down.addActionListener(referee); setSize(400, 400); setVisible(true); } public static void main(String[] args) { One one = new One(); one.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } int count; public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == b) { count += 1; } else if (source == down) { count -= 1; } else { System.out.println("What?!"); } System.out.println(this.count + " clicks recorded so far."); } } So let's start again in a very simple way: import javax.swing.*; class Quiz extends JFrame { } We compile but can't run this. So we add: import javax.swing.*; class Quiz extends JFrame { public static void main(String[] args) { } } So I try to get started: import javax.swing.*; class Quiz extends JFrame { public static void main(String[] args) { Quiz q = new Quiz(); q.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } But this runs yet no frame shows up. Why? import javax.swing.*; class Quiz extends JFrame { Quiz() { setSize(400, 400); setVisible(true); } public static void main(String[] args) { Quiz q = new Quiz(); q.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } This creates the frame and disposes of it when we close it. So it's a good start. Let's start putting the GUI together. import javax.swing.*; class Quiz extends JFrame { Quiz() { setSize(400, 400); setVisible(true); JButton b = new JButton(); b.setBounds(30, 30, 50, 50); b.setText("The Button"); } public static void main(String[] args) { Quiz q = new Quiz(); q.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } I created a button and told it how big it is, where it's located, and what writes on it. So let's add somehow the button to the frame: import javax.swing.*; import java.awt.*; class Quiz extends JFrame { Quiz() { setSize(400, 400); setVisible(true); JButton b = new JButton(); b.setBounds(30, 30, 50, 50); b.setText("The Button"); Container hook = getContentPane(); hook.add(b); } public static void main(String[] args) { Quiz q = new Quiz(); q.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } The problem here, though, is that the default layout manager is taking over. And I am getting a huge button. So how do I disable the layout manager to get a button that's as big as I want and positioned exactly as I want? import javax.swing.*; import java.awt.*; class Quiz extends JFrame { Quiz() { setSize(400, 400); setVisible(true); JButton b = new JButton(); b.setBounds(30, 30, 50, 50); b.setText("The Button"); Container hook = getContentPane(); hook.setLayout(null); hook.add(b); } public static void main(String[] args) { Quiz q = new Quiz(); q.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } So hook.setLayout(null) takes care of it. Now the button is where I want it, as big as I want it (too small in fact). Let's make it where the button is actually processing events: import javax.swing.*; import java.awt.*; import java.awt.event.*; class Quiz extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("The button has been pressed."); } Quiz() { setSize(400, 400); setVisible(true); JButton b = new JButton(); b.setBounds(30, 30, 150, 50); b.setText("The Button"); b.addActionListener(this); Container hook = getContentPane(); hook.setLayout(null); hook.add(b); } public static void main(String[] args) { Quiz q = new Quiz(); q.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } So from here we can make it where pushing the button asks new questions. import javax.swing.*; import java.awt.*; import java.awt.event.*; class Quiz extends JFrame implements ActionListener { int n1, n2; public void actionPerformed(ActionEvent e) { n1 = (int) (Math.random() * 100) - 50; n2 = (int) (Math.random() * 100) - 50; System.out.println("What is " + n1 + " + " + n2 + "?"); } Quiz() { setSize(400, 400); setVisible(true); JButton b = new JButton(); b.setBounds(30, 30, 150, 50); b.setText("The Button"); b.addActionListener(this); Container hook = getContentPane(); hook.setLayout(null); hook.add(b); } public static void main(String[] args) { Quiz q = new Quiz(); q.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } So we add a new text field: import javax.swing.*; import java.awt.*; import java.awt.event.*; class Quiz extends JFrame implements ActionListener { int n1, n2; public void actionPerformed(ActionEvent e) { n1 = (int) (Math.random() * 100) - 50; n2 = (int) (Math.random() * 100) - 50; System.out.println("What is " + n1 + " + " + n2 + "?"); } Quiz() { setSize(400, 400); setVisible(true); JButton b = new JButton(); b.setBounds(30, 30, 150, 50); b.setText("The Button"); JTextField t = new JTextField(); t.setBounds(200, 50, 50, 30); b.addActionListener(this); Container hook = getContentPane(); hook.setLayout(null); hook.add(b); hook.add(t); } public static void main(String[] args) { Quiz q = new Quiz(); q.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } So now the text field shows, let's read from it. import javax.swing.*; import java.awt.*; import java.awt.event.*; class Quiz extends JFrame implements ActionListener { int n1, n2; JTextField t; public void actionPerformed(ActionEvent e) { int answer = Integer.parseInt(t.getText()); if (answer == n1 + n2) System.out.println("Good."); else System.out.println("Nope."); n1 = (int) (Math.random() * 100) - 50; n2 = (int) (Math.random() * 100) - 50; System.out.println("What is " + n1 + " + " + n2 + "?"); } Quiz() { setSize(400, 400); setVisible(true); JButton b = new JButton(); b.setBounds(30, 30, 150, 50); b.setText("The Button"); t = new JTextField(); t.setBounds(200, 50, 50, 30); b.addActionListener(this); Container hook = getContentPane(); hook.setLayout(null); hook.add(b); hook.add(t); } public static void main(String[] args) { Quiz q = new Quiz(); q.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } This is almost doing the addition quiz, we just have to clean it up.