Sometimes we don't want to put our graphical applications in an Applet. There are many things that we can use instead including frames. Frames are specially designed to hold components - buttons, scroll bars etc. They can also show drawing and graphics components with the buttons and widgets. That can be very useful.
Here is the basic code for a JFrame:
import java.awt.event.*; import javax.swing.*;
public class FStarter extends JPanel
{
public static void main (String [] args)
{
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated (true);
//Create and set up the window.
JFrame frame = new JFrame ("Frame Demo");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new FStarter ();
newContentPane.setOpaque (true); //content panes must be opaque
frame.setContentPane (newContentPane);
frame.setSize (300, 150);
//Display the window.
frame.setVisible (true);
}
}
The only parts that you need to change are highlighted:
| FStarter extends JPanel | FStarter is the name of your class, pick something appropriate |
| new JFrame ("Frame Demo"); | "Frame Demo" is the name that will appear on the window. |
| new FStarter (); | Again, replace FStarter with the name of your class |
| frame.setSize (300, 150); | Replace with the dimensions that you want your frame to have. |
To add widgets and things to your JFrame, you need to make a series of JPanels and add things accordingly. Instead of making things Applets, make them JPanels. In the constructor, create and make widgets as you would in the init method of an Applet. You can add an actionListener method just as you would in an Applet.
Below is a more complex JFrame and the 3 classes that were used to set it up.
| import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FrameSample extends JPanel
public FrameSample () public static void main (String [] args) //Create and set up the window. //Create and set up the content pane. //Display the window. |
import java.awt.*; public class PartA extends JPanel implements ActionListener public PartA () public void actionPerformed (ActionEvent e) |
import java.awt.*; import javax.swing.*; import java.awt.event.*;
public PartB ()
|
| import java.awt.*; import java.awt.event.*; import javax.swing.*; |
Libraries |
| public class FrameSample extends JPanel | Class Declarations, note extends JPanel. (Add implements ActionListener if you want buttons) |
| PartA A; | Local (instance variables). The Buttons for that part. |
| public FrameSample () | The Constructors. Make all of the widgets and add them. |
| public static void main (String [] args) | In the main class, make the frame using the basic frame code. |
| public void actionPerformed (ActionEvent e) | In the other classes (and in the main one too if you want) handle the events using actionPerformed. |
You can set the position of the JFrame on the screen like this:
frame.setLocation(25,300) //where 25,300 is the top right corner
You can also set the user's cursor on the JFrame like this:
frame.setCursor( Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
These are the cursor options:
CROSSHAIR_CURSOR, DEFAULT_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, ICONIFIED, MAXIMIZED_BOTH, MAXIMIZED_HORIZ, MAXIMIZED_VERT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NORMAL, NW_RESIZE_CURSOR, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, W_RESIZE_CURSOR, WAIT_CURSOR
You can make your own cursor too:
Toolkit.createCustomCursor(java.awt.Image, java.awt.Point, java.lang.String)
More details here.