zoukankan      html  css  js  c++  java
  • java编程接口(6) ------ 图标

     本文提出了自己的学习笔记,欢迎转载,但请注明出处: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()要播放或更改图标。

    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    BeautifulSoup
    requests
    安装xpath helper
    取消搜狗输入法的快捷键
    numpy初识 old
    Jupyter Notebook 快捷键
    安装numpy、matplotlib
    JavaScript 继承 -JavaScript高级程序设计
    mac /windows
    unicode 地址
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4865683.html
Copyright © 2011-2022 走看看