将图片添加到JFrame [英] Add a picture to a JFrame

查看:151
本文介绍了将图片添加到JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的就是将图片添加到 JFrame

All I am trying to do is add a picture to a JFrame.

我真的很困惑,并且真的不明白......我已经在这个网站上查找了所有可能的问题,查看了其他java的东西,比如论坛。我尽我所能,现在我必须请求帮助。我希望代码清晰易读。感谢您的帮助。

I am really confused and don't really understand... I have looked up every possible question on this site, looked on other java stuff, such as forums. I tried my best and now I must ask guys for help. I hope the code is clean and easy to read. Thanks for the help.

package zeus;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;

public class Main extends JFrame{

    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;
    public static final int SCALE = 1;

    public static void Launch(){

        JFrame xF = new JFrame("xFrame");
        xF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        xF.setResizable(false);
        xF.setVisible(true);
        xF.setSize(WIDTH*SCALE,HEIGHT*SCALE);
        xF.setLocationRelativeTo(null);
        xF.add(new JLabel(new ImageIcon("/Clicker/xS/cow.png")));

    }

    public static void main(String[] args){

        Launch();

    }

}

非常抱歉混乱,eclipse显示没有错误,我也试图打开一个带有图像的JFrame,我最终可以创建一个按钮来改变一个int值。

Very sorry for the confusion, eclipse shows no error, also I am trying to open a JFrame with an image on it, that I can eventually use to create into a button to change a value of a int value.

推荐答案

我能看到的最大问题是...

The biggest issues I can see are...


  • JFrame ,但实际上并未使用它...

  • 依赖静态当不是真的需要...

  • 在实际开始添加之前调用 setVisible 。实际上,通常在添加任何内容之前以及在它被显示之后操纵框架属性...

  • Extending from JFrame, but not actually using it...
  • Reliance on static when not really required...
  • Calling setVisible before anything has actually begin added. In fact, generally trying to manipulate the frame properties before anything was added to it and after it was made visible...

public class Main {

public class Main {

 public static void main(String[] args){

     EventQueue.invokeLater(new Runnable() {
         public void run() {

             JFrame xF = new JFrame("xFrame");
             xF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             xF.add(new JLabel(new ImageIcon("/Clicker/xS/cow.png")));
             xF.setResizable(false);
             xF.setSize(WIDTH*SCALE,HEIGHT*SCALE);
             xF.setLocationRelativeTo(null);
             xF.setVisible(true);

          }
     }
 }

}

但是既然你从未真正描述过什么问题你有,这些都是猜测......

But since you never actually described what problems you were having, these are all guesses...

这篇关于将图片添加到JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