[英]Image on JButton
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class Test extends JPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
constructGUI();
}
});
}
private static void constructGUI() {
JFrame frame = new JFrame("Testy");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.DARK_GRAY);
centerPanel.setPreferredSize(new Dimension(100, 400));
frame.add(centerPanel, BorderLayout.CENTER);
Test eastPanel = new Test();
frame.add(eastPanel, BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
public Test() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
Dimension d = new Dimension(50, 50);
JButton button1 = new JButton("");
button1.setPreferredSize(d);
button1.setIcon(new ImageIcon(this.getClass().getResource("/Pictures/ellipse.png")));
button1.setMaximumSize(new Dimension(Integer.MAX_VALUE, button1.getMinimumSize().height));
add(button1);
JButton button2 = new JButton("");
button2.setPreferredSize(d);
button2.setIcon(new ImageIcon(this.getClass().getResource("/Pictures/ellipse.png")));
button2.setMaximumSize(new Dimension(Integer.MAX_VALUE, button2.getMinimumSize().height));
add(button2);
JButton button3 = new JButton("");
button3.setPreferredSize(d);
button3.setIcon(new ImageIcon(this.getClass().getResource("/Pictures/ellipse.png")));
button3.setMaximumSize(new Dimension(Integer.MAX_VALUE, button3.getMinimumSize().height));
add(button3);
add(Box.createVerticalGlue());
}
}
In my program I am trying to put the ellipse picture on top of all my buttons.在我的程序中,我试图将椭圆图片放在所有按钮的顶部。 As you can see in the image I posted, the ellipse.png is in the "Pictures" source folder.
正如您在我发布的图片中看到的,ellipse.png 位于“图片”源文件夹中。
However, the image does not appear on the JButtons for some reason.但是,由于某种原因,图像没有出现在 JButton 上。
I have read many posts but I can't see a way to solve my problem.我已经阅读了很多帖子,但我找不到解决问题的方法。
Also, here is a link to the actual ellipse picture:此外,这是实际椭圆图片的链接:
https://maxcdn.icons8.com/Share/icon/Editing//ellipse_stroked1600.png https://maxcdn.icons8.com/Share/icon/Editing//ellipse_stroked1600.png
First you have to use image that have smaller resolution size ( your image have 1600x1600 pixels , i would suggest 32X32)首先,您必须使用分辨率较小的图像(您的图像有 1600x1600 像素,我建议使用 32X32)
and refer below code并参考下面的代码
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class Test extends JPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
constructGUI();
}
});
}
private static void constructGUI() {
JFrame frame = new JFrame("Testy");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.DARK_GRAY);
centerPanel.setPreferredSize(new Dimension(100, 400));
frame.add(centerPanel, BorderLayout.CENTER);
Test eastPanel = new Test();
frame.add(eastPanel, BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
public Test() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
Dimension d = new Dimension(50, 50);
JButton button1 = new JButton("");
button1.setPreferredSize(d);
ImageIcon imageIcon = new ImageIcon(System.getProperty("user.dir") + "/Pictures/ellipse.png");
button1.setIcon(imageIcon);
button1.setMaximumSize(new Dimension(Integer.MAX_VALUE, button1.getMinimumSize().height));
add(button1);
JButton button2 = new JButton("");
button2.setPreferredSize(d);
button2.setIcon(imageIcon);
button2.setMaximumSize(new Dimension(Integer.MAX_VALUE, button2.getMinimumSize().height));
add(button2);
JButton button3 = new JButton("");
button3.setPreferredSize(d);
button3.setIcon(imageIcon);
button3.setMaximumSize(new Dimension(Integer.MAX_VALUE, button3.getMinimumSize().height));
add(button3);
add(Box.createVerticalGlue());
}
}
Your out-put should be like this.你的输出应该是这样的。
Eclipse is picky about images and crap, so here is the workaround (sorry it's so long.) Eclipse 对图像和废话很挑剔,所以这里是解决方法(抱歉它太长了。)
Dont make a seperate source folder for your images.不要为您的图像制作单独的源文件夹。 Put it in your main source folder under a subfolder called "assets" or something.
将它放在您的主源文件夹中名为“assets”或其他东西的子文件夹下。
From there, create a buffered image and an input stream ( java.awt.image.BufferedImage
and java.io.InputStream
)从那里,创建一个缓冲图像和一个输入流(
java.awt.image.BufferedImage
和java.io.InputStream
)
Then you want to set the input stream to the image using the getResourceAsStream
method.然后您想使用
getResourceAsStream
方法将输入流设置为图像。
Then do the following然后执行以下操作
bufferedImageName = ImageIO.read(inputStreamName);
From there, use the java.awt.Graphics
library to paint it to the JButton.从那里,使用
java.awt.Graphics
库将其绘制到 JButton。
Finished!完成的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.