macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Building a Simple Java Application in Mac OS X
Pages: 1, 2, 3

Refactoring

Related Reading

Learning JavaLearning Java
By Pat Niemeyer & Jonathan Knudsen
Table of Contents
Index
Sample Chapter
Full Description
Read Online -- Safari

Before moving on, let's reorganize NineSquares.java so the functionality is split between two files. This process of improving existing working code is called refactoring. For more on this process check out Martin Fowler's Refactoring Web Site. For now, we aren't going to do anything fancy. We'll use inheritance to create a subclass of JFrame called a MainFrame that sets its default close operation and visibility when it is created.



All I want from NineSquares.java is that it create an instance of MainFrame that we can still call myFrame. Here's the code that you'll put in the file MainFrame.java.

package NineSquares;

import javax.swing.JFrame;

public class MainFrame extends JFrame{

  public MainFrame() {
    super("Nine Squares");
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
}

You'll notice that MainFrame extends JFrame. The first thing we do, therefore, inside of the MainFrame constructor (the method with the name MainFrame()) is call the JFrame constructor that takes a String as input. In our first version of the program, we did this directly with the call new JFrame("Nine Squares"). You can see that the constructor sets up the JFrame just as it did before. What's left is to modify the NineSquares.java file as follows.

package NineSquares;

public class NineSquares {
  public static void main( String args[]) {
    new MainFrame();
  }
}

There's almost nothing here. All that we do is create a new MainFrame() and then walk away. Notice that we don't even need to import the JFrame class because we are creating a MainFrame and MainFrame belongs to the same package as NineSquares.

Save your changes and then compile these files from the Terminal. Run the application exactly as before. This example is very trivial, however the change we just made illustrates an important point. We shouldn't have to make any changes to NineSquares.java again. Its job is well defined: It gets the application up and running by creating a special JFrame called a MainFrame. Similarly, all of the ways in which we have specialized JFrame are contained in the simple class MainFrame. Now we know where to go to add functionality and features.

Living color

Now we are going to add a panel to the MainFrame. In our first iteration, we'll start out with a blue panel and then we'll give the user the ability to choose the color of the panel. We'll create an extension of the JPanel class that is in the javax.swing package. Go to the JavaDocs and select JPanel.

You'll see that it inherits the setBackground() method from JComponent. Click on setBackground() and you'll see that this method takes a color as its argument. Follow the link by clicking on "Color" and you'll see that there are several ways of specifying a color. We'll start by using the constant Color.blue. Create a file called EachSquare.java and save it as before to the NineSquares directory. It should look like this.

package NineSquares;

import javax.swing.JPanel;
import java.awt.Color;

public class EachSquare extends JPanel {
  public EachSquare() {
      setBackground(Color.blue);
  }
}

By now you should be an old hand at this. We've specified that EachSquare is a subclass of JPanel and in the constructor, we've set the background color to blue. In addition, we've had to import the information about JPanel and Color and we've assigned EachSquare to belong to the NineSquares package. What's remains is for us to modify MainFrame.java to add a blue JPanel to itself. This is a bit more complicated with JFrame than it is for other components. You need to add the EachSquare object to the JFrame content pane and not directly to the JFrame. The new code looks like this.

package NineSquares;

import javax.swing.JFrame;

public class MainFrame extends JFrame{
  public MainFrame() {
    super("Nine Squares");
    getContentPane().add(new EachSquare());
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
}

The additional line is highlighted above. We created a new instance of EachSquare and added it to the content pane. Now when you compile these files and run the application, you should get the same little application window but now the background will be blue.

Pages: 1, 2, 3

Next Pagearrow