Java Swing-如何在Jpanel北部添加图像

Java Swing-如何在Jpanel北部添加图像,java,image,swing,Java,Image,Swing,我想在我的JPanel的NORTH部分添加图像,但它不起作用。 我该怎么办 class PanelGlowny extends JPanel { PanelGlowny() { this.setLayout(new BorderLayout()); ImageIcon imageurl = new ImageIcon("logo.jpg"); Image img = imageurl.getImage();

我想在我的
JPanel
NORTH
部分添加图像,但它不起作用。 我该怎么办

class PanelGlowny extends JPanel 
{    
    PanelGlowny()
    {        
        this.setLayout(new BorderLayout());
        ImageIcon imageurl = new ImageIcon("logo.jpg");
        Image img = imageurl.getImage();
        this.add(img, BorderLayout.NORTH);

    }
}

public class Formatka extends JFrame 
{    
    private PanelGlowny panel = new PanelGlowny();

    public Formatka()
    {    
        ...
        add(panel);
    } 
}

图像图标创建一个新图标
并将其添加到
JPanel
图像图标
创建一个新图标

并将其添加到
JPanel
以下是对代码的有效修改。您应该能够按原样运行它。本质上,您不能简单地将
ImageIcon
添加到
JPanel
。首先需要将其包装在
JLabel

import java.awt.BorderLayout;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Test2
{
    public static class PanelGlowny extends JPanel
    {

        public PanelGlowny( )
        {

            this.setLayout( new BorderLayout( ) );

            // incorporated @nIcE cOw's comment about loading classpath resources
            URL url = getClass().getResource("logo.jpg")
            ImageIcon imageicon = new ImageIcon( url );
            JLabel label = new JLabel( imageicon );

            this.add( label, BorderLayout.NORTH );

        }
    }

    public static void main( String[] args )
    {
        JFrame frame = new JFrame( );

        frame.add( new PanelGlowny( ) );

        frame.setSize( 400, 400 );

        frame.setVisible( true );
    }

}

这是对代码的有效修改。您应该能够按原样运行它。本质上,您不能简单地将
ImageIcon
添加到
JPanel
。首先需要将其包装在
JLabel

import java.awt.BorderLayout;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Test2
{
    public static class PanelGlowny extends JPanel
    {

        public PanelGlowny( )
        {

            this.setLayout( new BorderLayout( ) );

            // incorporated @nIcE cOw's comment about loading classpath resources
            URL url = getClass().getResource("logo.jpg")
            ImageIcon imageicon = new ImageIcon( url );
            JLabel label = new JLabel( imageicon );

            this.add( label, BorderLayout.NORTH );

        }
    }

    public static void main( String[] args )
    {
        JFrame frame = new JFrame( );

        frame.add( new PanelGlowny( ) );

        frame.setSize( 400, 400 );

        frame.setVisible( true );
    }

}

我把它加在二等兵杰勒贝尔·亚当身上PanelGlowny(){this.setLayout(new BorderLayout());ImageIcon imageurl=new ImageIcon(“logo.jpg”);//Image img=imageurl.getImage();adam=new JLabel(imageurl);this.add(adam,BorderLayout.NORTH);}'但它不显示任何图像。@user1304098:请不要试图在注释中发布代码,因为它会丢失所有格式设置。相反,编辑您的原始问题并将您的代码发布在那里,然后在此处添加注释,让我们知道您已经这样做了。Jeffrey的答案是正确的(1+),因此,如果您尝试使用它失败,您就有一个bug,需要在原始问题中显示上面格式良好的代码。我将其添加到私有JLabel adam;'PanelGlowny(){this.setLayout(new BorderLayout());ImageIcon imageurl=new ImageIcon(“logo.jpg”);//Image img=imageurl.getImage();adam=new JLabel(imageurl);this.add(adam,BorderLayout.NORTH);}'但它不显示任何图像。@user1304098:请不要试图在注释中发布代码,因为它会丢失所有格式设置。相反,编辑您的原始问题并将您的代码发布在那里,然后在此处添加注释,让我们知道您已经这样做了。Jeffrey的答案是正确的(1+),因此如果您尝试使用它失败,您就有一个bug,需要在原始问题中显示上面格式良好的代码。您的目录结构到底是什么?你的照片放在哪里?使用
getClass().getResource(“/logo.jpg”)
访问图像,这就是访问
应用程序资源的方式。这两个答案都太好了:-)您是否使用某些IDE来制作Swing应用程序?希望这能帮助你更多。你的目录结构是什么?你的照片放在哪里?使用
getClass().getResource(“/logo.jpg”)
访问图像,这就是访问
应用程序资源的方式。这两个答案都太好了:-)您是否使用某些IDE来制作Swing应用程序?希望这能对你有更多帮助。正如@nIcE cOw指出的,问题很可能是找不到你的
“logo.jpg”
图像。作为第一个测试,尝试提供图像的绝对路径。这肯定行得通。然后使用
getClass().getResource(“logo.jpg”)
如@nIcE cOw所述获得
URL
,您可以将其传递到
ImageIcon
构造函数中。此外,当您使用
getClass().getResource(“logo.jpg”)
时,您需要确保
logo.jpg
图像位于@nIcE cOw所述的某个位置,问题很可能是找不到您的
“logo.jpg”
图像。作为第一个测试,尝试提供图像的绝对路径。这肯定行得通。然后使用
getClass().getResource(“logo.jpg”)
像前面提到的@nIcE cOw一样获得一个
URL
,你可以将它传递到
ImageIcon
构造函数中。另外,当你使用
getClass().getResource(“logo.jpg”)
时,你需要确保
logo.jpg
图像在你的屏幕上