当前位置:网站首页>Swing component Icon
Swing component Icon
2022-07-25 10:16:00 【Look at the bugs】
Using pictures as icons
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();
}
// Use pictures as icons
public ImageIconDemo() {
// Create one called ImageIcon The label of
JLabel label = new JLabel("ImageIcon");
// Get the address of the picture
URL url = ImageIconDemo.class.getResource("fj.jpg");
// Go picture properties to imageIcon On
ImageIcon imageIcon = new ImageIcon(url);
// Set the picture on the label
label.setIcon(imageIcon);
// Center the icon
label.setHorizontalAlignment(SwingConstants.CENTER);
// Create a container and add panels to it
Container container=getContentPane();
// Add labels to containers
container.add(label);
setVisible(true);
setBounds(100,100,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
Use the figure drawn by the brush as the icon
package Swing;
import javax.swing.*;
import java.awt.*;
// Icon , Need to implement class ,Frame Inherit
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public static void main(String[] args) {
// Both calling methods can
// 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);
// Icons can be placed on labels or buttons
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;
}
}
边栏推荐
猜你喜欢
随机推荐
几个常用的网络诊断命令
GCD详解
21. Merge Two Sorted Lists
静态路由的配置(以华为eNSP为例)
JS uses requestanimationframe to detect the FPS frame rate of the current animation in real time
Debug篇快捷键入门
多线程——静态代理模式
Round to the nearest
【无标题】
Nodejs初体验
mysql 解决不支持中文的问题
nodejs版本升级或切换的常用方式
Pytorch 张量列表转换为张量 List of Tensor to Tensor 使用 torch.stack()
基础背包问题
Detailed explanation of MySQL database
严重 [main] org.apache.catalina.util.LifecycleBase.handleSubClassException 初始化组件
力扣刷题组合问题总结(回溯)
Probabilistic robot learning notes Chapter 2
js数字千位分割的常用方法
Dataset 和 Dataloader数据加载









