窗体中添加标签Label、Icon图标

http://lixiyu.blog.51cto.com/4136883/1312248(原文)


一,在窗体中创建一个带有指定文本的标签对象并添加一个图像

实现界面:

114104284.jpg

详细代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.lixiyu;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Index {
public void add(JFrame frame){
     frame.setTitle( "添加标签" ); //修改窗口标题
     JLabel label= new JLabel( "一个可爱的小孩!" ,JLabel.CENTER); //创建指定文本的标签对象
     label.setIcon( new ImageIcon( "pic/MR1.JPG" )); //添加图像
     label.setHorizontalTextPosition(JLabel.CENTER); //设置文本相对于图像的水平位置
     label.setVerticalTextPosition(JLabel.BOTTOM); //设置文本相对于图像的垂直位置
     label.setEnabled( false ); //设置标签为不可用
     label.setDisabledIcon( new ImageIcon( "pic/MR2.JPG" )); //设置标签在不可用情况下显示的图像
     frame.add(label);
}
public static void main(String[] args){
     JFrame frame= new JFrame( "利用JFrame创建窗口" );
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setSize( 500 , 400 );
     Dimension displaySize=Toolkit.getDefaultToolkit().getScreenSize();
     Dimension frameSize=frame.getSize();
     if (frameSize.width>displaySize.width)
         frameSize.width=displaySize.width; //窗口的宽度不能大于显示器的
     if (frameSize.height>displaySize.height)
         frameSize.height=displaySize.height;
     frame.setLocation((displaySize.width-frameSize.width)/ 2 ,
     (displaySize.height-frameSize.height)/ 2 ); //设置窗口居中显示
     Index index= new Index();
     index.add(frame); //向JFrame窗口添加标签
     frame.setVisible( true );
}
}


二、在窗口中添加图标

1.创建图标

实现界面:

114450504.jpg

详细代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.lixiyu;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
public class DrawIcon implements Icon{ //实现Icon接口
private int width; //声明图标的宽
private int height; //声明图标的高
public int getIconHeight(){ //实现getIconHeight方法
     return this .height;
}
public int getIconWidth(){ //实现getIconWidth方法
     return this .width;
}
public DrawIcon( int width, int height) {
     // TODO Auto-generated constructor stub
     this .width=width;
     this .height=height;
}
public void paintIcon(Component arg0,Graphics arg1, int x, int y){ //实现paintIcon方法
     arg1.fillOval(x, y, width, height); //绘制一个图形
}
public static void main(String[] args){
     DrawIcon icon= new DrawIcon( 15 , 15 );
     JLabel j= new JLabel( "在窗体中添加图标" ,icon,SwingConstants.CENTER); //创建标签并设置为正中间
     JFrame jf= new JFrame();
     Container c=jf.getContentPane();
     c.add(j);
     jf.setSize( 170 , 100 );
     jf.setVisible( true );
     jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
}


2.使用图片图标

实现界面:

114640281.jpg

详细代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.lixiyu;
import java.awt.Container;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
@SuppressWarnings ( "serial" )
public class MyImageIcon extends JFrame{
public MyImageIcon(){
     Container container=getContentPane();
     JLabel jl= new JLabel( "今年NBA应该很好看" ,JLabel.CENTER);
     URL url=MyImageIcon. class .getResource( "image.png" ); //获取图片所在的URL
     Icon icon= new ImageIcon(url); //实例化Icon对象
     jl.setIcon(icon);
     jl.setSize( 15 , 15 );
     jl.setHorizontalAlignment(SwingConstants.CENTER);
     jl.setOpaque( true );
     container.add(jl);
     setSize( 250 , 100 );
     setVisible( true );
     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args){
     new MyImageIcon();
       
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值