Java Reference
In-Depth Information
Example 10•1: ShowComponent.java (continued)
import javax.swing.*;
import java.beans.*;
import java.lang.reflect.*;
import java.util.Vector;
/**
* This class is a program that uses reflection and JavaBeans introspection to
* create a set of named components, set named properties on those components,
* and display them. It allows the user to view the components using any
* installed look-and-feel. It is intended as a simple way to experiment with
* AWT and Swing components, and to view a number of the other examples
* developed in this chapter. It also demonstrates frames, menus, and the
* JTabbedPane component.
**/
public class ShowComponent {
// The main program
public static void main(String[] args) {
// Process the command line to get the components to display
Vector components = getComponentsFromArgs(args);
// Create a frame (a window) to display them in
JFrame frame = new JFrame("ShowComponent");
// Handle window close requests by exiting the VM
frame.addWindowListener(new WindowAdapter() { // Anonymous inner class
public void windowClosing(WindowEvent e) { System.exit(0); }
});
// Set up a menu system that allows the user to select the
// look-and-feel of the component from a list of installed PLAFs
JMenuBar menubar = new JMenuBar(); // Create a menubar
frame.setJMenuBar(menubar); // Tell the frame to display it
JMenu plafmenu = createPlafMenu(frame); // Create a menu
menubar.add(plafmenu);
// Add the menu to the menubar
// Create a JTabbedPane to display each of the components
JTabbedPane pane = new JTabbedPane();
// Now add each component as a tab of the tabbed pane
// Use the unqualified component classname as the tab text
for(int i = 0; i < components.size(); i++) {
Component c = (Component)components.elementAt(i);
String classname = c.getClass().getName();
String tabname = classname.substring(classname.lastIndexOf('.')+1);
pane.addTab(tabname, c);
}
// Add the tabbed pane to the frame. Note the call to getContentPane()
// This is required for JFrame, but not for most Swing components
frame.getContentPane().add(pane);
// Set the frame size and pop it up
frame.pack();
// Make frame as big as its kids need
frame.setVisible(true);
// Make the frame visible on the screen
// The main() method exits now but the Java VM keeps running because
// all AWT programs automatically start an event-handling thread.
}
Search WWH ::




Custom Search