|
|
|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98
Week 2 At a Glance
- Application Controls
- Dialogs
- Fonts and Texts
- Drawing
- Images
- Image Processing
- Animation
Day 8 Application Controls
Theres a big difference between adding controls to applets and adding controls to applications. When you add controls to applets, you have to manually type the code, figure out the size, and adjust the properties of the controls. In applications, theres a toolbox that already contains all the controls. You simply drag these controls onto your form, and the code for the control is automatically added to your program. You can also edit the properties of these application controls very easily with the Properties window.
Today, well cover these topics:
- Label, Edit, and RichEdit controls
- Button controls, including CheckBox and RadioButton controls
- ComboBox, ListBox, and CheckedListBox controls
- ListView and TreeView controls
Well talk about each of these controls, and then well group them and create some sample programs to demonstrate how to use them.
Label, Edit, and RichEdit Controls
These three controls are grouped together because they all deal with text presentation to users and text input from users.
Label Control
Label controls are encapsulated by the Label class. These controls display a string of text that the user cannot edit. You can use a label to display status information about your program. You can also use a Label control to identify a control that doesnt itself have a built-in label. When a Label control gets the focus, it passes that focus on to the next control in the tab order. Therefore, a Label control never actually gets and keeps the focus. Heres the icon from the toolbox that represents a Label control.
To use a Label control, simply click on it in the Toolbox window and drag it over to your form. When its on your form, you can resize it and move it around. When a Label control is on the form and its selected, youll notice that the Properties window shows all the properties for that label. At least half of these properties are stock properties that pretty much come with any control. An example of two stock properties that most controls have is backColor and foreColor. The one property youll end up changing most is the font property. If you click in the Edit field of the font property, youll see a small button with an ellipsis come up. If you click this, it brings up the file selector dialog box, which allows you to set the font for this control. The textAlignment property is also very important. To change the text thats in the label, you edit the text property. One of the most interesting properties Ive found is the cursor property. This allows you to set the cursor type that the mouse cursor will change to when the mouse enters the area that the label occupies.
When you first change the label, go back to the source code and look for the declaration of your label. Its in the gray area that you cant edit, but you can view it. The declaration for your label should be right above the initForm() method. The next line of source code shows the declaration of your first label when you add it:
Label label1 = new Label();
Other source code must also be added for this label. A label with default properties has five methods added: the setLocation(), setSize(), setTabIndex(), setTabStop(), and setText() methods. However, these wont be added to your source code until you close the Form window and reopen it. Thats the way it works! So if you add a label (or any other control, for that matter) to your form and you dont see source code before the control, just close the window and reopen it. Then watch your source code magically appear. The following example shows the source code that was added after I closed my form and opened it again:
label1.setLocation(new Point(32, 32));
label1.setSize(new Point(176, 48));
label1.setTabIndex(0);
label1.setTabStop(false);
label1.setText("label1");
The Label class has methods that match the label properties you can see in the Properties window. For instance, theres a getAutoSize() method, which gets the Boolean value indicating whether the size of the control automatically adjusts and the fonts change. That matches the autoSize property that youll see in the property window when a label is selected. So somewhere in the Label class you can see labels that match all the properties. If, however, you dont see a method in the Label class that matches the property, you might have to check the control superclass that has methods that the Label and other controls inherit.
Many events are available to Label controls. In the Properties window, click on the icon that looks like a lightning bolt.
When you click the Events button in the Properties window, you see a new list. All the properties available to that control are shown. To add a handler for the event you want, simply double-click on the event name, and the event is added to your source code.
Lets try a simple experiment. Add a mouseEnter() event and a mouseLeave() event. Youll immediately see the source code for both of them in your form source code. In the mouseEnter() event handler method, well set the foreground color of the label to aqua. In the mouseLeave() event handler method, well set it back to black, which is the default color. The following source code shows both examples:
private void label1_mouseEnter(Object source, Event e)
{
label1.setForeColor( Color.AQUA );
}
private void label1_mouseLeave(Object source, Event e)
{
label1.setForeColor( Color.BLACK );
}
Go ahead and compile and run the program. As you move the mouse into the label area, the text changes to an aqua color. As you move the mouse outside the label area, the text changes to black.
|