import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; /** * @author Brad Chase * A Simple Swing Application * */ public class MainWindow1 implements ActionListener { private static void createGUI() { MainWindow1 mainWindow = new MainWindow1(); JFrame frame = new JFrame("Amazing Swing App"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(320, 260)); /*** setup menu ***/ JMenuBar menuBar = new JMenuBar(); /* the menu bar itself */ JMenu menu = new JMenu("File"); /* create a new menu */ menuBar.add(menu); /* add the menu to the menu bar that will contain it*/ JMenuItem item = new JMenuItem("Exit"); /* add an exit option */ item.addActionListener(mainWindow); /* this class will handle actions for this item */ item.setActionCommand("exit"); /* this is the text that accompanies the action event */ menu.add(item); frame.setJMenuBar(menuBar); /* add the menubar to the frame */ frame.setVisible(true); frame.validate(); } /** * @param e the action that occurred * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ public void actionPerformed(ActionEvent e) { //we have to figure out what action occurred //to do so, we check the action command text in the actionevent if(e.getActionCommand().equals("exit")){ System.exit(0); } } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createGUI(); } }); } }