How to Draw Multiple Lines on One Component Java

Now that you have familiarized yourself with the GUI builder's interface, it's time to begin developing the UI of our ContactEditor application. In this section we'll take a look at using the IDE's Palette to add the various GUI components that we need to our form.

Thanks to the IDE's Free Design paradigm, you no longer have to struggle with layout managers to control the size and position of the components within your containers. All you need to do is drag and drop the components you need to your GUI form as shown in the illustrations that follow.

Adding Components: The Basics

Though the IDE's GUI Builder simplifies the process of creating Java GUIs, it is often helpful to sketch out the way you want your interface to look before beginning to lay it out. Many interface designers consider this a "best practice" technique, however, for the purposes of this tutorial you can simply peek at how our completed form should look by jumping ahead to the Previewing your GUI section.

Since we've already added a JFrame as our form's top-level container, the next step is to add a couple of JPanels which will enable us to cluster the components of our UI using titled borders. Refer to the following illustrations and notice the IDE's "drag and drop" behavior when accomplishing this.

To add a JPanel:

  1. In the Palette window, select the Panel component from the Swing Containers category by clicking and releasing the mouse button.

  2. Move the cursor to the upper left corner of the form in the GUI Builder. When the component is located near the container's top and left edges, horizontal and vertical alignment guidelines appear indicating the preferred margins. Click in the form to place the JPanel in this location.

The JPanel component appears in the ContactEditorUI form with orange highlighting signifying that it is selected. After releasing the mouse button, small indicators appear to show the component's anchoring relationships and a corresponding JPanel node is displayed in the Navigator window, as shown in the following illustration.

02 add panels 1 small

Next, we need to resize the JPanel to make room for the components we'll place within it a little later, but let's take a minute to point out another of the GUI Builder's visualization features first. In order to do this we need to deselect the JPanel we just added. Because we haven't added a title border yet, the panel disappears. Notice, however, that when you pass the cursor over the JPanel, its edges change to light gray so that its position can be clearly seen. You need only to click anywhere within the component to reselect it and cause the resize handles and anchoring indicators to reappear.

To resize the JPanel:

  1. Select the JPanel you just added. The small square resize handles reappear around the component's perimeter.

  2. Click and hold the resize handle on the right edge of the JPanel and drag until the dotted alignment guideline appears near the form's edge.

  3. Release the resize handle to resize the component.

The JPanel component is extended to span between the container's left and right margins in accordance with the recommended offset, as shown in the following illustration.

02 add panels 2 small

Now that we've added a panel to contain our UI's Name information, we need to repeat the process to add another directly below the first for the E-mail information. Referring to the following illustrations, repeat the previous two tasks, paying attention to the GUI Builder's suggested positioning. Notice that the suggested vertical spacing between the two JPanels is much narrower than that at the edges. Once you have added the second JPanel, resize it such that it fills the form's remaining vertical space.

02 add panels 3 small

02 add panels 4 small

02 add panels 5 small

Because we want to visually distinguish the functions in the upper and lower sections of our GUI, we need to add a border and title to each JPanel. First we'll accomplish this using the Properties window and then we'll try it using the pop-up menu.

To add title borders to the JPanels:

  1. Select the top JPanel in the GUI Builder.

  2. In the Properties window, click the ellipsis button (…​) next to the Border property.

  3. In the JPanel Border editor that appears, select the TitledBorder node in the Available Borders pane.

  4. In the Properties pane below, enter Name for the Title property.

  5. Click the ellipsis (…​) next to the Font property, select Bold for the Font Style, and enter 12 for the Size. Click OK to exit the dialogs.

  6. Select the bottom JPanel and repeat steps 2 through 5, but this time right-click the JPanel and access the Properties window using the pop-up menu. Enter E-mail for the Title property.

Titled borders are added to both JPanel components.

02 add borders small

Adding Individual Components to the Form

Now we need to start adding the components that will present the actual contact information in our contact list. In this task we'll add four JTextFields that will display the contact information and the JLabels that will describe them. While accomplishing this, notice the horizontal and vertical guidelines that the GUI Builder displays, suggesting the preferred component spacing as defined by your operating system's look and feel. This ensures that your GUI is automatically rendered respecting the target operating system's look and feel at runtime.

To add a JLabel to the form:

  1. In the Palette window, select the Label component from the Swing Controls category.

  2. Move the cursor over the Name JPanel we added earlier. When the guidelines appear indicating that the JLabel is positioned in the top left corner of the JPanel with a small margin at the top and left edges, click to place the label.

The JLabel is added to the form and a corresponding node representing the component is added to the Inspector window.

Before going further, we need to edit the display text of the JLabel we just added. Though you can edit component display text at any point, the easiest way is to do this as you add them.

To edit the display text of a JLabel:

  1. Double-click the JLabel to select its display text.

  2. Type First Name: and press Enter.

