Полезная информация кардшаринг

Exploring Java

Previous: 13.6 AWT Event SummaryChapter 14Next: 14.2 Text Components
 

14. Creating GUI Components

Contents:
Buttons and Labels
Text Components
Lists
Menus and Choices
PopupMenu
Checkbox and CheckboxGroup
ScrollPane and Scrollbars
Dialogs
Creating Custom Components

The previous chapter discussed a number of concepts, including how Java's user interface facility is put together and how the larger pieces work. You should understand what components and containers are, how you use them to create a display, what events are, how components use them to communicate with the rest of your application, and what layout managers are. In other words, we've covered a lot of material that's "good for you."

Now that we're through with the general concepts and background, we'll get to the fun stuff: how to do things with AWT. We will cover all the components that the AWT package supplies, how to use these components in applets and applications, and how to build your own components. We will have lots of code and lots of pretty examples to look at.

14.1 Buttons and Labels

We'll start with the simplest components: buttons and labels. Frankly, there isn't much to say about them. If you've seen one button, you've seen them all; and you've already seen buttons in the applets in Chapter 2 (HelloWeb3 and HelloWeb4). A button generates an ActionEvent when the user presses it. To receive these events, your program registers an ActionListener, which must implement the actionPerformed() method. The argument passed to actionPerformed() is the event itself.

There's one more thing worth saying about buttons, which applies to any component that generates an action event. Java lets us specify an "action command" for buttons (and other components, like menu items, that can generate action events). The action command is less interesting than it sounds. It is just a String that serves to identify the component that sent the event. By default, the action command of a Button is the same as the text of its label; it is included in action events, so you can use it to figure out which button an event came from. To get the action command from an action event, call the event's getActionCommand() method. The following code checks whether the user pressed the Yes button:

public void actionPerformed(ActionEvent e){
    if (e.getActionCommand().equals("Yes") {
        //the user pressed "Yes"; do something
        ...
    }
}

You can change the action command by calling the button's setActionCommand() method. The code below changes button b's action command to "confirm":

myButton.setActionCommand("confirm");

It's a good idea to get used to setting action commands explicitly because they prevent your code from breaking when you or some other developer "internationalizes" it. If you rely on the button's label, your code will stop working as soon as that label changes; a French user might see the label "Oui" rather than "Yes." By setting the action command, you eliminate one source of bugs; for example, the button myButton in the previous example will always generate the action command "confirm," regardless of what its label says.

There's even less to be said about Label components. They're just text strings housed in a component. There aren't any special events associated with labels; about all you can do is specify the text's alignment, which controls the position of the text within the area that the label occupies when displayed. The following code creates some labels with different alignments:

Label l1 = new Label("Lions"); //label with default alignment (CENTER)
Label l2 = new Label("Tigers", LEFT); //left aligned label
Label l3 = new Label (); //label with no text, default alignment
l3.setText("and Bears"); //assigning text to l3
l3.setAlignment(RIGHT); //setting l3's alignment

Now we've built three labels, using all three constructors and several of the class's methods. To display the labels, you only have to add them to a container by calling the container's add() method.

The other characteristics you might like to set on labels, like changing their font or color, are accomplished using the methods of the Component class. For example, you can call setFont() and setColor() on a label, as with any other component.

Given that labels are so simple, why do we need them at all? Why not just call drawString() whenever we need text? Remember that a Label is a Component. That's important; it means that labels have the normal complement of methods for setting fonts and colors that we mentioned above, as well as the ability to be managed sensibly by a layout manager. Therefore, they're much more flexible.

Speaking of layouts--if you use the setText() method to change the text of your label, its preferred size will probably change. So you should remember to call validate() on its container, to lay things out again:[1]

[1] At least as of Java 1.1, labels aren't very smart. Simply validating the container isn't enough. We had to explicitly invalidate the label first.

label.setText(...);
label.invalidate();
validate();  // on the container holding the label


Previous: 13.6 AWT Event SummaryExploring JavaNext: 14.2 Text Components
13.6 AWT Event SummaryBook Index14.2 Text Components

Other Books in this LibraryJava in a NutshellJava Language ReferenceJava AWTJava Fundamental ClassesExploring Java