Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向Jpanel添加对象_Java_Swing_Jframe_Jpanel - Fatal编程技术网

Java 向Jpanel添加对象

Java 向Jpanel添加对象,java,swing,jframe,jpanel,Java,Swing,Jframe,Jpanel,我知道如何将JButton添加到JPanel,但是我创建的类呢 我有这门课: public class Monster { private ImageIcon monster; private JButton b; public Monster() { monster = new ImageIcon("Monster.jpg"); b = new JButton(monster); b.setIcon(monste

我知道如何将JButton添加到JPanel,但是我创建的类呢

我有这门课:

public class Monster
{
    private ImageIcon monster;
    private JButton b;

    public Monster()
    {
        monster = new ImageIcon("Monster.jpg");
        b = new JButton(monster);
        b.setIcon(monster);
    }
}
我有另一个类,在这个类中,我想将图标添加到我的swing窗口中

import javax.swing.*;
import java.awt.*;
public class GameWindow
{
    private JFrame frame;
    private JPanel panel;
    private Monster monster;

    public GameWindow()
    {
        frame = new JFrame();
        panel = new JPanel();
        monster = new Monster();

        panel.add(monster); 

        frame.setContentPane(panel); 
        frame.setTitle("Game"); 
        frame.setSize(400,400); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

add()方法适用于JButtons,但不适用于我的Monster类。如何将我的怪物对象添加到我在GameWindow类中创建的Swing窗口中?

使您的类扩展
ImageIcon
JButton
。这是因为
JPanel
add()
方法需要
JComponent
使类扩展
ImageIcon
JButton
。这是因为
JPanel
add()
方法需要
JComponent
使类扩展
ImageIcon
JButton
。这是因为
JPanel
add()
方法需要
JComponent
使类扩展
ImageIcon
JButton
。这是因为
JPanel
add()
方法需要一个
JComponent
试试这个,因为
JPanel
接受一个
Swing UI
组件,这使得你的类
MonsterIcon
成为Swing的一部分(用普通人的话说)


试试这个,因为
JPanel
接受
Swing UI
组件,这使您的类
MonsterIcon
成为Swing的一部分(用外行的话说)


试试这个,因为
JPanel
接受
Swing UI
组件,这使您的类
MonsterIcon
成为Swing的一部分(用外行的话说)


试试这个,因为
JPanel
接受
Swing UI
组件,这使您的类
MonsterIcon
成为Swing的一部分(用外行的话说)


您应该使用Swing组件。扩展Swing组件(或任何其他Java类)的唯一原因是您希望重写其中一个类方法

你在怪物类中缺少一个方法

public class Monster
{
    private ImageIcon monster;
    private JButton b;

    public Monster()
    {
        monster = new ImageIcon("Monster.jpg");
        b = new JButton(monster);
        b.setIcon(monster);
    }

    public JButton getMonsterButton() {
        return b;
    }
}
GameWindow类中的添加行如下所示:

    panel.add(monster.getMonsterButton());

您应该使用Swing组件。扩展Swing组件(或任何其他Java类)的唯一原因是您希望重写其中一个类方法

你在怪物类中缺少一个方法

public class Monster
{
    private ImageIcon monster;
    private JButton b;

    public Monster()
    {
        monster = new ImageIcon("Monster.jpg");
        b = new JButton(monster);
        b.setIcon(monster);
    }

    public JButton getMonsterButton() {
        return b;
    }
}
GameWindow类中的添加行如下所示:

    panel.add(monster.getMonsterButton());

您应该使用Swing组件。扩展Swing组件(或任何其他Java类)的唯一原因是您希望重写其中一个类方法

你在怪物类中缺少一个方法

public class Monster
{
    private ImageIcon monster;
    private JButton b;

    public Monster()
    {
        monster = new ImageIcon("Monster.jpg");
        b = new JButton(monster);
        b.setIcon(monster);
    }

    public JButton getMonsterButton() {
        return b;
    }
}
GameWindow类中的添加行如下所示:

    panel.add(monster.getMonsterButton());

您应该使用Swing组件。扩展Swing组件(或任何其他Java类)的唯一原因是您希望重写其中一个类方法

你在怪物类中缺少一个方法

public class Monster
{
    private ImageIcon monster;
    private JButton b;

    public Monster()
    {
        monster = new ImageIcon("Monster.jpg");
        b = new JButton(monster);
        b.setIcon(monster);
    }

    public JButton getMonsterButton() {
        return b;
    }
}
GameWindow类中的添加行如下所示:

    panel.add(monster.getMonsterButton());


为什么不让一个类扩展
JButton
?您可以让您的类扩展JComponent并将这两个元素添加到其中!不要扩展,返回此对象,搜索Inheritace v.s.Composition为什么不创建一个扩展
JButton
的类?您可以使您的类扩展JComponent并将这两个元素添加到其中!不要扩展,返回此对象,搜索Inheritace v.s.Composition为什么不创建一个扩展
JButton
的类?您可以使您的类扩展JComponent并将这两个元素添加到其中!不要扩展,返回此对象,搜索Inheritace v.s.Composition为什么不创建一个扩展
JButton
的类?您可以使您的类扩展JComponent并将这两个元素添加到其中!不扩展,返回此对象,搜索Inheritace v.s.Composition除图标未显示外,此操作有效。我只得到一个非常小的空白JButton句子,使用
this.setIcon@user294698编辑了我的答案!这就是我错过的。感谢SGMART和ALL,而不是扩展<代码> JButton < /代码>,考虑一个创建并返回<代码> JButton < /C>的工厂方法。我只得到一个非常小的空白JButton句子,使用
this.setIcon@user294698编辑了我的答案!这就是我错过的。感谢SGMART和ALL,而不是扩展<代码> JButton < /代码>,考虑一个创建并返回<代码> JButton < /C>的工厂方法。我只得到一个非常小的空白JButton句子,使用
this.setIcon@user294698编辑了我的答案!这就是我错过的。感谢SGMART和ALL,而不是扩展<代码> JButton < /代码>,考虑一个创建并返回<代码> JButton < /C>的工厂方法。我只得到一个非常小的空白JButton句子,使用
this.setIcon@user294698编辑了我的答案!这就是我错过的。感谢SGMART和ALL,而不是扩展<代码> JButton < /代码>,考虑创建并返回<代码> JButton < /代码>的工厂方法。