Java編程swing組件JLabel詳解以及使用示例
JLabel 對象可以顯示文本、圖像或同時顯示二者??梢酝ㄟ^設(shè)置垂直和水平對齊方式,指定標(biāo)簽顯示區(qū)中標(biāo)簽內(nèi)容在何處對齊。默認(rèn)情況下,標(biāo)簽在其顯示區(qū)內(nèi)垂直居中對齊。默認(rèn)情況下,只顯示文本的標(biāo)簽是開始邊對齊;而只顯示圖像的標(biāo)簽則水平居中對齊。 還可以指定文本相對于圖像的位置。默認(rèn)情況下,文本位于圖像的結(jié)尾邊上,文本和圖像都垂直對齊。
構(gòu)造方法介紹:
JLabel() 創(chuàng)建無圖像并且其標(biāo)題為空字符串的 JLabel。
JLabel(Icon image) 創(chuàng)建具有指定圖像的 JLabel 實例。
JLabel(Icon image, int horizontalAlignment) 創(chuàng)建具有指定圖像和水平對齊方式的 JLabel 實例。
JLabel(String text) 創(chuàng)建具有指定文本的 JLabel 實例。
JLabel(String text, Icon icon, int horizontalAlignment) 創(chuàng)建具有指定文本、圖像和水平對齊方式的 JLabel 實例。
JLabel(String text, int horizontalAlignment) 創(chuàng)建具有指定文本和水平對齊方式的 JLabel 實例。
常用方法:
getHorizontalAlignment() 返回標(biāo)簽內(nèi)容沿 X 軸的對齊方式。
getHorizontalTextPosition() 返回標(biāo)簽的文本相對其圖像的水平位置。
getIcon() 返回該標(biāo)簽顯示的圖形圖像(字形、圖標(biāo))。 getText() 返回該標(biāo)簽所顯示的文本字符串。
setHorizontalAlignment(int alignment) 設(shè)置標(biāo)簽內(nèi)容沿 X 軸的對齊方式。
setHorizontalTextPosition(int textPosition) 設(shè)置標(biāo)簽的文本相對其圖像的水平位置。
setIcon(Icon icon) 定義此組件將要顯示的圖標(biāo)。
setText(String text) 定義此組件將要顯示的單行文本。 setUI(LabelUI ui) 設(shè)置呈現(xiàn)此組件的 L&F 對象。
setVerticalAlignment(int alignment) 設(shè)置標(biāo)簽內(nèi)容沿 Y 軸的對齊方式。
setVerticalTextPosition(int textPosition) 設(shè)置標(biāo)簽的文本相對其圖像的垂直位置。
在JLabel中增加圖片和文本
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class MixingIconLabel {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
frame.setTitle("JLabel Test");
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon imageIcon = new ImageIcon("yourFile.gif");
JLabel label = new JLabel("Mixed", imageIcon, SwingConstants.RIGHT);
frame.add(label);
frame.pack();
frame.setVisible(true);
}
}
JLabel中增加HTML文本
import javax.swing.JFrame;
import javax.swing.JLabel;
public class HTMLLabel {
public static void main(String[] a) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("<html>bold <br> plain</html>");
frame.add(label);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
重寫JLabel
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.Serializable;
import javax.swing.JLabel;
public class Colors extends JLabel implements Serializable {
transient private Color color;
// not persistent
private Boolean rectangular;
// is persistent
public Colors() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
change();
}
}
);
rectangular = false;
setSize(200, 100);
change();
}
public Boolean getRectangular() {
return rectangular;
}
public void setRectangular(Boolean flag) {
this.rectangular = flag;
repaint();
}
public void change() {
color = randomColor();
repaint();
}
private Color randomColor() {
int r = (int) (255 * Math.random());
int g = (int) (255 * Math.random());
int b = (int) (255 * Math.random());
return new Color(r, g, b);
}
public void paint(Graphics g) {
Dimension d = getSize();
int h = d.height;
int w = d.width;
g.setColor(color);
if (rectangular) {
g.fillRect(0, 0, w - 1, h - 1);
} else {
g.fillOval(0, 0, w - 1, h - 1);
}
}
}
將JLabel增加到JScrollPane中便于顯示大圖片
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
public class ScrollPaneFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();
JLabel image = new JLabel(new ImageIcon("A.jpg"));
frame.getContentPane().add(new JScrollPane(image));
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
JLabel中增加unicode編碼
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Unicode {
public static void main(String args[]) {
UnicodeJFrame unicodeJFrame = new UnicodeJFrame();
unicodeJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
unicodeJFrame.setSize(350, 250);
unicodeJFrame.setVisible(true);
}
}
class UnicodeJFrame extends JFrame {
public UnicodeJFrame() {
super("Demonstrating Unicode");
setLayout(new GridLayout(8, 1));
JLabel englishJLabel = new JLabel("/u0057/u0065/u006C/u0063"
+ "/u006F/u006D/u0065/u0020/u0074/u006F/u0020Unicode/u0021");
englishJLabel.setToolTipText("This is English");
add(englishJLabel);
JLabel chineseJLabel = new JLabel("/u6B22/u8FCE/u4F7F/u7528" + "/u0020/u0020Unicode/u0021");
chineseJLabel.setToolTipText("This is Traditional Chinese");
add(chineseJLabel);
JLabel cyrillicJLabel = new JLabel("/u0414/u043E/u0431/u0440"
+ "/u043E/u0020/u043F/u043E/u0436/u0430/u043B/u043E/u0432"
+ "/u0430/u0422/u044A/u0020/u0432/u0020Unicode/u0021");
cyrillicJLabel.setToolTipText("This is Russian");
add(cyrillicJLabel);
JLabel frenchJLabel = new JLabel("/u0042/u0069/u0065/u006E/u0076"
+ "/u0065/u006E/u0075/u0065/u0020/u0061/u0075/u0020Unicode/u0021");
frenchJLabel.setToolTipText("This is French");
add(frenchJLabel);
JLabel germanJLabel = new JLabel("/u0057/u0069/u006C/u006B/u006F"
+ "/u006D/u006D/u0065/u006E/u0020/u007A/u0075/u0020Unicode/u0021");
germanJLabel.setToolTipText("This is German");
add(germanJLabel);
JLabel japaneseJLabel = new JLabel("Unicode/u3078/u3087/u3045" + "/u3053/u305D/u0021");
japaneseJLabel.setToolTipText("This is Japanese");
add(japaneseJLabel);
JLabel portugueseJLabel = new JLabel("/u0053/u00E9/u006A/u0061"
+ "/u0020/u0042/u0065/u006D/u0076/u0069/u006E/u0064/u006F/u0020" + "Unicode/u0021");
portugueseJLabel.setToolTipText("This is Portuguese");
add(portugueseJLabel);
JLabel spanishJLabel = new JLabel("/u0042/u0069/u0065/u006E"
+ "/u0076/u0065/u006E/u0069/u0064/u0061/u0020/u0061/u0020" + "Unicode/u0021");
spanishJLabel.setToolTipText("This is Spanish");
add(spanishJLabel);
}
}
總結(jié)
以上就是本文關(guān)于Java編程swing組件JLabel詳解以及使用示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
《Java圖形化界面設(shè)計之容器(JFrame)詳解》
如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
Java導(dǎo)出Execl疑難點處理的實現(xiàn)
這篇文章主要介紹了Java導(dǎo)出Execl疑難點處理的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
淺談Java開發(fā)架構(gòu)之領(lǐng)域驅(qū)動設(shè)計DDD落地
DDD(Domain-Driven Design 領(lǐng)域驅(qū)動設(shè)計)是由Eric Evans最先提出,目的是對軟件所涉及到的領(lǐng)域進行建模,以應(yīng)對系統(tǒng)規(guī)模過大時引起的軟件復(fù)雜性的問題2021-06-06
AndroidHttpClient使用Cookie應(yīng)用分析
今天想把一個用使用了HttpClient的自動簽到小程序移植到Android上,還好Android的SDK自帶了HttpClient的包.當(dāng)然也可以繼續(xù)使用DefaultHttpClient,但用為Android定制的AndroidHttpClient自然更好2012-11-11
Java?SimpleDateFormat與System類使用示例詳解
這篇文章主要介紹了Java?SimpleDateFormat與System類使用示例,對于SimpleDateFormat類,是一個用來區(qū)分區(qū)域設(shè)置的方式進行日期的是指,以及對日期進行處理分析的一個實現(xiàn)類2022-11-11
IDEA中application.properties的圖標(biāo)顯示不正常的問題及解決方法
這篇文章主要介紹了IDEA中application.properties的圖標(biāo)顯示不正常的問題及解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
elasticsearch集群cluster?discovery可配式模塊示例分析
這篇文章主要為大家介紹了elasticsearch集群cluster?discovery可配式模塊示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04

