简体   繁体   English

添加图像时JFrame不起作用

[英]JFrame don't work when adding an Image

I'm having a problem that seems a little bit strange. 我遇到一个似乎有点奇怪的问题。 When I'm adding a new ImageIcon and try to run the program it just gives me a gray screen and no objects are added. 当我添加一个新的ImageIcon并尝试运行该程序时,它只给我一个灰色屏幕,并且没有添加任何对象。

public class Ctester {

public Ctester(){
    Frame();
}

public void Frame(){

    JFrame fr = new JFrame();
    fr.setVisible(true);
    fr.setSize(500, 500);
    fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    fr.setResizable(false);

    JPanel p = new JPanel(new GridBagLayout());
    ImageIcon icon = new ImageIcon(getClass().getResource("zippo.jpg"));

    JLabel l = new JLabel(icon)
    JButton bm1 = new JButton("hellu");

    p.add(l);
    p.add(bm1);
    fr.add(p);


}

public static void main(String[]args){
    new Ctester();
}
}

But if I remove the line: 但是,如果我删除该行:

    ImageIcon icon = new ImageIcon(getClass.getResource("zippo.jpg"));

then it works perfect. 那就完美了。 I'm not getting any error messages and i been looking for a while but I could only find that the problem might be something with the gridbaglayout. 我没有收到任何错误消息,并且已经寻找了一段时间,但我只能发现问题可能出在gridbaglayout上。

How can i solve it or do I have to change layout? 我该如何解决?还是必须更改布局? (this is just a simple code based of the original as an example so any solutions that not include having to change layout is highly appreciated) (这只是一个基于原始代码的简单代码作为示例,因此高度赞赏任何无需更改布局的解决方案)

Most of the code is wrong: 大多数代码是错误的:

  1. Swing components should be create on the Event Dispatch Thread (EDT). Swing组件应在事件调度线程(EDT)上创建。

  2. The frame should be made visible AFTER all the components have been added to the frame. 将所有组件添加到框架后,应使框架可见。

  3. You are attempting to use a GridBagLayout, but you aren't using any GridBagConstraints when you add the components. 您尝试使用GridBagLayout,但是在添加组件时未使用任何GridBagConstraints。

  4. Method names (Frame) should NOT start with an upper case character. 方法名称(框架)不应以大写字母开头。

Read the Swing Tutorial for Swing basics. 阅读Swing教程,了解Swing的基础知识。

You can find working examples in: 您可以在以下位置找到工作示例:

  1. How to Use GridBagLayout 如何使用GridBagLayout
  2. How to Use Icons 如何使用图标

so any solutions that not include having to change layout is highly appreciated 因此,不需更改版面的任何解决方案均受到高度赞赏

Start with the working examples and make changes for your requirements. 从工作示例开始,并根据您的要求进行更改。 If you start with better structured code you will have less problems. 如果您从更好的结构化代码开始,那么问题将会更少。

If something draws correctly after a window resize or minimize/maximize that is a sure sign of a race condition because you are not starting your GUI on the event dispatcher thread. 如果在调整窗口大小或最小化/最大化之后正确绘制某些图形,则这肯定是竞​​争状况的标志,因为您没有在事件分派器线程上启动GUI。 Your main problem is you are calling setVisible() way to early, don't call setVisible() until after you have added all components to your top-level container. 您的主要问题是您要尽早调用setVisible()的方式,直到将所有组件添加到顶级容器之后才调用setVisible()。 The other problem is you are not starting your GUI on the event dispatcher thread. 另一个问题是您没有在事件分配器线程上启动GUI。 Please see the main method below in the fixed code: 请在固定代码中查看下面的主要方法:

public class Ctester {
    public Ctester() {
        Frame();
    }

    public void Frame() {
        JFrame fr = new JFrame();
        fr.setSize(500, 500);
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fr.setResizable(false);

        JPanel p = new JPanel(new GridBagLayout());

        JLabel l = new JLabel("label");
        JButton bm1 = new JButton("hellu");

        p.add(l);
        p.add(bm1);
        fr.add(p);

        fr.setVisible(true);
    }

     public static void main(String[] args) {
         SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                 new Ctester();
            }
        });
    }
}

Try this code you might want to put that first line of code in a try catch just in case that it doesn't find the image. 尝试使用此代码,您可能希望将第一行代码放在try catch中,以防万一找不到图片。

URL iconURL = getClass().getResource("/some/package/favicon.png");
// iconURL is null when not found
ImageIcon icon = new ImageIcon(iconURL);
fr.setIconImage(icon.getImage());

Also use a .ico file if you are only using this program on Windows but use a .png if it is going to be multi-platform 如果仅在Windows上使用此程序,也请使用.ico文件;如果它将跨平台使用,请使用.png文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM