如何在 Java 应用程序中显示图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2943680/
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
How to display an image in a Java application
提问by Nubkadiya
I want to display an image in my Java application. I found a code to download the image from the webserver and show it in the jframe.
我想在我的 Java 应用程序中显示一个图像。我找到了一个从网络服务器下载图像并将其显示在 jframe 中的代码。
I want to use a label to show the image or something else. It shouldn't be JFrame.
我想使用标签来显示图像或其他内容。它不应该是 JFrame。
Here is my code:
这是我的代码:
Image image = null;
try {
URL url = new URL(
"http://www.personal.psu.edu/acr117/blogs/audrey/images/image-2.jpg");
image = ImageIO.read(url);
} catch (IOException e) {
}
// Use a label to display the image
JFrame frame = new JFrame();
JLabel lblimage = new JLabel(new ImageIcon(image));
frame.getContentPane().add(lblimage, BorderLayout.CENTER);
frame.setSize(300, 400);
frame.setVisible(true);
Can someone help me?
有人能帮我吗?
采纳答案by Haldean Brown
Using the code you provided, you already have the image in a JLabel
called lblimage
. You can now add that to a panel or any other container object, like so:
使用您提供的代码,您已经在JLabel
被调用的lblimage
. 您现在可以将其添加到面板或任何其他容器对象,如下所示:
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(lblimage);
// add more components here
frame.add(mainPanel);
frame.setVisible(true);
You can treat that label (lblimage
) just like you would any normal component.
您可以像对待lblimage
任何普通组件一样对待该标签 ( )。
回答by camickr
and i want to add more and more items (buttons and labels ) to my GUI. can you please help me
我想在我的 GUI 中添加越来越多的项目(按钮和标签)。你能帮我么
Your question has nothing to do with labels and images. You need to understand how to use Layout Managers to organize the components you add to a frame.
您的问题与标签和图像无关。您需要了解如何使用布局管理器来组织添加到框架中的组件。
So start by reading the Swing tutorial on Layout Managers. There are plenty of examples you can download and play with. Also you can nest different layout managers on different panels.
所以首先阅读关于布局管理器的 Swing 教程。您可以下载并使用大量示例。您也可以在不同的面板上嵌套不同的布局管理器。
回答by OscarRyz
Your code already does what you need ( to display it in other than a JFrame - that's a JLabel - )
您的代码已经完成了您的需要(在 JFrame 以外的地方显示它 - 那是 JLabel - )
For what you say in your comments, to try to drag and drop and add more components I think what you need is to use a GUI BUilder to achieve what you want ( which is not only show the image but add other components )
对于您在评论中所说的,尝试拖放和添加更多组件我认为您需要使用 GUI BUilder 来实现您想要的(不仅显示图像而且添加其他组件)
I would recommend you yo use IntelliJ IDEA's GUI Builder
我建议你使用IntelliJ IDEA的GUI Builder
In order to have a complete overview on how Swing works and what can you with it, you can also take a look at: The swing tutorial
为了全面了解 Swing 的工作原理以及您可以使用它做什么,您还可以查看:Swing 教程
回答by trashgod
I would add two notes to the other useful answers:
我会在其他有用的答案中添加两个注释:
1) Don't swallow exceptions; write to a log or at least print a stack trace.
1)不要吞下异常;写入日志或至少打印堆栈跟踪。
catch (IOException e) { e.printStackTrace(); }
2) Instead of writing frame.setSize(300, 400)
, use pack()
, which "Causes this Window
to be sized to fit the preferred size and layouts of its subcomponents." In this way, the picture's original dimensions will be used. This is also the foundation of @camickr's suggestion to read about Layout Managers.
2) 而不是写frame.setSize(300, 400)
,使用pack()
,它“导致它的Window
大小适合其子组件的首选大小和布局。” 这样,将使用图片的原始尺寸。这也是@camickr 建议阅读布局管理器的基础。
回答by exodrifter
You could try doing this instead:
你可以尝试这样做:
import javax.swing.ImageIcon;
...
JLabel image = new JLabel(new ImageIcon("imageName.png"));
...
frame.add(image);
I find it to be much easier to do :D
我发现它更容易做到:D
EDIT: Upon reading the question for the 4th time, I realized that this is exactly what you did in the question. Now, I'm having trouble figuring out what exactly you're asking for.
编辑:在第四次阅读问题时,我意识到这正是您在问题中所做的。现在,我很难弄清楚你到底在要求什么。
回答by Deathstar
Try this:
尝试这个:
JLabel lblNewLabel_1 = new JLabel("");
lblNewLabel_1.setIcon(new ImageIcon(MainMenu.class.getResource("/Images/Bugs.png")));