Java 在 JFrame 中显示图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20098124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying an image in a JFrame
提问by user2988879
I am currently learning Java, and I am stuck with something at the moment.
我目前正在学习 Java,目前我遇到了一些问题。
I was looking for a way to add an image to my JFrame. I found this on the internet:
我正在寻找一种将图像添加到 JFrame 的方法。我在网上找到了这个:
ImageIcon image = new ImageIcon("path & name & extension");
JLabel imageLabel = new JLabel(image);
And after implementing it to my own code, it looks like this (this is only the relevant part):
并在我自己的代码中实现后,它看起来像这样(这只是相关部分):
class Game1 extends JFrame
{
public static Display f = new Display();
public Game1()
{
Game1.f.setSize(1000, 750);
Game1.f.setResizable(false);
Game1.f.setVisible(true);
Game1.f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Game1.f.setTitle("Online First Person Shooter");
ImageIcon image = new ImageIcon("C:\Users\Meneer\Pictures\image.png");
JLabel imageLabel = new JLabel(image);
add(imageLabel);
}
}
class Display extends JFrame
{
}
When running this code, it doesn't give me any errors, but it also doesn't show the picture. I saw some questions and people having the same problem, but their code was completely different from mine, they used other ways to display the image.
运行此代码时,它不会给我任何错误,但也不会显示图片。我看到一些问题和有同样问题的人,但他们的代码和我的完全不同,他们使用其他方式来显示图像。
采纳答案by AJ.
do this after creating Jlabel
创建后执行此操作 Jlabel
imageLabel.setBounds(10, 10, 400, 400);
imageLabel.setVisible(true);
also set the layout to JFrame
还将布局设置为 JFrame
Game.f.setLayout(new FlowLayout);
回答by Vlad
You are adding the label to the wrong JFrame
. Also, move the setVisible()
to the end.
您将标签添加到错误的JFrame
. 另外,将 移到setVisible()
最后。
import javax.swing.*;
class Game1 extends JFrame
{
public static Display f = new Display();
public Game1()
{
// ....
Game1.f.add(imageLabel);
Game1.f.setVisible(true);
}
}
回答by Khobar
Also try to use image from resources, and not from hardcoded path from your PC You can look in here, where sombody asked similar question about images in Jframe: How to add an ImageIcon to a JFrame?
还尝试使用来自资源的图像,而不是来自您 PC 的硬编码路径您可以在这里查看,其中有人问了有关 Jframe 中图像的类似问题: 如何将 ImageIcon 添加到 JFrame?
回答by Sage
- You don't need to use another
JFrame
instance inside theGame
JFrame
: - Calling
setVisible(flag)
from the constructor is not preferable. Rather initialize yourJFrame
from outside and put yoursetVisible(true)
inside event dispatch thread to maintain Swing's GUI rendering rules usingSwingUtilities.invokeLater(Runnable)
- Do not give size hint by
setSize(Dimension)
of theJFrame
. Rather use proper layout with your component, callpack()
after adding all of your relevant component to theJFrame
. - Try using
JScrollPane
withJLabel
for a better user experience with image larger than the label's size can be.
- 您不需要在 内使用另一个
JFrame
实例Game
JFrame
: setVisible(flag)
从构造函数调用不是可取的。而是JFrame
从外部初始化您并使用setVisible(true)
内部事件调度线程来维护 Swing 的 GUI 渲染规则SwingUtilities.invokeLater(Runnable)
- 不要给尺寸暗示
setSize(Dimension)
的JFrame
。而是对您的组件使用正确的布局,pack()
在将所有相关组件添加到JFrame
. - 尝试使用
JScrollPane
withJLabel
以获得比标签尺寸更大的图像更好的用户体验。
All of the above description is made in the following example:
以上所有描述均在以下示例中进行:
class Game1 extends JFrame
{
public Game1()
{
// setSize(1000, 750); <---- do not do it
// setResizable(false); <----- do not do it either, unless any good reason
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Online First Person Shooter");
ImageIcon image = new ImageIcon("C:\Users\Meneer\Pictures\image.png");
JLabel label = new JLabel(image);
JScrollPane scrollPane = new JScrollPane(label);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
add(scrollPane, BorderLayout.CENTER);
pack();
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Game1().setVisible(true);
}
});
}
}
回答by alex2410
Your problem in next you add your JLabel
to Game1
but you display another Frame(Display f
). Change add(imageLabel);
to Game1.f.add(imageLabel);
.
接下来你的问题是你添加你的JLabel
,Game1
但你显示了另一个 Frame( Display f
)。更改add(imageLabel);
为Game1.f.add(imageLabel);
。
Recommendations:
建议:
1)according to your problem: Game1
extends JFrame
seems that Display
is also a frame, use only one frame to display content.
1)根据你的问题:Game1
extendsJFrame
似乎Display
也是一个框架,只用一个框架来显示内容。
2) use pack()
method instead of setSize(1000, 750);
2)使用pack()
方法代替setSize(1000, 750);
3)call setVisible(true);
at the end of construction.
3)setVisible(true);
施工结束时调用。
4)use LayoutManager
to layout components.
4)用于LayoutManager
布局组件。