标签"/>
Icon、ImageIcon标签
Icon、ImageIcon标签
①Icon标签
源代码:
package cn.ecut.swing.icon;import javax.swing.*;
import java.awt.*;public class IconDemo extends JFrame implements Icon {public static void main(String[] args) {new IconDemo().init();}private int width;private int height;public IconDemo(){}public IconDemo(int width,int height){this.width=width;this.height=height;}public void init(){IconDemo iconDemo=new IconDemo(15,15);JLabel jLabel=new JLabel("icontest",iconDemo,SwingConstants.CENTER);Container container=this.getContentPane();container.add(jLabel);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}@Overridepublic void paintIcon(Component c, Graphics g, int x, int y) {g.setColor(Color.BLUE);g.fillOval(x,y,width,height);}@Overridepublic int getIconWidth() {return this.width;}@Overridepublic int getIconHeight() {return this.height;}
}
运行结果:
②ImageIcon标签
源代码:
package cn.ecut.swing.icon;import javax.swing.*;
import java.awt.*;
import java.URL;public class ImageIconDemo extends JFrame {public static void main(String[] args) {new ImageIconDemo();}public ImageIconDemo(){//获取图片地址JLabel label=new JLabel("ImageIcon");URL url=ImageIconDemo.class.getResource("t.jpg");//使用ImageIcon类读取图像ImageIcon imageIcon=new ImageIcon(url);//将读取出的图像放在lable中label.setIcon(imageIcon);label.setHorizontalAlignment(SwingConstants.CENTER);Container container=getContentPane();container.add(label);setVisible(true);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);setBounds(100,100,200,200);}
}
运行结果:
更多推荐
Icon、ImageIcon标签
发布评论