GraphicsExampleFramepublic class GraphicsExampleFrame extends JFrame This class displays one or more GraphicsExample objects in a
Swing JFrame and a JTabbedPane |
Constructors Summary |
---|
public GraphicsExampleFrame(GraphicsExample[] examples)
super("GraphicsExampleFrame");
Container cpane = getContentPane(); // Set up the frame
cpane.setLayout(new BorderLayout());
final JTabbedPane tpane = new JTabbedPane(); // And the tabbed pane
cpane.add(tpane, BorderLayout.CENTER);
// Add a menubar
JMenuBar menubar = new JMenuBar(); // Create the menubar
this.setJMenuBar(menubar); // Add it to the frame
JMenu filemenu = new JMenu("File"); // Create a File menu
menubar.add(filemenu); // Add to the menubar
JMenuItem print = new JMenuItem("Print"); // Create a Print item
filemenu.add(print); // Add it to the menu
JMenuItem quit = new JMenuItem("Quit"); // Create a Quit item
filemenu.add(quit); // Add it to the menu
// Tell the Print menu item what to do when selected
print.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Get the currently displayed example, and call
// the print method (defined below)
print(examples[tpane.getSelectedIndex()]);
}
});
// Tell the Quit menu item what to do when selected
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { System.exit(0); }
});
// In addition to the Quit menu item, also handle window close events
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { System.exit(0); }
});
// Insert each of the example objects into the tabbed pane
for(int i = 0; i < examples.length; i++) {
GraphicsExample e = examples[i];
tpane.addTab(e.getName(), new GraphicsExamplePane(e));
}
|
Methods Summary |
---|
public static void | main(java.lang.String[] args)The main program. Use Java reflection to load and instantiate
the specified GraphicsExample classes, then create a
GraphicsExampleFrame to display them.
GraphicsExample[] examples = new GraphicsExample[args.length];
// Loop through the command line arguments
for(int i=0; i < args.length; i++) {
// The class name of the requested example
String classname = args[i];
// If no package is specified, assume it is in this package
if (classname.indexOf('.") == -1)
classname = "je3.graphics." + args[i];
// Try to instantiate the named GraphicsExample class
try {
Class exampleClass = Class.forName(classname);
examples[i] = (GraphicsExample) exampleClass.newInstance();
}
catch (ClassNotFoundException e) { // unknown class
System.err.println("Couldn't find example: " + classname);
System.exit(1);
}
catch (ClassCastException e) { // wrong type of class
System.err.println("Class " + classname +
" is not a GraphicsExample");
System.exit(1);
}
catch (Exception e) { // class doesn't have a public constructor
// catch InstantiationException, IllegalAccessException
System.err.println("Couldn't instantiate example: " +
classname);
System.exit(1);
}
}
// Now create a window to display the examples in, and make it visible
GraphicsExampleFrame f = new GraphicsExampleFrame(examples);
f.pack();
f.setVisible(true);
| public void | print(GraphicsExample example)This method is invoked by the Print menu item
// Start off by getting a printer job to do the printing
PrinterJob job = PrinterJob.getPrinterJob();
// Wrap the example in a Printable object (defined below)
// and tell the PrinterJob that we want to print it
job.setPrintable(new PrintableExample(example));
// Display the print dialog to the user
if (job.printDialog()) {
// If they didn't cancel it, then tell the job to start printing
try {
job.print();
}
catch(PrinterException e) {
System.out.println("Couldn't print: " + e.getMessage());
}
}
|
|