简体   繁体   English

Java Swing在MigLayout中添加图像代码似乎不起作用?

[英]Java Swing adding images code in MigLayout does not seem to be working?

So I have my own custom JFrame , and in it I am trying to create an auto-resizing image to be contained in my JFrame 's content JPanel , frameContent . 所以我有自己的自定义JFrame ,在其中我尝试创建一个自动调整大小的图像,以包含在我的JFrame的内容JPanelframeContent My layout manager for the JPanel is MigLayout , so I figured I would create a child of JPanel again, called ImagePanel . 我的JPanel布局管理器是MigLayout ,所以我想我会再次创建一个JPanel的子项,名为ImagePanel Here is what my ImagePanel class ended up looking like: 这是我的ImagePanel class最终看起来像:

class ImagePanel extends JPanel{
    private static final long serialVersionUID = -5602032021645365870L;
    private BufferedImage image;

    public ImagePanel() {
        try {                
            image = ImageIO.read(new File(getClass().getResource("../res/Title.png").toURI()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, getWidth(), getHeight(), null);      
    }

}

Now for some reason it doesn't seem like it's actually 'working'. 现在出于某种原因,它似乎并没有真正“起作用”。 When I am setting up my main JFrame 's content, I call: 当我设置我的主要JFrame内容时,我打电话给:

framecontent.add(new ImagePanel(),  "pos 5% 5% 95% 45%");

It's not that it's not adding the component, as with this code I'm able to get the following screen: 这并不是说它没有添加组件,因为这个代码我可以得到以下屏幕:

在此输入图像描述

Notice how it outlines the area against the gray background, meaning the paintComponent(g) method is being run, and the program also isn't outputting any errors, which is strange, so that means it is finding my image, just not placing it. 注意它是如何在灰色背景下勾勒出区域的,这意味着正在运行paintComponent(g)方法,并且程序也没有输出任何错误,这很奇怪,这意味着它正在找到我的图像,只是没有放置它。

Here is what my file hierarchy looks like: 这是我的文件层次结构:

Project Folder >
    src >
        res >
            Title.png (Picture I want to retrieve)
        guiwork >
            Working.class (Class that is running instructions/ main)

Got it all fixed up, here is the auto-resizing result: 完成了所有修复,这是自动调整大小的结果:

结果

Notice how it outlines the area against the gray background, meaning the paintComponent(g) method is being run, and the program also isn't outputting any errors, which is strange, so that means it is finding my image, just not placing it. 注意它是如何在灰色背景下勾勒出区域的,这意味着正在运行paintComponent(g)方法,并且程序也没有输出任何错误,这很奇怪,这意味着它正在找到我的图像,只是没有放置它。

Not so as a null image will throw no errors. 不是这样,空图像不会抛出任何错误。 To prove this, test your image variable in the paintComponent method and you'll likely find that it is in fact null. 为了证明这一点,在paintComponent方法中测试你的图像变量,你可能会发现它实际上是null。

ie, try this: 即试试这个:

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    System.out.println("image is null: " + (image == null)); // TODO: Delete this
    g.drawImage(image, getWidth(), getHeight(), null);      
}

Solution: don't change your resource to a file, but instead use the resource as is, and make sure that you're looking for it in the correct location. 解决方案:不要将资源更改为文件,而是按原样使用资源,并确保在正确的位置查找资源。


Edit 编辑
Oh chit , you're drawing your image beyond the bounds of your component: 哦, 小时候 ,你的图像超出了组件的范围:

g.drawImage(image, getWidth(), getHeight(), null);  

try: 尝试:

g.drawImage(image, 0, 0, null);  

Edit 2 编辑2
What if you try a simple test program, something like: 如果您尝试一个简单的测试程序,例如:

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class TestImage {
   public TestImage() throws IOException {
      String path = "/res/Title.png";
      BufferedImage img = ImageIO.read(getClass().getResource(path));
      ImageIcon icon = new ImageIcon(img);
      JOptionPane.showMessageDialog(null, icon);
   }
   public static void main(String[] args) {
      try {
         new TestImage();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

and be sure to place this in the same location as your current class. 并确保将其放在与当前班级相同的位置。 Does it run? 它运行吗?

For those who might have trouble understanding HoverCraft's Answer: 对于那些可能无法理解HoverCraft答案的人:

Say you have a Panel in which you want to add an image that scales, the following code does that for you 假设您有一个Panel,您要在其中添加可缩放的图像,以下代码为您执行此操作

import net.miginfocom.swing.MigLayout;
import javax.swing.*;
import java.awt.*;

public class MasterSlidePanel extends JPanel {
    private ImageIcon imageIcon;
    private JLabel image;

    public MasterSlidePanel() {
        super(new MigLayout("", "2% [] 2%", "2% [] 2%"));
    }

    @Override
    protected void paintComponent(Graphics g) {
        image.setIcon(new ImageIcon(imageIcon.getImage().getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_SMOOTH)));
        super.paintComponent(g);
    }

    public void setImage(SlidePanel panel) {
        imageIcon = <Where you get the imageIcon>;
        image = new JLabel();
        image.setMinimumSize(new Dimension(10, 10));
        image.setPreferredSize(new Dimension(10, 10));
        this.add(image, "width 96%, height 96%");
    }
}

We add 2 new members to the panel, image and imageIcon. 我们在面板中添加了2个新成员,image和imageIcon。 The moment when the application needs to paint the panel, a scaled inscance of the imageIcon is loaded and given to image. 当应用程序需要绘制面板时,会加载imageIcon的缩放内容并将其提供给图像。

To prevent 0 height/width errors, we add a minimumsize to the JLabel. 为了防止0高度/宽度错误,我们向JLabel添加了最小尺寸。

Hope this helps. 希望这可以帮助。

Credits still go to HoverCraft Full Of Eels. 积分仍然归于HoverCraft Full Of Eels。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM