在 Java 中创建图像图标
Sheeraz Gul
2024年2月15日
Java 中的 Swing 库具有类 ImageIcon,可用于创建图像图标。本教程将演示如何在 Java 中制作不同的图像图标。
在 Java 中使用 Swing.ImageIcon
绘制图像图标
绘制图像图标意味着从给定图像创建图像图标。
例子:
package delftstack;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
class DrawingPanel extends JPanel {
private ImageIcon img_icon;
public DrawingPanel() {
load_Image();
init_Panel();
}
private void load_Image() {
img_icon = new ImageIcon("delftstack.jpg");
}
private void init_Panel() {
int img_width = img_icon.getIconWidth();
int img_height = img_icon.getIconHeight();
setPreferredSize(new Dimension(img_width, img_height));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
img_icon.paintIcon(this, g, 0, 0);
}
}
public class Image_Icon extends JFrame {
public Image_Icon() {
init_User_Inter();
}
private void init_User_Inter() {
DrawingPanel drawing1 = new DrawingPanel();
createLayout(drawing1);
setTitle("Image Icon");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createLayout(JComponent... argument) {
Container demo_pane = getContentPane();
GroupLayout demo_gl = new GroupLayout(demo_pane);
demo_pane.setLayout(demo_gl);
demo_gl.setHorizontalGroup(demo_gl.createSequentialGroup().addComponent(argument[0]));
demo_gl.setVerticalGroup(demo_gl.createParallelGroup().addComponent(argument[0]));
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame demo_img = new Image_Icon();
demo_img.setVisible(true);
});
}
}
上面的代码尝试加载 delftstack.jpg
图像并将其绘制为图标。
输出:
在 Java 中使用 Swing.ImageIcon
创建图像图标按钮
图片图标主要用在按钮上; 让我们尝试创建带有图像图标的按钮。
例子:
package delftstack;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Custom_Image_Icon extends JFrame {
public Custom_Image_Icon() {
init_User_Interface();
}
private void init_User_Interface() {
ImageIcon Facebook_Icon = new ImageIcon("facebook.png");
ImageIcon Twitter_Icon = new ImageIcon("twitter.png");
ImageIcon Email_Icon = new ImageIcon("email.png");
JButton Facebook_Btn = new JButton(Facebook_Icon);
JButton Twitter_Btn = new JButton(Twitter_Icon);
JButton Email_Btn = new JButton(Email_Icon);
createLayout(Facebook_Btn, Twitter_Btn, Email_Btn);
setTitle("Image Icons on Buttons");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void createLayout(JComponent... argument) {
Container Btn_Pane = getContentPane();
GroupLayout demo_graphic = new GroupLayout(Btn_Pane);
Btn_Pane.setLayout(demo_graphic);
demo_graphic.setAutoCreateContainerGaps(true);
demo_graphic.setAutoCreateGaps(true);
demo_graphic.setHorizontalGroup(demo_graphic.createSequentialGroup()
.addComponent(argument[0])
.addComponent(argument[1])
.addComponent(argument[2]));
demo_graphic.setVerticalGroup(demo_graphic.createParallelGroup()
.addComponent(argument[0])
.addComponent(argument[1])
.addComponent(argument[2]));
demo_graphic.linkSize(argument[0], argument[1], argument[2]);
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Custom_Image_Icon Btn_Icons = new Custom_Image_Icon();
Btn_Icons.setVisible(true);
});
}
}
上面的代码创建了三个带有 Facebook、Twitter 和电子邮件图标的按钮。
输出:
作者: Sheeraz Gul
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook