import javax.swing.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; public class Sample3 extends JFrame{ // inherit JFrame /** * Constructor sets up the window and creates a toolbar */ public Sample3() { // // setup the frames banner/title bar // super("Sample 3"); // call JFrame's constructor and give it a title setSize(300,200); // set the width of the window to 300 pixels, height to 200. // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit the whole program when this frame is closed addWindowListener(new WindowAdapter() { // Quit the application public void windowClosing(WindowEvent e) { System.exit(0); } }); // // Create a JToolbar and add it to the panel in the top (North) position // JToolBar tools = new JToolBar(JToolBar.HORIZONTAL); // create a horizontal tool bar getContentPane().add(tools,"North"); // add the tool bar to the panel // // Create the buttons, set an action listner for each, and add them to the toolbar // addTextButton(tools,"Open","Open a file",new OpenToolBarListener()); // add text only button addImageButton(tools,"save.gif","save","Save the file",new SaveToolBarListener()); // add image and text addImageButton(tools,"forward.gif","Forward the email",new ForwardToolBarListener()); // add image only button } /** * This method creates and adds a text button to the tool bar * @param bar reference to a JToolBar * @param text text string to appear on the button * @param tooltip text string to appear when cursor lingers over the button * @param listener reference to an object that implements an ActionListener */ void addTextButton(JToolBar bar, String text, String tooltip, ActionListener listener) { JButton b1 = new JButton(text); // create the button b1.addActionListener(listener); // tell it who to send events to b1.setToolTipText(tooltip); // set the tip text bar.add(b1); // add the button to the toolbar } /** * This method creates and adds an image+text button to the tool bar * @param bar reference to a JToolBar * @param file filename of the .gif image file to appear on the button * @param text text string to appear on the button * @param tooltip text string to appear when cursor lingers over the button * @param listener reference to an object that implements an ActionListener */ void addImageButton(JToolBar bar, String file, String text, String tooltip, ActionListener listener) { ImageIcon icon = new ImageIcon(file); // create an image based on the file JButton b1 = new JButton(text,icon); // create the button b1.addActionListener(listener); // tell it who to send events to b1.setToolTipText(tooltip); // tell it what the tip should be bar.add(b1); // add the button to the toolbar } /** * This method creates and adds an image button to the tool bar * @param bar reference to a JToolBar * @param file filename of the .gif image file to appear on the button * @param tooltip text string to appear when cursor lingers over the button * @param listener reference to an object that implements an ActionListener */ void addImageButton(JToolBar bar, String file, String tooltip,ActionListener listener) { ImageIcon icon = new ImageIcon(file); // create an image based on the file JButton b1 = new JButton(icon); // create the button b1.addActionListener(listener); // tell it who to send events to b1.setToolTipText(tooltip); // tell it what the tip should be bar.add(b1); } // /*********************************************************************** * * Main routine, Note, this routine can be in any class. */ public static void main(String[] arguments){ Sample3 fr = new Sample3(); // create the sample 3 frame fr.setVisible(true); // allow it to be seen } /******************************************************************* * Inner Listener Classes, one acts as a listener for each user interface item * * Alternatively you could just implement one actionPerformed routine * in the Frame class, however, that routine would have to do a lot of * work just to figure out what menu item, button, etc, created the event. * this is easier, cleaner, and isolates code for each UI item */ /** * Handle the Open toolbar button item */ class OpenToolBarListener implements ActionListener{ public void actionPerformed(ActionEvent event) { System.out.println("Open button pressed"); } } /** * Handle the Save toolbar button item */ class SaveToolBarListener implements ActionListener{ public void actionPerformed(ActionEvent event) { System.out.println("Save button pressed"); } } /** * Handle the Forward toolbar button item */ class ForwardToolBarListener implements ActionListener{ public void actionPerformed(ActionEvent event) { System.out.println("Forward button pressed"); } } }