Bonus Chapter: A Tour of the Swing GUI

The JLabel

The first GUI object that we are going to take a look at is the JLabel. This is basically an object that contains a string text that we can position anywhere on our application. Let's look at a simple example on how we can use the JLabel.

Code Listing 1: Using the JLabel

start example
import java.awt.*; import javax.swing.*;     public class JLabelExample extends JFrame {     public static void main(String[] argv)     {         JLabelExample mainApp = new JLabelExample();     }          public JLabelExample()     {         super("JLabel Example");         setBounds(0, 0, 300, 300);         getContentPane().setLayout(null);         setDefaultCloseOperation(EXIT_ON_CLOSE);                         // Using the label contructor to set the text...         label1 = new JLabel("This is the first label");         label1.setLocation(10, 10);         label1.setSize(label1.getPreferredSize());                  // Different coloured text...         label2 = new JLabel();         label2.setText("This is a coloured label");         label2.setLocation(10, 30);         label2.setSize(label2.getPreferredSize());         label2.setForeground(new Color(255, 0, 0));                  // Using setBounds instead of setLocation/setSize...         label3 = new JLabel();         label3.setText("Using setBounds");         label3.setBounds(10, 50, 200, 20);                  // Add the labels to the content pane         getContentPane().add(label1);         getContentPane().add(label2);         getContentPane().add(label3);                  setVisible(true);     }          JLabel label1;     JLabel label2;     JLabel label3; }
end example

When we execute the application code, we can see that three labels are displayed in the application frame. Here is a screen shot of the code output:


Figure 1: The JLabel example application

Now let's now look at the code and see how it works. For the first label, use the constructor for the JLabel class to set the text to display. This can be seen in the following line of code:

label1 = new JLabel("This is the first label");

Then, once the label is created and the text is set, set the size and location with the following two lines of code:

label1.setLocation(10, 10); label1.setSize(label1.getPreferredSize());

Notice here how we set the size of the text to the "preferred size" of the label. This ensures that the label is large enough to display all the text.

If we look at the second label that we have added, we can see that we can leave the constructor with no parameters and then specify the label's text by calling the setText method. This can be seen in the following line of code:

label2.setText("This is a coloured label");

Also, with the second label we have changed the foreground color to red by using the setForeground method. This again can be seen in the following line of code:

label2.setForeground(new Color(255, 0, 0)); 

With our third and final label, we have specified the location and size of the label object by utilizing the setBounds method to manually set the width and height. Although this really is not the most practical option for a JLabel object, it is highly useful with the more complex GUI objects, as we will see later in this chapter. The use of the setBounds method can be seen in the following line of code:

label3.setBounds(10, 50, 200, 20);

Once we have set up all of our JLabel objects as required, we can then add them to the content pane of the application's frame. This is accomplished using the following three lines of code:

getContentPane().add(label1); getContentPane().add(label2); getContentPane().add(label3);
Note 

When using positioning and sizing methods (such as setBounds) the layout manager, which is used to automatically position GUI objects, must be set to null (using the setLayout(null) method, which can be seen in the previous code example). Otherwise, it will override the positions and sizes that you set. The main use of the layout manager is to handle GUI objects in a resizable application or component and is beyond the scope of this book.



Java 1.4 Game Programming
Java 1.4 Game Programming (Wordware Game and Graphics Library)
ISBN: 1556229631
EAN: 2147483647
Year: 2003
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net