Java Reference
In-Depth Information
// Main window for the Sketcher application
import javax.swing.*;
public class SketcherFrame extends JFrame {
// Constructor
public SketcherFrame(String title) {
setTitle(title);
// Set the window
title
setDefaultCloseOperation(EXIT_ON_CLOSE);
setJMenuBar(menuBar);
// Add the menu bar
to the window
JMenu fileMenu = new JMenu("File");
// Create File menu
JMenu elementMenu = new JMenu("Elements");
// Create Elements
menu
menuBar.add(fileMenu);
// Add the file menu
menuBar.add(elementMenu);
// Add the element
menu
}
private JMenuBar menuBar = new JMenuBar();
// Window menu bar
}
Directory "Sketcher 1"
Save this code as SketcherFrame.java in the Sketcher directory. Because JFrame is serializable, the
compiler expects SketcherFrame to be serializable too. The compiler issues a warning message that
there is no definition for serialVersionUID . Because you have no plans to serialize the SketcherFrame
class, you can ignore the warning. If you find getting the warning message each time you recompile irrit-
ating, you can suppress a particular compiler warning by using an annotation. You could add the follow-
ing annotation before the SketcherFrame class definition to suppress the serial warning:
@SuppressWarnings("serial")
The name of the warning that you want suppressed appears as a string between the parentheses. The
warnings that can be suppressed depends on your compiler. "fallthrough" suppresses messages about
missing break statements in a switch statement and "all" suppresses all compiler warning messages.
However, compiler warnings are very helpful in many instances so don't suppress them all.
Next, you can enter the code for the Sketcher class in a separate file:
Search WWH ::




Custom Search