import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.JMenu; import javax.swing.JDialog; import javax.swing.JMenuItem; import javax.swing.JMenuBar; import java.io.*; public class TextEditor extends WindowAdapter implements ActionListener { final JLabel label = new JLabel("Text Editor"); final static String LOOKANDFEEL = null; public static JFrame frame = new JFrame( "Text Editor" ); public void actionPerformed(ActionEvent e) { // gets events String num = e.getActionCommand(); if( num.equals( "NEW" ) ) { NewDoc(); } else if( num.equals( "OPEN" ) ) { OldDoc(); } else if( num.equals( "QUIT" ) ) { frame.dispose(); } } public void NewDoc() { TextWindow txw = new TextWindow(); txw.NewDoc(); } public void OldDoc() { TextWindow txw = new TextWindow(); txw.OldDoc(); } public Component createMain() { JButton button = new JButton("New Document"); // name of button button.setActionCommand("NEW"); // name of command button.addActionListener(this); // adds listener label.setLabelFor(button); JButton button2 = new JButton("Open Document"); button2.setActionCommand("OPEN"); button2.addActionListener(this); label.setLabelFor(button2); JButton button3 = new JButton("Quit Document"); button3.setActionCommand("QUIT"); button3.addActionListener(this); label.setLabelFor(button3); /* * An easy way to put space between a top-level container * and its contents is to put the contents in a JPanel * that has an "empty" border. */ JPanel pane = new JPanel(new GridLayout(0, 1)); pane.setBorder(BorderFactory.createEmptyBorder( 50, //top 80, //left 50, //bottom 80) //right ); pane.add(label); pane.add(button); pane.add(button2); pane.add(button3); return pane; } private static void initLookAndFeel() { String lookAndFeel = null; if (LOOKANDFEEL != null) { if (LOOKANDFEEL.equals("Metal")) { lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName(); } else if (LOOKANDFEEL.equals("System")) { lookAndFeel = UIManager.getSystemLookAndFeelClassName(); } else if (LOOKANDFEEL.equals("Motif")) { lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; } else if (LOOKANDFEEL.equals("GTK+")) { //new in 1.4.2 lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"; } else { System.err.println("Unexpected value of LOOKANDFEEL specified: " + LOOKANDFEEL); lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName(); } try { UIManager.setLookAndFeel(lookAndFeel); } catch (ClassNotFoundException e) { System.err.println("Couldn't find class for specified look and feel:" + lookAndFeel); System.err.println("Did you include the L&F library in the class path?"); System.err.println("Using the default look and feel."); } catch (UnsupportedLookAndFeelException e) { System.err.println("Can't use the specified look and feel (" + lookAndFeel + ") on this platform."); System.err.println("Using the default look and feel."); } catch (Exception e) { System.err.println("Couldn't get specified look and feel (" + lookAndFeel + "), for some reason."); System.err.println("Using the default look and feel."); e.printStackTrace(); } } } private static void createAndShowGUI() { initLookAndFeel(); //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); TextEditor app = new TextEditor(); Component contents = app.createMain(); frame.getContentPane().add(contents, BorderLayout.CENTER); //Display the window. frame.pack(); frame.setLocation(100,100); //put it at 100, 100 frame.setVisible(true); } public static void main(String[] args) { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }