1. Here is the graphical equivalent to the ``Hello world.'' program from the first lab. import javax.swing.*; public class Test { public static void main (String[] args) { JOptionPane.showMessageDialog(null, "Click OK and I\'ll go away."); } } Try it. Fun, huh? 2. The trouble is that we want to create our own window with a Sudoku board and a SOLVE button on it, and that requires a separate object. Here is a start. Create a file GUI.java: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GUI { private JPanel panel; private JButton button; public GUI () { panel = new JPanel(new FlowLayout()); button = new JButton("Press me."); panel.add(button); } public void showInFrame () { JFrame frame = new JFrame("My first GUI"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(panel); frame.pack(); frame.setVisible(true); } } The panel is where we will put all the graphical elements. FlowLayout means fill in the panel from left to right, top to bottom, like how you write. The button is added to the panel. The frame is the window that appears when you run the program. The setDefaultCloseOperation line means close when you click the X. The panel is added to the frame. The pack line means make the frame just the right size for the panel and its button. After you finish GUI.java, add the lines GUI gui = new GUI(); gui.showInFrame(); to main in Test.java. Try it. 3. Unfortunately, nothing happens when you click the button because you did not say what should happen. For that you need to listen for the ACTION of clicking on the button. Add an implements to the class: public class GUI implements ActionListener { Add a method to handle the action: public void actionPerformed (ActionEvent event) { if (event.getSource() == button) { JOptionPane.showMessageDialog(null, "You pressed the button!"); } } Finally, right after creating the button, tell it to send its event to the listener, which is this GUI object: button.addActionListener(this); Now recompile Test.java (which should also recompile GUI.java) and run Test. It should now respond to the button. 4. Now let's add a place to talk to the user with text called a JTextField. private JTextField text; text = new JTextField("What is your name?"); panel.add(text); Add each of these lines after the analogous line for button. Don't add an action listener for the text. 5. Let's suppose we really want the button ABOVE the text. To do that, we can change the type of layout. Modify the initialization of panel to use a BorderLayout: panel = new JPanel(new BorderLayout()); Modify the add commands to specify where to put the button and text: panel.add(button, BorderLayout.NORTH); panel.add(text, BorderLayout.CENTER); Now try it. 6. In actionPerformed, read what is in the text field and put back "What is your name?": String name = text.getText(); text.setText("What is your name?"); Insert these lines just before the showMessageDialog line. Change the dialog to say: "Hello, " + name + "!" Now, when you press the button, it should read the name you typed and say hello to you. 7. Now, let's put your program up on the web. Java programs on the web are called APPLETS. To make your GUI an applet, modify its declaration public class GUI extends JApplet implements ActionListener { and add the following init method public void init () { setLayout(new FlowLayout()); add(panel); } Create GUI.html with the following contents. If you are in the lab, test your applet by typing appletviewer GUI.html The appletviewer won't work if you are at home, but the following step will work. 8. Copy your class files and html files into your web directory: cp *.class *.html ../public_html Then in your browser visit http://www.cs.miami.edu/students/????120/GUI.html where ????120 is your course account. NOTE: if you make a change, you have do the cp again, SHUT DOWN your browser, and then load again. If you don't shut down, the browser may not load your changes. The reload button isn't good enough.