Composition versus Inheritance in JButton

I want to create a simple board game from Swing. I have a JFrame and a JPanel variable.

I want to add JButtons to this JPanel, but I would like to create my own class.

I created a class that extends JButton (inheritance):

public class GameField extends JButton {...}

So, I can add GameFields to JPanel.

But I would like to create GameFields by composition:

public class GameField{

    private JButton button;

}

But in this class, how can I add GameField to JPanel? Can I solve this problem by compiling?

+3
source share
2 answers

But in this class, how can I add GameField to JPanel? Can I solve this problem? problem by compiling?

You do this by adding a simple getter as follows:

public class GameField{

    private JButton button;

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

    public JButton getButton() {
        return button;
    }
}

Then in your 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);
}

Edit

You indicated in the comment:

, , (. panel.getComponent(i)), JButton, GameField.

GameField, putClientProperty(), GameField , :

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();
            }
        });
    }    
}
+6

, GameField JComponents JComponents, JPanel JButton.

GameField JPanels JComponents JFrame.

0

All Articles