package guiexample; import java.awt.*; // older GUI library, has layout stuff for components import java.awt.event.*; // events import javax.swing.*; // swing is a library of GUI components // example GUI // class extends JFrame, so it is its own window public class BunnyGUI extends JFrame { // the bunny private Bunny mrBun; // input private JTextField namebox; private JTextField carrotbox; private JButton feedbutton; // output private JLabel display; // constructor does all setup of window and buttons public BunnyGUI() { // set up the window super("Your pet bunny"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); mrBun = new Bunny(); mrBun.setMyUI(this); // output text box display = new JLabel(); // call our own method for updating the display updateDisplayBox(); setupNameBox(); setupCarrotBox(); setupFeedButton(); // put the widgets in the window add(new JLabel("Enter values to reset Bunny's data")); add(namebox); add(carrotbox); add(display); add(feedbutton); // make the window visible pack(); setVisible(true); } // setup box for entering name public void setupNameBox() { // input text box for name namebox = new JTextField(10); namebox.setText("Bunny Name"); // on enter, change the name of the bunny to the value given namebox.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { mrBun.setName(namebox.getText()); updateDisplayBox(); } } ); } // setup the box for entering number of carrots public void setupCarrotBox() { // input text box for carrots carrotbox = new JTextField(10); carrotbox.setText("Carrots"); // on enter, change the name of the bunny to the value given carrotbox.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try { // carrotbox contains a string, so need to parse as int mrBun.setCarrotCount(Integer.parseInt(carrotbox.getText())); updateDisplayBox(); } catch (Exception ex) { // nasty message on bad input JOptionPane.showMessageDialog( BunnyGUI.this, // parent window to center over // just this would be the actionlistener, BunnyGUI.this gets us the outer class "enter a whole number", // output to show "Bad Input", // message box title JOptionPane.ERROR_MESSAGE); // message box style } } } ); } // set up button for feeding Bunny public void setupFeedButton() { // button to feed the bunny feedbutton = new JButton("feed the bunny"); // on button click, feed the bunny some carrots feedbutton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { // this also causes output by the bunny... mrBun.feed((int) (Math.random() * 5) + 1); updateDisplayBox(); } } ); } // need to update display in several places, so stick it in its own method public void updateDisplayBox() { display.setText(mrBun.getName() + " ate " + mrBun.getCarrotCount() + " carrots total."); } // send message from the bunny to the user public void messageToUser(String msg) { JOptionPane.showMessageDialog(null, msg, "Bunny Message", JOptionPane.INFORMATION_MESSAGE); } // main to launch the GUI public static void main(String[] args) { BunnyGUI gui = new BunnyGUI(); } }