Java编程swing组件JLabel详解以及使用示例
作者:玉开Sir 时间:2023-12-15 22:28:50
JLabel 对象可以显示文本、图像或同时显示二者。可以通过设置垂直和水平对齐方式,指定标签显示区中标签内容在何处对齐。默认情况下,标签在其显示区内垂直居中对齐。默认情况下,只显示文本的标签是开始边对齐;而只显示图像的标签则水平居中对齐。 还可以指定文本相对于图像的位置。默认情况下,文本位于图像的结尾边上,文本和图像都垂直对齐。
构造方法介绍:
JLabel() 创建无图像并且其标题为空字符串的 JLabel。
JLabel(Icon image) 创建具有指定图像的 JLabel 实例。
JLabel(Icon image, int horizontalAlignment) 创建具有指定图像和水平对齐方式的 JLabel 实例。
JLabel(String text) 创建具有指定文本的 JLabel 实例。
JLabel(String text, Icon icon, int horizontalAlignment) 创建具有指定文本、图像和水平对齐方式的 JLabel 实例。
JLabel(String text, int horizontalAlignment) 创建具有指定文本和水平对齐方式的 JLabel 实例。
常用方法:
getHorizontalAlignment() 返回标签内容沿 X 轴的对齐方式。
getHorizontalTextPosition() 返回标签的文本相对其图像的水平位置。
getIcon() 返回该标签显示的图形图像(字形、图标)。 getText() 返回该标签所显示的文本字符串。
setHorizontalAlignment(int alignment) 设置标签内容沿 X 轴的对齐方式。
setHorizontalTextPosition(int textPosition) 设置标签的文本相对其图像的水平位置。
setIcon(Icon icon) 定义此组件将要显示的图标。
setText(String text) 定义此组件将要显示的单行文本。 setUI(LabelUI ui) 设置呈现此组件的 L&F 对象。
setVerticalAlignment(int alignment) 设置标签内容沿 Y 轴的对齐方式。
setVerticalTextPosition(int textPosition) 设置标签的文本相对其图像的垂直位置。
在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);
}
}
总结
《Java编程实现swing圆形按钮实例代码》
《浅谈标签和JLabel类构造方法》
《Java图形化界面设计之容器(JFrame)详解》
如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
来源:http://outofmemory.cn/code-snippet/1121/swing-JLabel-explain-in-detail-yiji-usage-shili
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java 逻辑控制全面详解
![](https://img.aspxhome.com/file/2023/3/79913_0s.png)
Android实现的仿淘宝购物车demo示例
![](https://img.aspxhome.com/file/2023/9/110689_0s.png)
Spring Cache+Redis缓存数据的实现示例
![](https://img.aspxhome.com/file/2023/0/60060_0s.png)
详解Java高并发编程之AtomicReference
![](https://img.aspxhome.com/file/2023/4/99924_0s.jpg)
Java超详细讲解抽象类的原理与用法
![](https://img.aspxhome.com/file/2023/6/80306_0s.png)
Android自定义Camera实现拍照功能
springboot多模块化整合mybatis,mapper自动注入失败问题及解决
![](https://img.aspxhome.com/file/2023/8/60778_0s.jpg)
java 中同步、异步、阻塞和非阻塞区别详解
详解C#如何实现屏幕放大和取色功能
![](https://img.aspxhome.com/file/2023/8/106438_0s.png)
Android中ImageView用法实例分析
图解Eclipse在线安装ADT插件过程
![](https://img.aspxhome.com/file/2023/8/101818_0s.png)
Android 微信6.1 tab栏图标和字体颜色渐变的实现
![](https://img.aspxhome.com/file/2023/4/114364_0s.gif)
Java实现数据连接池Druid举例
C#中IEnumerable、ICollection、IList、List之间的区别
SpringSecurity整合jwt权限认证的全流程讲解
![](https://img.aspxhome.com/file/2023/0/113200_0s.png)
Java设计模式之建造者模式
C#/VB.NET实现将XML转为PDF
![](https://img.aspxhome.com/file/2023/8/72468_0s.jpg)