- Swing 教程
- Swing - 首頁
- Swing - 概述
- Swing - 環境
- Swing - 控制元件
- Swing - 事件處理
- Swing - 事件類
- Swing - 事件監聽器
- Swing - 事件介面卡
- Swing - 佈局
- Swing - 選單
- Swing - 容器
- Swing 有用資源
- Swing - 快速指南
- Swing - 有用資源
- Swing - 討論
Swing - JLabel 類
簡介
JLabel 類可以顯示文字、影像或兩者兼而有之。標籤的內容透過設定其顯示區域中的垂直和水平對齊方式來對齊。預設情況下,標籤在其顯示區域中垂直居中。僅包含文字的標籤預設情況下左對齊;僅包含影像的標籤預設情況下水平居中。
類宣告
以下是 javax.swing.JLabel 類的宣告:
public class JLabel
extends JComponent
implements SwingConstants, Accessible
欄位
以下是 javax.swing.JLabel 類的欄位:
protected Component labelFor
類建構函式
| 序號 | 建構函式 & 描述 |
|---|---|
| 1 |
JLabel() 建立一個沒有影像且標題為空字串的 JLabel 例項。 |
| 2 |
JLabel(Icon image) 使用指定的影像建立一個 JLabel 例項。 |
| 3 |
JLabel(Icon image, int horizontalAlignment) 使用指定的影像和水平對齊方式建立一個 JLabel 例項。 |
| 4 |
JLabel(String text) 使用指定的文字建立一個 JLabel 例項。 |
| 5 |
JLabel(String text, Icon icon, int horizontalAlignment) 使用指定的文字、影像和水平對齊方式建立一個 JLabel 例項。 |
| 6 |
JLabel(String text, int horizontalAlignment) 使用指定的文字和水平對齊方式建立一個 JLabel 例項。 |
類方法
| 序號 | 方法 & 描述 |
|---|---|
| 1 |
protected int checkHorizontalKey(int key, String message) 驗證 key 是否為 horizontalAlignment 屬性的合法值。 |
| 2 |
protected int checkVerticalKey(int key, String message) 驗證 key 是否為 verticalAlignment 或 verticalTextPosition 屬性的合法值。 |
| 3 |
AccessibleContext getAccessibleContext() 獲取此物件的 AccessibleContext。 |
| 4 |
Icon getDisabledIcon() 返回標籤在停用時使用的圖示。 |
| 5 |
int getDisplayedMnemonic() 返回指示助記鍵的鍵碼。 |
| 6 |
int getDisplayedMnemonicIndex() 返回外觀應提供修飾以表示助記符字元的字元(作為索引)。 |
| 7 |
int getHorizontalAlignment() 返回標籤內容沿 X 軸的對齊方式。 |
| 8 |
int getHorizontalTextPosition() 返回標籤文字相對於其影像的水平位置。 |
| 9 |
Icon getIcon() 返回標籤顯示的圖形影像(字形、圖示)。 |
| 10 |
int getIconTextGap() 返回此標籤中顯示的文字和圖示之間的空間量。 |
| 11 |
Component getLabelFor() 獲取此標籤所標記的元件。 |
| 12 |
String getText() 返回標籤顯示的文字字串。 |
| 13 |
LabelUI getUI() 返回渲染此元件的 L&F 物件。 |
| 14 |
String getUIClassID() 返回一個字串,該字串指定渲染此元件的 l&f 類的名稱。 |
| 15 |
int getVerticalAlignment() 返回標籤內容沿 Y 軸的對齊方式。 |
| 16 |
int getVerticalTextPosition() 返回標籤文字相對於其影像的垂直位置。 |
| 17 |
boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) 覆蓋此方法以在當前 Icon 的影像不等於傳入的 Image img 時返回 false。 |
| 18 |
protected String paramString() 返回此 JLabel 的字串表示形式。 |
| 19 |
void setDisabledIcon(Icon disabledIcon) 設定如果此 JLabel“停用”(JLabel.setEnabled(false))要顯示的圖示。 |
| 20 |
void setDisplayedMnemonic(char aChar) 將 displayedMnemonic 指定為 char 值。 |
| 21 |
void setDisplayedMnemonic(int key) 指定指示助記鍵的鍵碼。 |
| 22 |
void setDisplayedMnemonicIndex(int index) 向外觀提供提示,指示文字中哪個字元應進行裝飾以表示助記符。 |
| 23 |
void setHorizontalAlignment(int alignment) 設定標籤內容沿 X 軸的對齊方式。 |
| 24 |
void setHorizontalTextPosition(int textPosition) 設定標籤文字相對於其影像的水平位置。 |
| 25 |
void setIcon(Icon icon) 定義此元件將顯示的圖示。 |
| 26 |
void setIconTextGap(int iconTextGap) 如果同時設定了圖示和文字屬性,則此屬性定義它們之間的空間。 |
| 27 |
void setLabelFor(Component c) 設定此標籤所標記的元件。 |
| 28 |
void setText(String text) 定義此元件將顯示的單行文字。 |
| 29 |
void setUI(LabelUI ui) 設定渲染此元件的 L&F 物件。 |
| 30 |
void setVerticalAlignment(int alignment) 設定標籤內容沿 Y 軸的對齊方式。 |
| 31 |
void setVerticalTextPosition(int textPosition) 設定標籤文字相對於其影像的垂直位置。 |
| 32 |
void updateUI() 將 UI 屬性重置為當前外觀和風格中的值。 |
繼承的方法
此類繼承自以下類的方法:
- javax.swing.JComponent
- java.awt.Container
- java.awt.Component
- java.lang.Object
JLabel 示例
使用您選擇的任何編輯器建立以下 Java 程式,例如在 D:/ > SWING > com > tutorialspoint > gui > 中。
SwingControlDemo.javapackage com.tutorialspoint.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.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showLabelDemo(){
headerLabel.setText("Control in action: JLabel");
JLabel label = new JLabel("", JLabel.CENTER);
label.setText("Welcome to TutorialsPoint Swing Tutorial.");
label.setOpaque(true);
label.setBackground(Color.GRAY);
label.setForeground(Color.WHITE);
controlPanel.add(label);
mainFrame.setVisible(true);
}
}
使用命令提示符編譯程式。轉到 D:/ > SWING 並鍵入以下命令。
D:\SWING>javac com\tutorialspoint\gui\SwingControlDemo.java
如果未出現錯誤,則表示編譯成功。使用以下命令執行程式。
D:\SWING>java com.tutorialspoint.gui.SwingControlDemo
驗證以下輸出。