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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:08:56  来源:igfitidea点击:

Displaying an image in a JFrame

javaimageswingjframe

提问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

  1. You don't need to use another JFrameinstance inside the GameJFrame:
  2. Calling setVisible(flag)from the constructor is not preferable. Rather initialize your JFramefrom outside and put your setVisible(true)inside event dispatch thread to maintain Swing's GUI rendering rules using SwingUtilities.invokeLater(Runnable)
  3. Do not give size hint by setSize(Dimension)of the JFrame. Rather use proper layout with your component, call pack()after adding all of your relevant component to the JFrame.
  4. Try using JScrollPanewith JLabelfor a better user experience with image larger than the label's size can be.
  1. 您不需要在 内使用另一个JFrame实例GameJFrame
  2. setVisible(flag)从构造函数调用不是可取的。而是JFrame从外部初始化您并使用setVisible(true)内部事件调度线程来维护 Swing 的 GUI 渲染规则SwingUtilities.invokeLater(Runnable)
  3. 不要给尺寸暗示setSize(Dimension)JFrame。而是对您的组件使用正确的布局,pack()在将所有相关组件添加到JFrame.
  4. 尝试使用JScrollPanewithJLabel以获得比标签尺寸更大的图像更好的用户体验。

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 JLabelto Game1but you display another Frame(Display f). Change add(imageLabel);to Game1.f.add(imageLabel);.

接下来你的问题是你添加你的JLabelGame1但你显示了另一个 Frame( Display f)。更改add(imageLabel);Game1.f.add(imageLabel);

Recommendations:

建议:

1)according to your problem: Game1extends JFrameseems that Displayis also a frame, use only one frame to display content.

1)根据你的问题:Game1extendsJFrame似乎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 LayoutManagerto layout components.

4)用于LayoutManager布局组件。