Perhaps you used to use AWT to design a graphical user interface (GUI), how do you feel? Now that JAVA2 offers a swing pack that shows you a richer set of features, the interface you use to design it will be friendlier, so let's take a look.
Before you designed a small application to expand the applet (Java.applet.Applet); Now to use swing packs, you want to extend JApplet (Javax.swing.JApplet) Let's look at an example of an extended JApplet applet and then analyze the difference.
Cheng? as follows:
(JAppletTest.java)
import javax.swing.*;
import java.awt.*;
import java.event.*;
public class JAppletTest extends JApplet {
public void init() {
Container contentPane = getContentPane();
Icon icon = new ImageIcon("swing.gif", "A GIF on a swing");
JLabel label = new JLabel("Swing!", icon, SwingConstants.CENTER);
contentPane.add(label, BorderLayout.CENTER);
}
}
There are a few things to note:
1, the last line of the program is not necessary, because the JApplet class uses an instance of BorderLayout as the layout Manager for its content pane, The default constraint for BorderLayout is Borderlayout.center, so the last line of the program is not required.
2, the default layout manager of the applet is FlowLayout, and flowlayout default constraint is Flowlayout.left, which is one of the most significant differences.
3, JApplet is a container containing only one component, this component is an instance of JRootPane, and JRootPane contains a container called the content pane.
4, the content pane, which contains all the content related to a particular small application. That is, small applications must add all components to the content pane, rather than adding them directly to a small application.
5, we should not directly set the layout manager for small applications.
6, japplet instance can have a menu bar, it is specified by the Setjmenubar method, but the AWT small application can not.