1 / 18

An Application Shell

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(

holland
Download Presentation

An Application Shell

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. An Application Shell using Swing

  2. 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); }

  3. 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");

  4. 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);

  5. 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); }

  6. 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)); }

  7. Step 5 // step 5 - "show" the window this.setVisible(true); • The constructor should show the window

  8. Step 6 this.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } } ); • and handle the window closing event.

  9. 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

  10. 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.

  11. 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.

  12. 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.

  13. 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);

  14. Step 10 • You will need a collection of images. Tooltips are the best way to assist the user with these images.

  15. 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); }

  16. Step 11 • A status bar gives a place to report progress. status = new StatusPanel(); contentPane.add("South", status); status.setText("This application is ready.");

  17. 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 } }

  18. 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);

More Related