JButton 上的图像

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());

    }

}

在我的程序中,我试图将椭圆图片放在所有按钮的顶部。正如您在我发布的图片中所见,ellipse.png 位于 "Pictures" 源文件夹中。

但是,由于某种原因,图像没有出现在 JButton 上。

我已经阅读了很多帖子,但我找不到解决问题的方法。

此外,这里是一个link到实际的椭圆图片:

https://maxcdn.icons8.com/Share/icon/Editing//ellipse_stroked1600.png

首先你必须使用分辨率较小的图像(你的图像有 1600x1600 像素,我建议 32X32)

像这样使用图片

参考下面的代码

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());

    }

}

你的输出应该是这样的。

Eclipse 对图像和废话很挑剔,所以这里是解决方法(抱歉这么长。)

不要为您的图像创建单独的源文件夹。把它放在你的主源文件夹下的一个名为 "assets" 之类的子文件夹下。

从那里创建缓冲图像和输入流(java.awt.image.BufferedImagejava.io.InputStream

然后你想使用getResourceAsStream方法将输入流设置为图像。

然后执行以下操作

bufferedImageName = ImageIO.read(inputStreamName);

从那里,使用 java.awt.Graphics 库将其绘制到 JButton。

完成!