【问题标题】:Can't Get ImageIcon to Display无法让 ImageIcon 显示
【发布时间】:2015-07-31 11:20:50
【问题描述】:

我查看了大量其他问题,其中人们遇到的问题与我在这里遇到的问题相似(大多数解决方案都是通过简单的错误找到的),但我终其一生都无法弄清楚为什么我的图形会失败t 显示在我的 jframe 中。我对 Java Graphics 很陌生,所以我很感激我能得到的所有帮助。 (如果有人认为这是一个重复的问题,我只要求你等到我得到答案后再关闭它)

哦,还有,当我运行程序时,它告诉我调用了 repaint 方法,如果这有帮助的话

package game.try5;

import java.awt.Dimension;
import java.net.URL;

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

public class Window {

    public Window() {
        JFrame frame = new JFrame("Epic Game");
        frame.setSize(800,600);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GamePanel panel = new GamePanel();
        frame.add(panel);

        frame.setVisible(true);
    }

    public static void main(String[] args){
        Window window = new Window();
    }
}   

.

package game.try5;

import java.awt.Graphics;

import javax.swing.JPanel;

public class GamePanel extends JPanel{
    GameObject go = new GameObject(0,0,false,"Dog.jpg");

    public GamePanel(){
        repaint();
        System.out.println("Repaint method called");
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(go.getImg().getImage(), go.getxLoc(), go.getyLoc(), 50, 50, null);
        System.out.println("Graphics method called");
    }
}

.

package game.try5;

import java.awt.Dimension;
import java.net.URL;

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

public class Window {

    public Window() {
        JFrame frame = new JFrame("Epic Game");
        frame.setSize(800,600);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GamePanel panel = new GamePanel();
        frame.add(panel);

        frame.setVisible(true);
    }

    public static void main(String[] args){
        Window window = new Window();
    }
}   

【问题讨论】:

  • 1) 您复制了两次Window,但没有GameObject。 2) Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them 以及white space 的布局填充和边框。 3) 注释掉frame.setLayout(null);,你很可能会看到这个游戏。
  • 你是对的,当我删除“frame.setLayout(null)”行时它起作用了。当你说我复制了两次“Window”并且没有 GameObject 时,你是什么意思?我并没有真正理解你的意思,另外,如果没有声明默认布局是什么?
  • “你说我复制了两次“Window”并且没有GameObject是什么意思?”问题中的代码。 ;) “如果没有声明,默认布局是什么?” 这取决于您指的是哪个组件的哪个(版本)。对于Frame(可能还有JFrame - 不太记得),它曾经是FlowLayout,但现在是BorderLayout。对于JPanel,它是FlowLayout。我真的不喜欢依赖任何“默认”布局,因为 Sun/Oracle 很少在 Java 文档中声明它们,因此它们应该被视为“开放更改”。

标签: java swing graphics jframe imageicon


【解决方案1】:

评论frame.setLayout(null);,您很可能会看到该游戏。

  1. JFrame 的默认布局当前是BorderLayout
  2. 添加到BorderLayout 且没有约束的对象默认为CENTER
  3. BorderLayoutCENTER 中的对象被拉伸到可用的高度和高度。
  4. 由于代码调用 frame.setSize(800,600);,可用的宽度和高度是 800x600 像素减去框架装饰。

更好的全方位方法是:

  1. 让自定义绘制组件覆盖 getPreferredSize() 以返回合理的值。
  2. 将组件添加到容器中。
  3. 最后,打包顶层窗口。这将使框架成为显示内容所需的最小尺寸。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-19
    • 2012-06-08
    • 2014-05-04
    • 2019-04-18
    • 2023-03-20
    • 1970-01-01
    • 2015-10-08
    • 2020-11-26
    相关资源
    最近更新 更多