next up previous
Next: Adding a Menu Bar Up: The Frightened Freshers Guide Previous: Introduction

My First GUI Application

In java you can either use AWT or Swing in your GUIs. Swing is basically just AWT with extra fancy new things added to it and as a consequence most applications you use Swing in you will need to use a little bit of AWT.

First off, you do need to import a couple of classes from the Java API to the class you are building the GUI in.
\begin{lstlisting}
import java.awt.*;
import java.awt.event;
import javax.swing;
\end{lstlisting}

Now that you have the relevent import statements sorted out you can begin to build the GUI. The starting point for GUIs is the JFrame. The JFrame is basically just a window on which you add different components. The code below is taken from the lecture slides as an example of initialising a simple window and the comments explain what is going on


\begin{lstlisting}
private JFrame frame;
\par
private void makeFrame()
{
// Cre...
... // We do want to be able to see it!
frame.setVisible(true);
}
\end{lstlisting}

If you typed this out into BlueJ then you would probably not be too impressed with it, but its useful. Really it is! That function makes your first ever GUI, which consists of a window with a little label on that says I am a label. Using this same sort of code you will be able to add menus (Using JMenuBar, JMenu and JMenuItems) as well as other swing controls like scrollbars, listboxes and so on.

Okay, so now you have got yourself a window with a little label on. Great fun. Now how about something useful like a menu bar.


next up previous
Next: Adding a Menu Bar Up: The Frightened Freshers Guide Previous: Introduction
Tom Carlson 2006-02-16