【问题标题】:Adding images to JFrame from other classes从其他类向 JFrame 添加图像
【发布时间】:2016-04-14 23:53:10
【问题描述】:

对于我正在做的一个项目,我必须创建 2 个类,其中一个创建一个 JFrame,另一个包含必须添加到 JFrame 的图像。

我的 JFrame 创建类名为 Driver,如下所示:

import javax.swing.JFrame;
import javax.swing.JTextField;
public class Driver {
    private JFrame win;
    private Animals animals = new Animals();
    private BirdButton nextBtn, enlargeBtn, shrinkBtn, moveToBtn;
    private JTextField field;

    public Driver() {
        win = new JFrame("Angry Animal Name Game");
        win.setBounds(100, 100, 600, 600);
        win.setLayout(null);
        win.setVisible(true);
        nextBtn = new BirdButton( "NEXT", 10, 10, animals);
        win.add(nextBtn, 0);
        enlargeBtn = new BirdButton( "ENLARGE", 10, 60, animals);
        win.add(enlargeBtn, 0);
        shrinkBtn = new BirdButton( "SHRINK", 10, 110, animals);
        win.add(shrinkBtn, 0);
        field = new JTextField();
        field.setBounds(10, 250, 100, 20);
        win.add(field, 0);
        moveToBtn = new BirdButton( "MOVETO", 10, 275, animals, field);
        win.add(moveToBtn, 0);
        win.add(animals, 0);
        animals.recenter();
        win.repaint();
    }
}

然后我有一个单独的类 Animals,其中包含一些鸟类的图像。我想让这张图片出现在我的 JFrame 上,但我的代码似乎没有那么热。动物长这样:

public class Animals extends Image {
    public Circle selector;
    public Image birds;

    public Animals(){
         birds = new Image(225,200,150,200);
         birds.setImage("AngryBirds.png");
         selector = new Circle(70,70,birds.getX(),birds.getY());
         selector.setThickness(5);
         birds.add(selector,0);
    } 

    public void recenter(){
         birds.setLocation((600-birds.getWidth()/2),(600-birds.getHeight()/2));
    }
}

既然 Animals extends Image 我可以使用这个命令来使 Animals 成为图像本身,或者我需要做什么?我不应该以任何方式修改驱动程序。

谢谢

经过一些回复后,我将我的 Animals 代码编辑为:

public class Animals extends Image {
    public Circle selector;

    public Animals(){
         new Image(225,200,150,200);
         setImage("AngryBirds.png");
         selector = new Circle(70,70,this.getX(),this.getY());
         selector.setThickness(5);
         this.add(selector,0);
    } 

    public void recenter(){
         this.setLocation((600-this.getWidth()/2),(600-this.getHeight()/2));
    }
}

【问题讨论】:

  • 您的新 Animals 类仍然扩展 Image。你没有提到这个 Image 类的任何内容——它是你的类吗?是java.awt.Image吗?
  • win.setLayout(null); 1) Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them 以及white space 的布局填充和边框。 2) 为了尽快获得更好的帮助,请发帖minimal reproducible exampleShort, Self Contained, Correct Example
  • 很抱歉,图片是教授给我们上的一门课。我似乎自己解决了这些问题,但@HovercraftFullOfEels 你的想法给了我动力。谢谢
  • "..你的想法给了我动力。谢谢" 提示:添加@HovercraftFullOfEels(或任何重要的@)到通知 新评论的人。

标签: java image swing class


【解决方案1】:

首先,确保 Animals 不会扩展 Image。动物不是任何形状或形式的图像,因此不应扩展它。您还需要澄清您正在使用的 Image 类。 Swing 组件将期望使用派生自 java.awt.Image 的图像,并且如果您有自己的名为 Image 的类,并且看起来您这样做(例如,birds = new Image(225,200,150,200);)立即重命名它以避免名称冲突。

现在要让 JFrame 使用图像,您必须创建它可以使用的图像,通常是 BufferedImage,通常通过 ImageIO.read(...) 获得——我在您的代码中没有看到 BufferedImage 使用,所以如果我是你,我会从那里开始。

看起来你的 Animals 类不应该扩展任何类型的图像,而是应该扩展某种类型的 Swing 组件,因为你似乎将它添加到你的 JFrame 中。

其他问题:

  • 您的驱动程序类代码使用null 布局。虽然空布局和setBounds() 对于 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但创建的 Swing GUI 越多,使用它们时遇到的困难就越严重。当 GUI 调整大小时,它们不会调整您的组件大小,它们是增强或维护的皇家女巫,放置在滚动窗格中时它们完全失败,在与原始不同的所有平台或屏幕分辨率上查看时它们看起来很糟糕.你的老师创建了这门课吗?如果是这样,我不得不怀疑他们是否知道自己在做什么。

【讨论】:

  • 首先,感谢您的回答,正是像您这样的人让这个社区变得如此棒。在查看您的答案后,我稍微编辑了我的代码,以使事情在我的老师需要的上下文中工作。你说动物是一种形象,但这就是我需要的。如果您愿意再次查看,我将编辑我的原始帖子以显示新代码
  • 我绝对明白你所说的空布局的意思,它确实让事情变得很尴尬。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-21
  • 2014-11-03
  • 2017-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多