当前位置:网站首页>Swing的组件图标
Swing的组件图标
2022-07-25 09:27:00 【看小虫子】
以图片做图标的方法
package Swing;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame{
public static void main(String[] args) {
new ImageIconDemo();
}
//以图片做图标
public ImageIconDemo() {
//创造一个名为ImageIcon的标签
JLabel label = new JLabel("ImageIcon");
//获取图片的地址
URL url = ImageIconDemo.class.getResource("fj.jpg");
//将图片属性转到imageIcon上
ImageIcon imageIcon = new ImageIcon(url);
//将图片设置在标签上
label.setIcon(imageIcon);
//设置图标居中
label.setHorizontalAlignment(SwingConstants.CENTER);
//创造一个容器并向容器中添加面板
Container container=getContentPane();
//将标签添加到容器中
container.add(label);
setVisible(true);
setBounds(100,100,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
以画笔绘制的图形做图标
package Swing;
import javax.swing.*;
import java.awt.*;
//图标,需要实现类,Frame 继承
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public static void main(String[] args) {
//两个调用方法都可
// IconDemo iconDemo = new IconDemo(15,15);
// iconDemo.init();
new IconDemo().init();
}
public IconDemo() throws HeadlessException {
}
public IconDemo(int width, int height) {
this.width = width;
this.height = height;
}
public void init(){
IconDemo iconDemo = new IconDemo(15, 15);
//图标可以放在标签上也可以放在按钮上
JLabel label = new JLabel("iconTest", iconDemo, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setBounds(100,200,500,500);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
边栏推荐
猜你喜欢
随机推荐
简易加法计算器
小程序分享功能
CCF 201512-3 drawing
多线程——五大状态
小程序H5获取手机号方案
Reflection 反射
oracle 解析同名xml 问题
字符串最长公共前缀
UE4源码的获取和编译
Advanced introduction to digital IC Design SOC
JSP details
Copy the old project into a web project
静态路由的配置(以华为eNSP为例)
js加密参数定位
小程序调起微信支付
21. Merge Two Sorted Lists
修改mysql的分组报错Expression #1 of SELECT list is not in GROUP
yarn速查手册
@Import,Conditional和@ImportResourse注解
Introduction to arm GIC









