【问题标题】:What is the difference between a JFrame and a JInternalFrame?JFrame 和 JInternalFrame 有什么区别?
【发布时间】:2022-01-23 05:12:27
【问题描述】:

我正在尝试制作太空入侵者游戏

public static SpaceInvaders spaceinvaders;

public static Renderer renderer;

// Constructor
public SpaceInvaders() {
    JInternalFrame frame = new JInternalFrame();
    
    renderer = new Renderer();
    frame.add(renderer);
    frame.setSize(WIDTH, HEIGHT);
    frame.setResizable(false);
    frame.setVisible(true);
    
    gameDisplay g = new gameDisplay(); // a JFrame Form
    g.setVisible(true);
    g.gameScreen.add(frame); // gameScreen is a JPanel
    renderer.repaint();

}

public void repaint (Graphics g) {
    g.setColor(Color.red);
    g.fillRect(0, 0, WIDTH, HEIGHT);
}

如果我运行它,我会得到一个填充了红色矩形的对话框的预期结果。但是如果我用 JFrame 替换 JInternalFrame,我会收到一条错误消息

IllegalArgumentException:向容器添加窗口

那么JInternalFrame和JFrame有什么区别,为什么我可以给前者加渲染器却不能给后者呢?

【问题讨论】:

  • JFrame 是附加到本地对等点的“顶级”容器。就其本身而言,它不能添加到其他容器中(如您所见)。 JInternalFrame 是一个普通容器,被装饰成看起来像一个窗口(或至少一个用作“文档”界面的容器)
  • 哦,不要这样覆盖repaint
  • @MadProgrammer 哦,好的,谢谢。以这种方式覆盖重绘有什么问题?
  • 我不确定它来自哪里,repaint(Graphics) 似乎不是基于 Swing 的方法,但一般来说,repaint(在 Swing 内)用于安排新的绘制为组件传递

标签: java swing graphics


【解决方案1】:

全貌是 (J)Frame、(J)Dialog 和 (J)Window 是特殊的Components,它们在某种程度上与平台本机“对等体”密切相关。而且你不能混合这些同行。您可能想了解一些有关 OS 工具包及其提供的 UI 窗口服务的内容。

Swing 的设计目的是尽可能减少对操作系统工具包的依赖。但是程序不能在不使用 OS 工具包的情况下在屏幕上显示窗口

【讨论】:

    猜你喜欢
    • 2011-07-29
    • 2018-10-17
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 2014-02-25
    • 2010-10-02
    • 2011-12-12
    相关资源
    最近更新 更多