欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java编程swing组件JLabel详解以及使用示例

程序员文章站 2023-12-16 15:05:10
jlabel 对象可以显示文本、图像或同时显示二者。可以通过设置垂直和水平对齐方式,指定标签显示区中标签内容在何处对齐。默认情况下,标签在其显示区内垂直居中对齐。默认情况下...

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编程实现swing圆形按钮实例代码

浅谈标签和jlabel类构造方法

java图形化界面设计之容器(jframe)详解

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

上一篇:

下一篇: