Icon、ImageIcon标签

编程入门 行业动态 更新时间:2024-10-26 16:29:22

Icon、ImageIcon<a href=https://www.elefans.com/category/jswz/34/1770160.html style=标签"/>

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标签

本文发布于:2024-02-25 16:13:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1699562.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标签   Icon   ImageIcon

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!