使用一个标签
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;import javax.swing.JFrame;
import javax.swing.JLabel;//=================================================
// File Name : JFrame_demo
//------------------------------------------------------------------------------
// Author : Common//主类
//Function : JFrame_demo
public class JFrame_demo {public static void main(String[] args) {// TODO 自动生成的方法存根JFrame f = new JFrame("Swing窗口"); //实例化窗体对象JLabel lab = new JLabel("Label",JLabel.CENTER); //实例化对象,使用剧中对齐f.add(lab); //向容器中加入组建Dimension dim = new Dimension(); //实例化Dimension对象dim.setSize(230,80); //设置大小f.setSize(dim); //设置组件大小f.setBackground(Color.WHITE);Point point = new Point(300,200); //设置现实的坐标点f.setLocation(point);f.setVisible(true);}}
更改JLabel的文本样式
设置标签的显示字体、大小、背景颜色
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;import javax.swing.JFrame;
import javax.swing.JLabel;//=================================================
// File Name : JFrame_demo
//------------------------------------------------------------------------------
// Author : Common//主类
//Function : JFrame_demo
public class JFrame_demo {public static void main(String[] args) {// TODO 自动生成的方法存根JFrame f = new JFrame("Swing窗口"); //实例化窗体对象JLabel lab = new JLabel("Label",JLabel.CENTER); //实例化对象,使用剧中对齐Font fnt = new Font("Serief",Font.ITALIC+Font.BOLD,28);lab.setFont(fnt); //设置标签的显示字体lab.setForeground(Color.RED); //设置标签的文字颜色f.add(lab); //向容器中加入组建Dimension dim = new Dimension(); //实例化Dimension对象dim.setSize(230,80); //设置大小f.setSize(dim); //设置组件大小f.setBackground(Color.WHITE);Point point = new Point(300,200); //设置现实的坐标点f.setLocation(point);f.setVisible(true);}}
在JLabel中设置图片
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;//=================================================
// File Name : JFrame_demo
//------------------------------------------------------------------------------
// Author : Common//主类
//Function : JFrame_demo
public class JFrame_demo {public static void main(String[] args) {// TODO 自动生成的方法存根JFrame f = new JFrame("Swing窗口"); //实例化窗体对象String picPath = "/home/common/software/database/photo.jpg";Icon icon = new ImageIcon(picPath);JLabel lab = new JLabel(icon,JLabel.CENTER); //实例化对象,使用剧中对齐Font fnt = new Font("Serief",Font.ITALIC+Font.BOLD,28);lab.setFont(fnt); //设置标签的显示字体lab.setForeground(Color.RED); //设置标签的文字颜色f.add(lab); //向容器中加入组建Dimension dim = new Dimension(); //实例化Dimension对象dim.setSize(230,80); //设置大小f.setSize(dim); //设置组件大小f.setBackground(Color.WHITE);Point point = new Point(300,200); //设置现实的坐标点f.setLocation(point);f.setVisible(true);}}