鍍金池/ 教程/ Java/ Swing JLabel
Swing JRadioButton
Swing Layout布局
Swing JTextField
Swing JLabel
Swing開(kāi)發(fā)環(huán)境安裝
Swing JComponent類
SWING ActionEvent事件類
Swing JScrollBar
Swing教程首頁(yè)
Swing JFileChooser
SWING ContainerEvent事件處理類
Swing AdjustmentListener接口
Swing WindowAdapter類
SWING ComponentListener接口
Swing JMenuBar類
Swing JPanel類及實(shí)例
Swing
SWING AdjustmentEvent事件處理
Swing JWindow類及實(shí)例
Swing介紹
Swing MouseMotionAdapter類
SWING WindowEvent事件類
Swing ItemListener接口
SWING MouseEvent事件類
Swing JMenu類
SWING KeyEvent事件類
Swing MouseMotionListener接口
Swing JProgressBar
SWING AWTEvent事件類
Swing JRadioButtonMenuItem類及例子
Swing KeyAdapter類
SWING ActionListener接口
Swing LayoutManager接口
SWING MouseMotionEvent事件處理
Swing Component類
Swing Container類
Swing CardLayout布局
Swing MouseAdapter類
Swing JSlider
Swing KeyListener接口
Swing FocusAdapter類
Swing JColorChooser
Swing ImageIcon
Swing JComboBox
Swing GroupLayout布局類
Swing JOptionPane
Swing GridBagLayout布局類
Swing JMenuItem類
Swing MouseListener Interface
Swing事件適配器
SWING ComponentEvent處理類
SWING PaintEvent事件類
Swing FlowLayout布局類
SWING Event事件類
Swing FocusListener接口
Swing控件
Swing JButton
Swing JCheckBox
Swing SpringLayout布局類
Swing容器
Swing事件處理
Swing事件監(jiān)聽(tīng)器
Swing Menu菜單類
Swing JList
Swing ContainerListener接口
Swing LayoutManager2接口
Swing JFrame類和實(shí)例
Swing JTextArea
SWING InputEvent事件類
Swing BorderLayout布局
Swing WindowListener接口
Swing JCheckboxMenuItem類及例子
Swing JSpinner
Swing GridLayout布局類
Swing JPopupMenu類及實(shí)例

Swing JLabel

JLabel類可以顯示文本,圖像,或兩者兼而有。標(biāo)簽的內(nèi)容是一致的,通過(guò)設(shè)置在其顯示區(qū)域中的垂直和水平位置。默認(rèn)情況下,標(biāo)簽在其顯示區(qū)域垂直居中。純文字標(biāo)簽的前沿對(duì)齊,默認(rèn)情況下,圖像標(biāo)簽,默認(rèn)情況下,水平居中。

類聲明

以下是聲明的javax.swing.JLabel類:

public class JLabel
   extends JComponent
      implements SwingConstants, Accessible

字段域

以下是javax.swing.JLabel類的字段:

  • protected Component labelFor

類構(gòu)造函數(shù)

S.N. 構(gòu)造函數(shù) & 描述
1 JLabel()
Creates a JLabel instance with no image and with an empty string for the title.
2 JLabel(Icon image) 
Creates a JLabel instance with the specified image.
3 JLabel(Icon image, int horizontalAlignment) 
Creates a JLabel instance with the specified image and horizontal alignment.
4 JLabel(String text) 
Creates a JLabel instance with the specified text.
5 JLabel(String text, Icon icon, int horizontalAlignment) 
Creates a JLabel instance with the specified text, image, and horizontal alignment.
6 JLabel(String text, int horizontalAlignment) 
Creates a JLabel instance with the specified text and horizontal alignment.

類方法

S.N. 方法 & 描述
1 protected int checkHorizontalKey(int key, String message) 
Verify that key is a legal value for the horizontalAlignment properties.
2 protected int checkVerticalKey(int key, String message) 
Verify that key is a legal value for the verticalAlignment or verticalTextPosition properties.
3 AccessibleContext getAccessibleContext() 
Get the AccessibleContext of this object.
4 Icon getDisabledIcon() 
Returns the icon used by the label when it's disabled.
5 int getDisplayedMnemonic() 
Return the keycode that indicates a mnemonic key.
6 int getDisplayedMnemonicIndex() 
Returns the character, as an index, that the look and feel should provide decoration for as representing the mnemonic character.
7 int getHorizontalAlignment() 
Returns the alignment of the label's contents along the X axis.
8 int getHorizontalTextPosition() 
Returns the horizontal position of the label's text, relative to its image.
9 Icon getIcon() 
Returns the graphic image (glyph, icon) that the label displays.
10 int getIconTextGap() 
Returns the amount of space between the text and the icon displayed in this label.
11 Component getLabelFor() 
Get the component this is labelling.
12 String getText() 
Returns the text string that the label displays.
13 LabelUI getUI() 
Returns the L&F object that renders this component.
14 String getUIClassID() 
Returns a string that specifies the name of the l&f class that renders this component.
15 int getVerticalAlignment() 
Returns the alignment of the label's contents along the Y axis.
16 int getVerticalTextPosition() 
Returns the vertical position of the label's text, relative to its image.
17 boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) 
This is overridden to return false if the current Icon's Image is not equal to the passed in Image img.
18 protected String paramString() 
Returns a string representation of this JLabel.
19 void setDisabledIcon(Icon disabledIcon) 
Set the icon to be displayed if this JLabel is "disabled" (JLabel.setEnabled(false)).
20 void setDisplayedMnemonic(char aChar) 
Specifies the displayedMnemonic as a char value.
21 void setDisplayedMnemonic(int key) 
Specify a keycode that indicates a mnemonic key.
22 void setDisplayedMnemonicIndex(int index) 
Provides a hint to the look and feel as to which character in the text should be decorated to represent the mnemonic.
23 void setHorizontalAlignment(int alignment) 
Sets the alignment of the label's contents along the X axis.
24 void setHorizontalTextPosition(int textPosition) 
Sets the horizontal position of the label's text, relative to its image.
25 void setIcon(Icon icon) 
Defines the icon this component will display.
26 void setIconTextGap(int iconTextGap) 
If both the icon and text properties are set, this property defines the space between them.
27 void setLabelFor(Component c) 
Set the component this is labelling.
28 void setText(String text) 
Defines the single line of text this component will display.
29 void setUI(LabelUI ui) 
Sets the L&F object that renders this component.
30 void setVerticalAlignment(int alignment) 
Sets the alignment of the label's contents along the Y axis.
31 void setVerticalTextPosition(int textPosition) 
Sets the vertical position of the label's text, relative to its image.
32 void updateUI() 
Resets the UI property to a value from the current look and feel.

方法繼承

這個(gè)類從以下類繼承的方法:

  • javax.swing.JComponent

  • java.awt.Container

  • java.awt.Component

  • java.lang.Object

JLabel 例子

選擇使用任何編輯器創(chuàng)建以下java程序在 D:/ > SWING > com > yiibai > gui >

SwingControlDemo.java
package com.yiibai.gui;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class SwingControlDemo {
    
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();      
      swingControlDemo.showLabelDemo();
   }

   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame