比较来自世界各地的卖家的域名和 IT 服务价格

Java Swing - 如何添加北的图像 Jpanel

我想添加一个图像
NORTH

部分矿井。
JPanel

, 但它不起作用。
我该怎么办?


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/;
}
}
已邀请:

君笑尘

赞同来自:

以下是您的代码的工作修改。 你必须能够像它一样运行它。 事实上,你不能只是添加
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 /;
}

}

小姐请别说爱

赞同来自:

创建新的
http://docs.oracle.com/javase/ ... %2529
来自你的
ImageIcon

并将其添加到您的
JPanel

.

要回复问题请先登录注册