Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

A Java Skeleton Code Generator


July 1999/A Java Skeleton Code Generator/Figure 4

Figure 4: A GUI/applet skeleton created by function generateAppletGUI

import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class j12swingappguimenu extends JApplet {

 public static void main(String args[]) {
  j12swingappguimenu app = new j12swingappguimenu();
  app.init();
  app.start();
 }

 public void init() {
  AppletContext ac = null;
  try{ac = getAppletContext();}
  catch(NullPointerException npe){}
  new j12swingappguimenuFrame(ac);
 }
}

class j12swingappguimenuFrame extends JFrame // broken to fit 
    implements ActionListener {              // on page
 JMenuBar mb = new JMenuBar();
 JMenu file = new JMenu("File");
 JMenu edit = new JMenu("Edit");
 JMenu view = new JMenu("View");
 JMenu help = new JMenu("Help");
 JMenuItem fileOpen = new JMenuItem("Open...");
 JSeparator separator = new JSeparator();
 JMenuItem fileSaveAs = new JMenuItem("Save As...");
 JMenuItem editCut = new JMenuItem("Cut");
 JMenuItem editCopy = new JMenuItem("Copy");
 JMenuItem editPaste = new JMenuItem("Paste");
 JMenuItem helpAbout = new JMenuItem("About...");
 AppletContext ac;

 j12swingappguimenuFrame(AppletContext ac) {
  super();
  this.ac = ac;

  /* Components should be added to the container's 
     content pane */
  Container cp = getContentPane();

  /* Add menu items to menus */
  file.add(fileOpen);
  file.add(separator);
  file.add(fileSaveAs);
  edit.add(editCut);
  edit.add(editCopy);
  edit.add(editPaste);
  help.add(helpAbout);

  /* Add menus to menubar */
  mb.add(file);
  mb.add(edit);
  mb.add(view);
  mb.add(help);

  /* Set menubar */
  setJMenuBar(mb);

  /* Add the action listeners */
  fileOpen.addActionListener(this);
  fileSaveAs.addActionListener(this);
  editCut.addActionListener(this);
  editCopy.addActionListener(this);
  editPaste.addActionListener(this);
  helpAbout.addActionListener(this);

  /* Add the window listener */
  addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent evt) {
    dispose(); if (j12swingappguimenuFrame.this.ac == null) 
                  System.exit(0);}}); // broken to fit on page

  /* Size the frame */
  setSize(200,200);

  /* Center the frame */
  Dimension screenDim = // broken to fit on page
    Toolkit.getDefaultToolkit().getScreenSize();
  Rectangle frameDim = getBounds();
  setLocation((screenDim.width - frameDim.width) / 2, // broken
    (screenDim.height - frameDim.height) / 2); // to fit on page

  /* Show the frame */
  setVisible(true);
 }

 public void actionPerformed(ActionEvent evt) {
  Object obj = evt.getSource();

  if (obj == fileOpen);
  else if (obj == fileSaveAs);
  else if (obj == editCut);
  else if (obj == editCopy);
  else if (obj == editPaste);
  else if (obj == helpAbout);
 }
}


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.