The JLabel's new name is displayed and the component's width adjusts as a result of the edit.

Now we'll add a JTextField so we can get a glimpse of the GUI Builder's baseline alignment feature.

To add a JTextField to the form:

  1. In the Palette window, select the Text Field component from the Swing Controls category.

  2. Move the cursor immediately to the right of the First Name: JLabel we just added. When the horizontal guideline appears indicating that the JTextField's baseline is aligned with that of the JLabel and the spacing between the two components is suggested with a vertical guideline, click to position the JTextField.

The JTextField snaps into position in the form aligned with the JLabel's baseline, as shown in the following illustration. Notice that the JLabel shifted downward slightly in order to align with the taller text field's baseline. As usual, a node representing the component is added to the Navigator window.

03 indy add 1

Before proceeding further, we need to add an additional JLabel and JTextField immediately to the right of those we just added, as shown in the following illustration. This time enter Last Name: as the JLabel's display text and leave the JTextFields' placeholder text as it is for now.

03 indy add 2

To resize a JTextField:

  1. Select the JTextField we just added to the right of the Last Name: JLabel.

  2. Drag the JTextField's right edge resize handle toward the right edge of the enclosing JPanel.

  3. When the vertical alignment guidelines appear suggesting the margin between the text field and right edge of the JPanel, release the mouse button to resize the JTextField.

The JTextField's right edge snaps into alignment with the JPanel's recommended edge margin, as shown in the following illustration.

03 indy add 3

Adding Multiple Components to the Form

Now we'll add the Title: and Nickname: JLabels that describe two JTextFields that we're going to add in a minute. We'll drag and drop the components while pressing the Shift key, to quickly add them to the form. While accomplishing this, again notice that the GUI Builder displays horizontal and vertical guidelines suggesting the preferred component spacing.

To add multiple JLabels to the form:

  1. In the Palette window, select the Label component from the Swing Controls category by clicking and releasing the mouse button.

  2. Move the cursor over the form directly below the First Name: JLabel we added earlier. When the guidelines appear indicating that the new JLabel's left edge is aligned with that of the JLabel above and a small margin exists between them, shift-click to place the first JLabel.

  3. While still pressing the Shift key, place another JLabel immediately to the right of the first. Make certain to release the Shift key prior to positioning the second JLabel. If you forget to release the Shift key prior to positioning the last JLabel, simply press the Escape key.

The JLabels are added to the form creating a second row, as shown in the following illustration. Nodes representing each component are added to the Navigator window.

04 multi add 1

Before moving on, we need to edit the JLabels' name so that we'll be able to see the effect of the alignments we'll set later.

To edit the display text of JLabels:

  1. Double-click the first JLabel to select its display text.

  2. Type Title: and press Enter.

  3. Repeat steps 1 and 2, entering Nickname: for the second JLabel's name property.

The JLabels' new names are displayed in the form and are shifted as a result of their edited widths, as shown in the following illustration.

04 multi add 2

Inserting Components

Often it is necessary to add a component between components that are already placed in a form. Whenever you add a component between two existing components, the GUI Builder automatically shifts them to make room for the new component. To demonstrate this, we'll insert a JTextField between the JLabels we added previously, as shown in the following two illustrations.

To insert a JTextField between two JLabels:

  1. In the Palette window, select the Text Field component from the Swing Controls category.

  2. Move the cursor over the Title: and Nickname: JLabels on the second row such that the JTextField overlaps both and is aligned to their baselines. If you encounter difficulty positioning the new text field, you can snap it to the left guideline of the Nickname JLabel as shown in the first image below.

  3. Click to place the JTextField between the Title: and Nickname: JLabels.

The JTextField snaps into position between the two JLabels. The rightmost JLabel shifts toward the right of the JTextField to accommodate the suggested horizontal offset.

05 insert 1

05 insert 2

We still need to add one additional JTextField to the form that will display each contact's nickname on the right side of the form.

To add a JTextField:

  1. In the Palette window, select the Text Field component from the Swing category.

  2. Move the cursor to the right of the Nickname label and click to place the text field.

The JTextField snaps into position next to the JLabel on its left.

To resize a JTextField:

  1. Drag the resize handles of the Nickname: label's JTextField you added in the previous task toward the right of the enclosing JPanel.

  2. When the vertical alignment guidelines appear suggesting the margin between the text field and JPanel edges, release the mouse button to resize the JTextField.

The JTextField's right edge snaps into alignment with the JPanel's recommended edge margin and the GUI Builder infers the appropriate resizing behavior.

  1. Press Ctrl-S to save the file.

How to Draw Multiple Lines on One Component Java

Source: https://netbeans.apache.org/kb/docs/java/quickstart-gui.html

0 Response to "How to Draw Multiple Lines on One Component Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel