With the label (jLabel), we can display unselectable text and image. We can give the label various characteristics such as multiple lines, multiple fonts or multiple colors by specifying html code in a label's text. Label are not opaque by default, turn its opacity property to "true", if you need to paint the label's background.
Assume: we have created file Frame01 as following archive 001. THE BEGINNING.
- Open Frame01.
- In Palette, subtab Swing Controls click Label;
- Add 4 labels in panel, hold SHIFT + click, when adding a last label release SHIFT, just click.
- Select fourth label, hold SHIFT + drag then surround four labels.
- In tab Properties, subtab Other Properties, property border, click button … If there isn't border property, so click one jLabel, then repeat selecting 4 jLabels.
- In dialog select Line Border, click OK.
- Change size for every label, then compose them as following:
- Right click jLabel1, select Edit Text, then type This is the first, hit ENTER.
- In tab Properties, subtab Properties, property horizontalAlignment, select CENTER.
- Property background, select yellow. Though 've been OK, the viewing doesn't change, cause opaque property is false.
- Subtab Other Properties, checked opaque property.
- Right click jLabel1, select Change Variable Name… named lb1, click OK.
- Click jLabel2, hit F2.
- In mode edit, type “This is the second with multiline and multi-style”. Hit ENTER.
- But, there are not multiple-line and not multiple-style. you 've to do that using html script.
- When jLabel2 selected, In tab Properties, subtab Properties, property text, click button …
- In dialog type following text:
<html>
This is the first line<br>
<center>This is the second line centered</center><br>
<span style="color: red; font-weight: bold;">This is the first style</span><br>
<big><font style="font-family: Arial; color: rgb(51, 102, 255);">This is the second style</font><big>
</html>
click OK.
- Copy image (.jpg) file with small size to folder …\Project01\src\package01
- Click jLabel3, in tab Properties, subtab Properties, property icon, click button …
- In dialog, choose option Image Within Project, select image file.Click OK.
- In tab Properties, follow this properties:
subtab property value Properties horizotalAlignment CENTER Other Properties text This is an image
horizontalTextPosition CENTER
verticalTextPosition TOP - To positioning text with image, use horizontalTextPosition and verticalTextPosition properties.
- To remove text, select jLabel3, hit F2, delete the text.
- Click jLabel4, hit F2.
Type:
<html><img src="file:///C:/globe.jpg" width="160" height="80"></html>
Hit ENTER.
MNEMONIC
- Add jTextField and put it aside of lb1 (jLabel1);
- Right click jTextField1, select Change Variable Name… named tx1, click OK.
- Click lb1 (jLabel), in tab Properties, subtab Properties, property labelFor, choose tx1.
- Property displayedMnemonic, value= T.
CHANGING PROPERTY AT RUNTIME
- Right click jButton1, choose Events, choose Action, choose actionPerformed.
- Change script:
JOptionPane.showMessageDialog(null, lb1.getText());
lb1.setText("We 've Test it!");
RUNNING PROGRAM
- Press F6, if lb1 doesn't change, close program.
- Click menu Run, select Clean and Build Project, or click toolbar
- After finished, press F6.
- Click jButton1, then click OK.
- Click ALT+T, focus will move to tx1.
Thank you, please try it. In the next blog we will discuss about jTextField, jTextArea and jPasswordField.