简体   繁体   English

从扩展的 JPanel Class 看不到 ImageIcon

[英]Can't see ImageIcon from an extended JPanel Class

I have a Class that extends a JPanel and I want it to display an ImageIcon.我有一个扩展 JPanel 的 Class,我希望它显示一个 ImageIcon。 Something does not seem to work through.有些事情似乎没有通过。 map.png is found and when I print out its size in the class is's correct.找到 map.png 并且当我在 class 中打印出它的大小时是正确的。 Also, I made the panel black so I know that the panel works but not the ImageIcon.另外,我将面板设置为黑色,所以我知道面板可以工作,但 ImageIcon 不能。

However when I place the same code from TestPanel.java into the constructor of GUI.java the ImageIcon works.但是,当我将 TestPanel.java 中的相同代码放入 GUI.java 的构造函数中时,ImageIcon 工作。

Can someone tell me why the ImageIcon does not work in TestPanel.java but it does in GUI.java?有人能告诉我为什么 ImageIcon 在 TestPanel.java 中不起作用,但在 GUI.java 中起作用吗?

Here is my code这是我的代码

TestPanel.java测试面板.java

public class TestPanel extends JPanel {
    
    public static JLabel map = new JLabel();

    public TestPanel() {
        this.setLayout(null); //to prevent icon from taking the whole screen
        this.setVisible(true); //make frame visible
        this.setBounds(0, 0, 100, 100);
        
        this.setBackground(new Color(0,0,0));
        
        Image imageToScale = new ImageIcon("map.png").getImage();
        double scale = .9; //scale
        int x = (int) (imageToScale.getWidth(null) * scale); //leave image observer as null because we know that the image is loaded
        int y = (int) (imageToScale.getHeight(null) * scale);
        Image scaledImage = imageToScale.getScaledInstance( x, y, java.awt.Image.SCALE_SMOOTH); //scale the image
        ImageIcon image = new ImageIcon(scaledImage); //case the image into an image icon
        
        this.setBounds(0, -4, image.getIconWidth(), image.getIconHeight()); //set position and size of panel to the size of the image. -4 on the Y position because it was not aligned with the top
     
        map.setIcon(image); //set icon for map label
        this.add(map); //add label to panel
    }
}

GUI.java GUI.java

public class GUI extends JFrame {
    
    public static JPanel problemPanel = new JPanel(); //panel where the points and path will be displayed
    public static JLabel map = new JLabel();
    
    public static TestPanel test = new TestPanel();
    
    public GUI() {
        this.setLayout(null); //to prevent icon from taking the whole screen
        
        this.add(test);
        
        //Frame
        this.setVisible(true); //make frame visible
        this.setSize(1200,1000); //set frame size
        this.setTitle("Travelling Apache Pizza Delivery Driver Problem (TAPDDP)"); //set title of panel
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE); //terminates program when frame is closed
        this.setLocationRelativeTo(null); //open frame in the middle of the screen
        this.setResizable(false); //prevent resizing of GUI
        

    }

}

I fixed the code by removing this.setLayout(null);我通过删除this.setLayout(null);来修复代码in the TestPanel class.TestPanel中。

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

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