This is a sample GUI starter. import 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); } } import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JPanel; /* * This class basically says, "let's make a JPanel, but change it a bit so that we can decide * what it looks like." We won't be writing any methods that JPanel doesn't already have (we * could if we wanted to...but in this case we don't need to.) The 'extends' keyword says, * "This class will have all the methods of JPanel", so even though this class looks blank, * we've got a lot here. */ public class Canvas extends JPanel { /* * The constructor is a special type of method. It A) Has the same name as the class and * B) Has no return type. The goal of the constructor is to setup all the class specific * variables. IMPORTANT THING THAT WILL COME BACK TO BITE YOU LATER: Whenever you use * the keyword 'extends', make sure you call the constructor of your parent...otherwise * you might not initialize some stuff your parent wants to. */ public Canvas() { /* * This command will call the parent's constructor. The keyword 'super' means * my immediate parent. As a fun (maybe) experiment, you could do a * System.out.println(super.getClass().getName()); and it will tell you what your * parent class is. */ super(); /* * This tells the window what it wants its size to be. Pretty simple command...this * is a 600 by 600 window. */ this.setPreferredSize(new Dimension(600,600)); /* * Read the comments on these later. But if we want this panel to listen for mouse * clicks and keyboard presses, we have to add listener classes. */ this.addMouseListener(new MouseClickHandler() /*A class defined below*/); } /* * The bulk of any drawing code will go here. This is the actual paint method and * determines what the JPanel looks like. A Graphics object is basically a big square * that's the size of your window. You can paint on this square kinda like you would on * say...an MSPaint window, only it's a bit tricker...because you have to use the g object. * To view the different options you have for painting you can either use Eclipse's auto * complete or go to the online API for the graphics object (Google Java 5 API Graphics) */ public void paint(Graphics g) { //This will draw an X across the middle of the screen. int oneFourthWidth = this.getWidth() / 4; int threeFourthsWidth = this.getWidth() / 4 * 3; int oneFourthHeight = this.getHeight() / 4; int threeFourthsHeight = this.getWidth() / 4 * 3; //Make the line red...this is a static constant in the Color class g.setColor(Color.RED); //Draws the line g.drawLine(oneFourthWidth, oneFourthHeight, threeFourthsWidth, threeFourthsHeight); g.drawLine(oneFourthWidth, threeFourthsHeight, threeFourthsWidth, oneFourthHeight); } /* * Umm...this one is a bit weird. This is what is called a subclass. TYPICALLY a single * file contains a single class. However, due to some design decisions at Java, any GUI * will end up containing a bunch of small special purpose classes (like this one) and so * they allow you to create what is called a subclass. This is kind of a private class * that can only be accessed by it's parent class (Canvas in this case) and that has access * to all of it's parent classes instance variables. That's the first half of the tricky * stuff, next up is the 'implements' keyword. This keyword means you're implementing an * interface. At first, interfaces seem stupid, but they make sense later...however I'd * rather explain in person then make these comments tooooo lengthy. What they are * is a list of methods. Any class that implements that interface HAS to have those * methods. */ public class MouseClickHandler implements MouseListener { /* * Notice the stub methods. Because it's an interface I have to implement every * method, even if I don't wanna do anything...so I can just make stubs. */ /* * I'm gonna print out the x and y position of the mouse (relative to the panel) * using the data given by the MouseEvent. */ public void mouseClicked(MouseEvent e) { System.out.print("User clicked a mouse button at "); System.out.println(e.getX() + "," + e.getY()); } public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } } //Future stuff you could do for fun! /* * The following objects are all part of the Java GUI stuff and can be added to a JPanel * * JButton <-- Use a sublcass implmenting the ActionListener interface to do stuff when * the button is pressed * JLabel * JSpinBox * JComboBox * JTextArea * JTextField * etc. * etc. * For a full listing go to http://java.sun.com/docs/books/tutorial/uiswing/components/components.html * * You can do animation with something called a TimerTask...I would suggest reading up on * the documentation. This basically executes a command every so many milliseconds. * * I dunno...other stuff...Java GUI is pretty easy UNTIL you try and mess with layouts. * I can explain them if you bug me sometime. */ }