/** Pipe.java * Example Java Swing program displays Magritte's pipe with button * Mike Barnes */ import java.awt.*; import java.awt.event.*; import javax.swing.*; // subclass Frame for the main window /** Pipe uses a menubar, a BorderLayout manager, * and contains an icon label and push button. */ public class Pipe extends JFrame implements ActionListener { // instantiate the menubar and menuitems private JMenuBar menuBar; private JMenu aboutMenu; private JMenuItem artistItem, authorItem; private JButton aButton; private boolean toggle = true; private String frenchText = new String( "Ceci n'est pas une pipe"); private String englishText = new String( "This is not a pipe"); public static void main(String args[]) { Pipe mainW = new Pipe("With apologies to Rene Magritte"); mainW.setBounds(20,20,440,360); // set location and size mainW.setResizable(false); mainW.setVisible(true); // display the JFrame mainW.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // create and setup menu, dialogs, icon, and button. public Pipe(String frameTitle) { super(frameTitle); // call base constructor to set title Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout(5,5)); // create menus JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); aboutMenu = new JMenu("About"); aboutMenu.setMnemonic('A'); aboutMenu.setToolTipText( "See information about Magritte and Barnes"); // create a menu item and the dialog it invokes artistItem = new JMenuItem("artist"); artistItem.addActionListener( // anonymous inner class event handler new ActionListener() { public void actionPerformed(ActionEvent event) { JOptionPane.showMessageDialog( Pipe.this, "Rene Magrite\n" + "Belgium surrealist 1898-1967", "Artist", JOptionPane.PLAIN_MESSAGE); }} ); aboutMenu.add(artistItem); // add menu item to menu // create a menu item and the dialog it invokes authorItem = new JMenuItem("author"); authorItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { JOptionPane.showMessageDialog( Pipe.this, "G. Michael Barnes\n" + "Computer Science Dept\n" + "California State University Northridge\n\n" + "www.csun.edu/~renzo\n"+ "818.677.2299", "Author", JOptionPane.INFORMATION_MESSAGE, new ImageIcon("renzo.gif")); }} ); aboutMenu.add(authorItem); menuBar.add(aboutMenu); // create icon label from jpeg file JLabel pipeLabel = new JLabel(); pipeLabel.setIcon(new ImageIcon("pipe.jpg")); contentPane.add(pipeLabel, BorderLayout.CENTER); aButton = new JButton(frenchText); aButton.setToolTipText("Click to switch between French and English"); aButton.addActionListener(this); contentPane.add(aButton, BorderLayout.SOUTH); } // Respond to button clicks public void actionPerformed (ActionEvent e) { if (toggle) { aButton.setText(englishText); toggle = false; } else { aButton.setText(frenchText); toggle = true; } } }