Java JButton中的合成与继承

Java JButton中的合成与继承,java,swing,inheritance,Java,Swing,Inheritance,我想通过Swing创建一个简单的桌面游戏。我有一个JFrame和一个JPanel变量 我想向这个JPanel添加JButtons,但我想创建一个自己的类 我创建了一个扩展JButton(继承)的类: 所以我可以在JPanel中添加游戏场 但我想通过组合创建游戏场: public class GameField{ private JButton button; } 但在这门课中,我如何将游戏场添加到JPanel? 我可以通过compision解决这个问题吗?如果您希望您的游戏场对象有

我想通过Swing创建一个简单的桌面游戏。我有一个JFrame和一个JPanel变量

我想向这个JPanel添加JButtons,但我想创建一个自己的类

我创建了一个扩展JButton(继承)的类:

所以我可以在JPanel中添加游戏场

但我想通过组合创建游戏场:

public class GameField{

    private JButton button;

}
但在这门课中,我如何将游戏场添加到JPanel?
我可以通过compision解决这个问题吗?

如果您希望您的游戏场对象有JComponents,并且仍然能够将它们添加到其他JComponents,请让它们扩展
JPanel
,而不是
JButton

然后,您可以将游戏场对象作为JPanel与其他JComponents一起添加到JFrame中

但在这门课中,我如何将游戏场添加到JPanel?我能解决这个问题吗 比较问题

为此,您可以添加一个简单的getter,如下所示:

public class GameField{

    private JButton button;

    public GameField(String text) {
        button = new JButton(text);
        // do your stuff here
    }

    public JButton getButton() {
        return button;
    }
}
然后在GUI中:

public void createAndShowGUI() {
    JPanel panel = new JPanel(new GridLayout(5,5));
    panel.add(new GameField("Button # 1").getButton());
    panel.add(new GameField("Button # 2").getButton());
    ...
    JFrame frame = new JFrame("Game");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.getContentPane().add(panel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

编辑 你在评论中说:


谢谢,但在这种情况下,如果我想访问此字段(即。 getComponent(i)),我只能得到一个JButton,而不能得到一个游戏场

您可以保留包含
游戏场
对象的列表,也可以使用方法保留对
游戏场
对象的引用,如下例所示:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Demo {

    private void createAndShowGUI() {        
        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton button = (JButton)e.getSource();
                GameField gameField = (GameField)button.getClientProperty("GameField");
                if(gameField != null) {
                    System.out.println(gameField.getText());
                }
            }
        };

        GameField gameField1 = new GameField("Button # 1");
        gameField1.getButton().addActionListener(actionListener);

        GameField gameField2 = new GameField("Button # 2");
        gameField2.getButton().addActionListener(actionListener);

        JPanel content = new JPanel(new GridLayout());
        content.add(gameField1.getButton());
        content.add(gameField2.getButton());

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(content);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    class GameField {

        private String text;
        private JButton button;

        public GameField(String text) {
            this.text = text;
            button = new JButton(text);
            button.putClientProperty("GameField", GameField.this);
        }

        public JButton getButton() {
            return button;
        }

        public String getText() {
            return text;
        }        
    }

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

谢谢,但在这种情况下,如果我想访问此字段(即panel.getComponent(I)),我只能获得一个JButton,而不能获得游戏字段。@user2693979请查看我的编辑。希望对您有所帮助。谢谢,这帮了大忙。:)+1避免延伸回转组件。
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Demo {

    private void createAndShowGUI() {        
        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton button = (JButton)e.getSource();
                GameField gameField = (GameField)button.getClientProperty("GameField");
                if(gameField != null) {
                    System.out.println(gameField.getText());
                }
            }
        };

        GameField gameField1 = new GameField("Button # 1");
        gameField1.getButton().addActionListener(actionListener);

        GameField gameField2 = new GameField("Button # 2");
        gameField2.getButton().addActionListener(actionListener);

        JPanel content = new JPanel(new GridLayout());
        content.add(gameField1.getButton());
        content.add(gameField2.getButton());

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(content);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    class GameField {

        private String text;
        private JButton button;

        public GameField(String text) {
            this.text = text;
            button = new JButton(text);
            button.putClientProperty("GameField", GameField.this);
        }

        public JButton getButton() {
            return button;
        }

        public String getText() {
            return text;
        }        
    }

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