当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

IDEA整体字体大小修改

Probability theory and mathematical statistics 4 continuous random variables and probability distributions (Part 1)

UE4源码的获取和编译

复现 ASVspoof 2021 baseline RawNet2

Probabilistic robot learning notes Chapter 2

Detailed explanation of MySQL database

PyTorch 代码模板 (CNN)

mysql历史数据补充新数据

DHCP的配置(以华为eNSP为例)

小程序调起微信支付
随机推荐
广度优先遍历(图和二叉树的层序遍历相关问题)
Download and installation of QT 6.2
mysql历史数据补充新数据
小程序H5获取手机号方案
Nodejs初体验
canal实现mysql数据同步
Detailed explanation of MySQL database
常用类的小知识
Summary of most consistency problems
ROS distributed operation -- launch file starts nodes on multiple machines
Configuring ROS development environment with vscode: Causes and solutions to the problem of ineffective code modification
切换 shell 命令行终端(bash/zsh)后,conda 无法使用: command not found
多线程——死锁和synchronized
UE4源码的获取和编译
Nodejs initial experience
Use of dictionary tree
Copy the old project into a web project
小程序分享功能
C3D模型pytorch源码逐句详析(三)
Use and principle of rest