在原始 GUI 框架内显示图像
Displaying Image Inside of Original GUI Frame
我有一个名为 SimpleGUI
的 GUI 表单,它只是一个 JPanel
。我的目标是在 JPanel
中显示我的 URL 图像。
当我尝试添加图像时,没有出现错误,但没有显示任何内容。如果我创建一个新的 JFrame
并将我的 JPanel
添加到其中,图像会显示。但是后来我有两个 Windows 打开。
如何以原始 SimpleGUI
格式显示图像? (下面的代码)
谢谢!
public class SimpleGUI extends JFrame {
private JPanel imagesPanel;
//private JFrame mainFrame;
public SimpleGUI() throws IOException {
super("GUI Page");
setContentPane(imagesPanel);
pack();
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(new Dimension(500, 500));
//mainFrame = new JFrame();
imagesPanel.setLayout(new GridLayout());
imagesPanel.setBounds(0,0,200,200);
//mainFrame.add(imagesPanel);
//mainFrame.setVisible(true);
BufferedImage img1 = null;
try
{
URL url1 = new URL("http://cosplayidol.otakuhouse.com/wp-content/uploads/2012/06/s-1-1.jpg");
URLConnection conn1 = url1.openConnection();
conn1.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
InputStream in1 = conn1.getInputStream();
img1 = ImageIO.read(in1);
} catch (IOException e)
{
e.printStackTrace();
}
JLabel wIcon = new JLabel(new ImageIcon(img1));
imagesPanel.add(wIcon);
}
}
主要方法:
public class Main {
public static void main(String[] args) {
// write your code here
try {
SimpleGUI simpleGUI = new SimpleGUI();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
}
您方法顶部的这段代码应该放在最后。这应该可以解决问题
setContentPane(imagesPanel);
pack();
setVisible(true);
编辑:除了解决问题外,我还应该解释为什么这样可以解决问题。问题是当您在方法开头调用 pack() 时,没有向 imagePanel 添加任何内容,因此 getPreferredSize()
的 return 值将非常小 / (0,0 ).添加组件后,可以适当调整大小
我有一个名为 SimpleGUI
的 GUI 表单,它只是一个 JPanel
。我的目标是在 JPanel
中显示我的 URL 图像。
当我尝试添加图像时,没有出现错误,但没有显示任何内容。如果我创建一个新的 JFrame
并将我的 JPanel
添加到其中,图像会显示。但是后来我有两个 Windows 打开。
如何以原始 SimpleGUI
格式显示图像? (下面的代码)
谢谢!
public class SimpleGUI extends JFrame {
private JPanel imagesPanel;
//private JFrame mainFrame;
public SimpleGUI() throws IOException {
super("GUI Page");
setContentPane(imagesPanel);
pack();
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(new Dimension(500, 500));
//mainFrame = new JFrame();
imagesPanel.setLayout(new GridLayout());
imagesPanel.setBounds(0,0,200,200);
//mainFrame.add(imagesPanel);
//mainFrame.setVisible(true);
BufferedImage img1 = null;
try
{
URL url1 = new URL("http://cosplayidol.otakuhouse.com/wp-content/uploads/2012/06/s-1-1.jpg");
URLConnection conn1 = url1.openConnection();
conn1.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
InputStream in1 = conn1.getInputStream();
img1 = ImageIO.read(in1);
} catch (IOException e)
{
e.printStackTrace();
}
JLabel wIcon = new JLabel(new ImageIcon(img1));
imagesPanel.add(wIcon);
}
}
主要方法:
public class Main {
public static void main(String[] args) {
// write your code here
try {
SimpleGUI simpleGUI = new SimpleGUI();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
}
您方法顶部的这段代码应该放在最后。这应该可以解决问题
setContentPane(imagesPanel);
pack();
setVisible(true);
编辑:除了解决问题外,我还应该解释为什么这样可以解决问题。问题是当您在方法开头调用 pack() 时,没有向 imagePanel 添加任何内容,因此 getPreferredSize()
的 return 值将非常小 / (0,0 ).添加组件后,可以适当调整大小