【问题标题】:Image on JButtonJButton 上的图像
【发布时间】:2018-04-04 17:31:13
【问题描述】:
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 位于“图片”源文件夹中。

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

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

另外,这里是实际椭圆图片的链接:

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

【问题讨论】:

  • 我不是 Eclipse 用户,但根据您项目的布局和错误消息,我会说 Pictures 不包含在应用程序的类路径上下文中。您可以尝试将应用导出为 Jar 并提取内容以仔细检查包含的内容
  • @MadProgrammer 我进行了编辑。程序运行但椭圆图片没有出现在任何按钮上
  • 我也建议不要弄乱按钮的尺寸,任何 Swing 组件都不会缩放图像
  • 更改图片大小以满足您的要求
  • 要么使用图像编辑器,要么在运行时缩放它们——第一个选项几乎总是更好

标签: java eclipse swing jpanel jbutton


【解决方案1】:

首先你必须使用分辨率较小的图像(你的图像有 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());

    }

}

你的输出应该是这样的。

【讨论】:

  • 它似乎对我不起作用。我正在使用 Mac 顺便说一句。我应该将“user.dir”更改为其他内容吗?
  • 请看我刚刚发的桂的图片
  • 遗憾的是我没有mac环境来测试这段代码,无论如何这个链接可能会对你有所帮助>>Java System.getProperty(“user.dir”) on Mac OS X
  • 对不起,我是新手,我不明白这是怎么回事。有没有比使用 System.getProperty 更简单的方法?
  • 您是否使用完整路径进行了测试? 例如:Users/LuxuryMode/Desktop/image.png 和图像正在显示?如果 yes 你必须想办法获得绝对路径。如果不是,则您的图片有问题
【解决方案2】:

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

不要为您的图像创建单独的源文件夹。把它放在你的主源文件夹下的一个名为“assets”或其他东西的子文件夹下。

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

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

然后执行以下操作

bufferedImageName = ImageIO.read(inputStreamName);

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

完成!

【讨论】:

  • "从那里,使用 java.awt.Graphics 库将其绘制到 JButton" ...或者只是使用按钮 icon 支持,因为,你知道,它就在那里
  • 嗯,是的,这可以工作,但这种方式似乎更适用于 eclipse 和所有最终产品。 imo 更长但更安全。
  • 如果您需要手动“绘制”按钮,那么无论您使用什么 IDE,都会出现严重错误
  • 嘿,小问题@MadProgrammer,你在做什么开发?
  • Swing 在 Windows 和 Mac 上 - 我通常使用 Netbeans,但使用过 Eclipse 和 IntelliJ - 归根结底,IDE 应该是无关紧要的 - 因为它是工作的 API
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多