180 likes | 284 Views
An Application Shell. using Swing. Step 1. The main will set a look and feel and create the application shell. try { // set Java Look and Feel MetalLookAndFeel.setCurrentTheme( new javax.swing.plaf.metal.DefaultMetalTheme()); UIManager.setLookAndFeel(
E N D
An Application Shell using Swing
Step 1 • The main will set a look and feel and create the application shell. try { // set Java Look and Feel MetalLookAndFeel.setCurrentTheme( new javax.swing.plaf.metal.DefaultMetalTheme()); UIManager.setLookAndFeel( "javax.swing.plaf.metal.MetalLookAndFeel"); new ApplicationShell();} catch (Exception e){ System.out.println( "GUI build error. \nReason:" + e); System.exit(1); }
Step 2 • Write the application shell. The shell constructor should call its parent’s constructor (JFame) to set name. public class ApplicationShell extends JFrame { public ApplicationShell() { // step 2 - call parent constructor super("Application Shell");
Steps 3 & 4 • The constructor should set the size of the frame and center itself in the frame’s layout. • These methods are not JFrame methods but are defined in the following slides. setDesktopSize(this, 80, 80); centerOnScreen(this);
Step 3 // step 3 - percentage size a window public static void setDesktopSize( JFrame frame, int wPerc, int hPerc) { Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); int iWidth = screen.width * wPerc / 100; int iHeight = screen.height * hPerc / 100; frame.setSize(iWidth, iHeight); }
Step 4 public static void centerOnScreen(JFrame frame) { Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); Dimension window = frame.getSize(); int iCenterX = screen.width / 2; int iCenterY = screen.height / 2; frame.setLocation( iCenterX - (window.width / 2), iCenterY - (window.height / 2)); }
Step 5 // step 5 - "show" the window this.setVisible(true); • The constructor should show the window
Step 6 this.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } } ); • and handle the window closing event.
Step 7 Action [] actions = { new NewAction(this), new OpenAction(), new SaveAction(), new ExitAction(), new CopyAction(), new CutAction(), new PasteAction(), new NormalThemeAction(this), new PresentationThemeAction(this), new AboutAction() new HelpAction(), }; • The shell will need callbacks for a menu bar’s action events
Step 8 Hashtable commands = new Hashtable(); for (int i=0; i < actions.length; i++) commands.put( actions[i].getValue(Action.NAME), actions[i]); mBar = buildMenus(commands); // class method this.setJMenuBar(mBar); • The constructor will build the menues.
Step 8 private JMenuBar buildMenus(Hashtable commands) { JMenuBar bar = new JMenuBar(); // Create File Menu JMenu fileMenu = new JMenu("File"); addMenuItem(fileMenu,"New",commands); addMenuItem(fileMenu,"Open",commands); //etc.
Step 9 • If themes are provided, modifications to suit the application may be needed. class PresentationMetalTheme extends DefaultMetalTheme { public String getName() { return "Presentation"; } private final FontUIResource controlFont = new FontUIResource("Dialog", Font.BOLD, 18); //etc.
Step 10 • Toolbars are now a popular addition to application shells. Container contentPane = this.getContentPane(); contentPane.setLayout(new BorderLayout(2,2)); tBar = buildToolBar(commands); contentPane.add("North", tBar);
Step 10 • You will need a collection of images. Tooltips are the best way to assist the user with these images.
Step 10 private void addButton(JToolBar tBar, String cmd, Hashtable commands) { URL loc = this.getClass().getResource( "images"+ File.separator+(cmd.toLowerCase()) + ".gif"); ImageIcon ic = new ImageIcon(loc); JButton aButton = new JButton(ic); aButton.addActionListener((Action)commands.get(cmd)); // added 7/3/99 aButton.setToolTipText(cmd.toLowerCase()); tBar.add(aButton); }
Step 11 • A status bar gives a place to report progress. status = new StatusPanel(); contentPane.add("South", status); status.setText("This application is ready.");
Step 11 public StatusPanel(JComponent secondBox, JComponent thirdBox) { this.setLayout(new BorderLayout(2,2)); JPanel eastPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,2,0)); this.setBorder(new SoftBevelBorder(BevelBorder.RAISED)); statusMessage = new JTextField(); statusMessage.setEditable(false); statusMessage.setBorder( new EtchedBorder(EtchedBorder.LOWERED)); this.add("Center", statusMessage); //etc } }
Step 12 • Create a DesktopPane to allow a Multiple-Document Interface. This allows you to add JInternalFrames to the desktop. desktop = new JDesktopPane(); contentPane.add("Center", desktop);