您的位置:首页 > 编程语言 > Java开发

java编程接口(6) ------ 图标

2015-10-10 08:16 495 查看

本文提出了自己的学习笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020


能够在JLable或者不论什么从AbstractButton继承的组件使用Icon。

能够使用不论什么想用的gif文件,要打开一个文件而且得到图形。仅仅须要创建一个ImageIcon对象并把文件名称传递给它就可以。然后就能够在程序中使用它了。

以下的样例是给JLabel和JButton加入图片,而且为button的各个状态设置不同的图片。代码例如以下:

public class Icons extends JFrame {

private static Icon[] icons;
private JButton jb,jb2 = new JButton("Disable");

public Icons() {
setLayout(new FlowLayout());
setVisible(true);
setSize(400,200);
setTitle("Icon");
// TODO Auto-generated constructor stub
icons = new Icon[]{
new ImageIcon(getClass().getResource("icon1.png")),
new ImageIcon(getClass().getResource("icon2.png")),
new ImageIcon(getClass().getResource("icon3.png")),
new ImageIcon(getClass().getResource("icon4.png")),
new ImageIcon(getClass().getResource("icon5.png")),
new ImageIcon(getClass().getResource("icon6.png"))
};

jb = new JButton(icons[3]);

add(new JLabel(icons[5]));

jb.setRolloverEnabled(true);
jb.setRolloverIcon(icons[2]);
jb.setPressedIcon(icons[2]);
jb.setDisabledIcon(icons[4]);
jb.setToolTipText("提示");
add(jb);
jb2.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(jb.isEnabled()){
jb.setEnabled(false);
jb2.setText("Enable");
}
else{
jb.setEnabled(true);
jb2.setText("Disable");
}
}
});
add(jb2);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Icons();
}

}
运行程序,结果例如以下:







   能够看出,button在按下、禁止、浮动时的显示的图片不同,而且。也给button加入了“工具提示”的功能。当鼠标停留在button上时。就会出现提示的文本,这就使得button具有了相当不错的动画效果。

   很多不同的Swing组件的构造器都接受Icon类型的參数,也可采用setIcon()要播放或更改图标。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: