import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// Swing Program Template for running as application or Applet
@SuppressWarnings("serial")
public class SwingTemplateApp
extends JPanel { // Name-constants to define the various dimensions
public static final int WINDOW_WIDTH = 300;
public static final int WINDOW_HEIGHT = 150;
// ......
// private variables of UI components
// ......
/** Constructor to setup the UI components */
public SwingTemplateApp() {
// "this" JPanel sets layout
// this.setLayout(new ....Layout());
// Allocate the UI components
// .....
// "this" JPanel adds components
// this.add(....)
// Source object adds listener
// .....
}
/** The entry main() method */
public static void main
(String[] args
) { // Run GUI codes in the Event-Dispatching thread for thread safety
public void run() {
frame.setContentPane(new SwingTemplateApp());
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE); frame.setTitle("......");
frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // or pack()
frame.setVisible(true);
}
});
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.lang.reflect.InvocationTargetException;
import javax.swing.JApplet;
// Swing Program Template for running as Applet
@SuppressWarnings("serial")
public class SwingTemplateApplet
extends JApplet {
/** init() to setup the UI components */
@Override
public void init() {
// Run GUI codes in the Event-Dispatching thread for thread safety
try {
public void run() {
setContentPane(new SwingTemplateApplet());
}
});
e.printStackTrace();
e.printStackTrace();
}
}
}