mport javax.swing.JFrame; /* * This is the main driver program. It simply creates a window and displays it. All of * the interesting code is in event response and window display. This tends to be a trend * in a lot of GUI systems. */ public class Driver { //The main class...entry point of code and whatnot...blah blah... public static void main(String args[]) { /* * A JFrame is actually an entire window. It's what's called a heavyweight component. * This is a fancy word that gets thrown around alot and isn't important. Basically, * heavyweight components tend to be the top level components, and can stand by * themselves. They also look different on different operating systems (a window in * Windows looks different than a window in UNIX, this is because the operating system * is responsible for creating top level windows.) Anyways...most of this is * irrelevent. In any GUI you want to try and mess with the JFrame as little as * possible. Ideally, if you have some complex GUI, you create a JFrame and then a * JPanel, you add the JPanel to the JFrame and everything else to the JPanel (you * can even add more JPanels to a JPanel). */ JFrame mainWindow = new JFrame(); /* * This creates a Canvas object, which I have defined in Canvas.java. Canvas extends * JPanel, so it can be used just like a JPanel, this allows us to add it to * mainWindow. A JPanel doesn't have any real looks of it's own, the main idea is that * it holds stuff in it. However, we can use this to our advantage, because since it * has no looks, we can just draw right on top of it. Another way to do this would be * to extend a JComponent. Every lightweight object extends from JComponent at some * point or another. A JComponent is just a blank square. A JPanel is just a * JComponent that can have stuff put inside it. */ Canvas mainCanvas = new Canvas(); /* * This actually adds the Canvas object (mainCanvas) to the JFrame (mainWindow) */ mainWindow.add(mainCanvas); /* * These 3 calls create and display a window. The pack() method is unique to JFrames * this method tells the JFrame to look at everything it contains and figure out how * big it needs to be. Since we set a preferred size in our Canvas object, the JFrame * will resize itself so it's at least that big. The setDefaultCloseOperation() is * easy to forget, but important. Setting it to JFrame.EXIT_ON_CLOSE (this is a good * example of when to use public static final int by the way (although Java might use * enums here...)) will tell the program to exit when this window is closed. Setting * it to JFrame.DISPOSE_ON_CLOSE will tell it to do the opposite, just discard the * window. The danger here is that your program might continue running without you * knowing. JFrame.DO_NOTHING_ON_CLOSE is fun if you want to spite your user. This * will make it so that clicking on the X does..."Nothing!!!" The final command, * setVisible(true) actually displays the window. If you want to hide your window * temporarily, you could do setVisible(false) somewhere else and then display it * again with setVisible(true). Anyways...these 3 commands tend to clump together. */ mainWindow.pack(); mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainWindow.setVisible(true); } }