At Features Provided by JApplet it also shows how to add components to the Content Pane
Basically Applets are not much different from applications. In fact, mostly the only difference is how they are started and that for an application,
if you want a GUI, you need to instantiate the Frame (or JFrame) to put things on.
Note how example JApplet code shows two methods (the main/init and the createAndShowGUI()/createGUI()) illustrating that starting JApplets are (can be/should be) very similar to starting applications:
Swing application HelloWorld
See the applications main method
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
Now the JApplet's init
public void init() {
// loadAppletParameters(); if there are any parameters being given through the .html page
//Execute a job on the event-dispatching thread:
//creating this applet's GUI.
try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}
}
The invokeLater [Da Doo Ron Ron] method is not appropriate for some JApplet's because it could
allow init to return before initialization is complete, which could cause applet problems that are difficult to debug.
OK, now their building of the GUI:
The applications createAndShowGUI() method
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add the ubiquitous "Hello World" label.
JLabel label = new JLabel("Hello World");
// could do a label.setPreferredSize(new Dimension(175, 100));
frame.getContentPane().add(label);
//Display the window.
frame.pack();
frame.setVisible(true);
}
Here is another for a class LabelDemo.java (Labels) which extends JPanel. See how it makes a JFrame, then a LabelDemo (which is a JPanel), then gives the frame the ContentPane of this JPanel. LabelDemo's constructor adds all the components to the JPanel
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("LabelDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
LabelDemo newContentPane = new LabelDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
Now the JApplet's createGUI()
private void createGUI() {
JLabel label = new JLabel(
"You are successfully running a Swing applet!");
label.setHorizontalAlignment(JLabel.CENTER);
label.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black));
getContentPane().add(label, BorderLayout.CENTER);
}
Discuss createAndShowGUI() for applications vs. createGUI() for applets (remember - they "show" on browsers via .html pages)
Note that if the applications and applets use any event-driven aspects, whether using Swing or not will probably need to import both
java.awt.*
java.awt.event.*
A bit more complex IconDemoApp (Icon Demo)
and application
In fact, here is the tutorials Table of Examples