Listing 4.1 JFrame with Label and Window Listener to Handle Closing the Frame (TJFrame.java) // Demonstrates the Swing frames. import javax.swing.*; import java.awt.*; import java.awt.event.*; class TJFrame extends JFrame { Container container = null; // 1. Create a Swing frame. public TJFrame(String title) { super(title); // set the title for the frame // 2. Get the content pane of the frame and // configure the frame using its content pane. container = this.getContentPane(); container.setBackground(Color.white); container.setForeground(Color.blue); // 3. Create a label and add it to the frame. JLabel label = new JLabel("This is a Swing frame", JLabel.CENTER); label.setFont(new Font("Sans", Font.PLAIN, 22)); container.add(label); // 4. Add the window listener to close the frame // and display it with the specified size. addWindowListener(new WindowEventHandler()); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); setSize(350, 200); // width=350, height=200 show(); // Display the frame } // 5. The main method. public static void main(String[] args) { TJFrame frame = new TJFrame("Swing Frame"); } // 6. The listener class to handle closing of the frame. class WindowEventHandler extends WindowAdapter { public void windowClosing(WindowEvent evt) { System.exit(0); } } } Listing 4.2 JLayeredPane with Buttons and Internal Frames (TJLayeredPane.java) // Demonstrates the Swing layered panes. import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class TJLayeredPane extends JFrame implements ActionListener { JButton button; JLayeredPane layeredPane; static int frameCount = 0; static final int xOffSet = 25; // each layer pane x offset static final int yOffSet = 25; // each layer pane y offset int numFrames = 1; // Define arrays for the layered pane constants and their names. Integer[] layerConstants = { JLayeredPane.DEFAULT_LAYER, JLayeredPane.PALETTE_LAYER, JLayeredPane.MODAL_LAYER, JLayeredPane.POPUP_LAYER, JLayeredPane.DRAG_LAYER }; String[] layerNames = { "Default Layer", "Palette Layer", "Modal Layer", "Popup Layer", "Drag Layer" }; Vector framesVector = new Vector(); // Constructor. public TJLayeredPane() { super("TJLayeredPane"); // Assign a title to the frame. // 1. Create a panel and assign grid layout with 3 row and 3 columns. JPanel panel = new JPanel(new GridLayout(3,3)); panel.setBorder( // Assign a title border around the panel. BorderFactory.createTitledBorder( "Click the Specific Button to Add Frames or Clear Frames")); // 2. Add the following control buttons to the panel. for (int i=0; i * */ import javax.swing.*; import java.awt.*; public class TJPanel extends JApplet { Container container = null; public void init() { // 1. Get a reference to the applet's content pane and // assigns a grid layout. container = getContentPane(); container.setLayout(new GridLayout(2,1,2,2)); // 2. Create four panels with grid layout and specified number // of rows and columns. The first two numbers in the // GridLayout constructors are the number of rows and columns // The next two numbers represent the horizontal and vertical // spacing between the components. JPanel panel1 = new JPanel(new GridLayout(1,3,2,2)); JPanel panel2 = new JPanel(new GridLayout(1,2,2,2)); JPanel panel3 = new JPanel(new GridLayout(2,1,2,2)); JPanel panel4 = new JPanel(new GridLayout(1,2,2,2)); // 3. Prepare panel1 with labels 1, 2, and 3. setLabel(panel1, "Label1"); setLabel(panel1, "Label2"); setLabel(panel1, "Label3"); // 4. Prepare panel2 with labels 4 and 5. setLabel(panel2, "Label4"); setLabel(panel2, "Label5"); // 5. Prepare panel3 with labels 6 and 7. setLabel(panel3, "Label6"); setLabel(panel3, "Label7"); // 6. Add panel2 and panel3 to panel4. panel4.add(panel2); panel4.add(panel3); // 7. Finally, add panel1 and panel4 to the content pane. container.add(panel1); container.add(panel4); } // 8. Supporting method to create and attach a label. public void setLabel(Container panel, String text) { JLabel label = new JLabel(text, JLabel.CENTER); label.setOpaque(true); label.setForeground(Color.white); label.setBackground(Color.gray); panel.add(label); } } Listing 4.5 JSplitPane with One Horizontal and One Vertical Split Pane (TJSplitPane.java) /* * * */ import javax.swing.*; import java.awt.*; public class TJSplitPane extends JApplet { static int HORIZSPLIT = JSplitPane.HORIZONTAL_SPLIT; static int VERTSPLIT = JSplitPane.VERTICAL_SPLIT; boolean continuousLayout = true; Icon icon1 = new ImageIcon("saravan2.gif"); Icon icon2 = new ImageIcon("saravan3.gif"); Icon icon3 = new ImageIcon("ISaravan.gif"); public void init() { // 1. Create a label to display icon1 and add it to a scroll pane. JLabel label1 = new JLabel(icon1); JScrollPane topLeftComp = new JScrollPane(label1); // 2. Create another lable to display icon2 and add it to another scroll pane. JLabel label2 = new JLabel(icon2); JScrollPane bottomLeftComp = new JScrollPane(label2); // 3. Create a third label to display icon3 and add it to one more scroll pane. JLabel label3 = new JLabel(icon3); JScrollPane rightComp = new JScrollPane(label3); // 4. Add the scroll panes displaying icon1 and icon2 to a split pane. JSplitPane splitPane1 = new JSplitPane(VERTSPLIT, continuousLayout, topLeftComp, bottomLeftComp); splitPane1.setOneTouchExpandable(true); // Provide a collapse/expand widget. splitPane1.setDividerSize(2); // Divider size. splitPane1.setDividerLocation(0.5); // Initial location of the divider. // 5. Add the previous split pane and the scroll pane displaying // icon3 to an outer split pane. JSplitPane splitPane2 = new JSplitPane(HORIZSPLIT, splitPane1, // left comp rightComp); splitPane2.setOneTouchExpandable(true); // Provide a collapse/expand widget. splitPane2.setDividerLocation(0.4); // divider size splitPane2.setDividerSize(2); // Initial location of the divider. // 6. Add the outer split pane to the content pane of the applet. getContentPane().add(splitPane2); } } Listing 4.6 JTabbedPane with Three Tabs (TJTabbedPane.java) // Demonstrates how to implement a tabbed pane. /* * * */ import javax.swing.*; import javax.swing.event.*; import java.awt.*; public class TJTabbedPane extends JApplet { JTabbedPane tabbedPane; String[] aircraft = {"Aircraft1.jpg", "Aircraft2.jpg", "Aircraft3.jpg"}; String[] tips = {"Cruise", "Banking", "Take-off"}; public void init() { // 1. Create a tabbed pane object. tabbedPane = new JTabbedPane(JTabbedPane.BOTTOM); tabbedPane.addChangeListener(new TabbedPaneListener()); // 2. Add tabs to the tabbed pane. Each tab displays // an aircraft. for (int i=0; i