1. No browser yet supports Java 1.2.
- But you can add Swing to Java 1.1.
- When experimenting, the easiest way to test applets is to use
the Java 1.2 appletviewer.
- The Java Plugin is an option also, mostly only useful for intranets.
- Next release of Netscape 5 promises Java 1.2 support.
2. Most usage is the same as with Applet
JApplet inherits from Applet
init, start, stop,
etc. unchanged
3. Main differences in use
- Components go in the "content pane", not directly in
the frame. Changing other properties (layout manager, background
color, etc.) also apply to the content pane. Access content pane via
getContentPane, or if you want to replace it with your
container (e.g. a JPanel), use setContentPane.
- Default layout manager is
BorderLayout
(like Frame and JFrame), not
FlowLayout (like Applet). This is really
the layout manager of the content pane.
- You get Java (Metal) look by default, so you have to explicitly
switch if you want native look.
- Do drawing in
paintComponent, not paint.
- Double buffering turned on by default.
4. JApplet Example: Source Code
This simple example shows the steps required to get what you would
have in the AWT if you had a simple applet whose init
method did nothing but drop three buttons into the window.
JAppletExample.java
(Download source code)
import java.awt.*;
import javax.swing.*;
public class JAppletExample extends JApplet {
public void init() {
WindowUtilities.setNativeLookAndFeel();
Container content = getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
content.add(new JButton("Button 1"));
content.add(new JButton("Button 2"));
content.add(new JButton("Button 3"));
}
}
Note: the setNativeLookAndFeel code is presented in
WindowUtilities.java.
4. JApplet Example: Result
This page is part of my
Quick Swing Tutorial for AWT Programmers. © 1999
Marty Hall. All
source code freely available for unrestricted use. Created for for
work in the Research
and Technology Development Center of the Johns Hopkins University
Applied Physics Lab, for courses in the Johns Hopkins Part-Time MS
Program in Computer Science, and for various industry seminars and
Java short courses.