Many times the user of a sotware application performs functions by selecting items from a menu. In this article I give you an overview of the Java event model and how to create a Java program with a simple menu.
Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Your First Java Menu

Many times the user of a software application performs functions by selecting items from a menu. In this article I give you an overview of the Java event model and how to create a Java program with a simple menu.

Java is a programming language created by Sun Microsystems that lets you develop powerful windowed programs that can be run without modification on computers using the Windows, Linux, Macintosh, and most other operating systems. If you have not downloaded and configured Sun's free Java 2 Software Development kit, read my previous article, Getting Started as a Java Programmer.

When Java was released, it used a class library called the Abstract Window Toolkit (AWT). The AWT delegated the creation of graphical user interface (GUI) objects to the native operating system. The advantage of that was that Java programs had the same appearance as the other programs on the system.

The problem with that was that the same Java program looked different when run on different operating systems. To solve that problem, Sun created the swing class library. Using swing, a Java program looks the same on every operating system.

To create a program window, Java uses a component called a JFrame. A JFrame is a container, which means that it can contain other interface components such as buttons and text boxes. The program in this example will display a message in a Jframe.

It is possible to draw the message directly onto the JFrame, but that is not good programming practice. A JFrame is meant to be used as a container for other components. The component that you should draw output on is called a JPanel. The code shown below creates a JPanel class named MenuAppPanel.

class MenuAppPanel extends JPanel
{
   private String text;

   public MenuAppPanel()
   {
      text = "";
   }

   public void setText(String w)
   {
      text = w;
      repaint();
   }

   public void paintComponent(Graphics g)
   {
      super.paintComponent(g);
      g.drawString(text, 64, 64);
   }
}

The MenuAppPanel class defines a constructor that allocates a text string along with a method that sets the text contained in the string, and a method that draws the string on the MenuAppPanel.

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro


Java

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2024 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268