You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将Reversi类的makeBoard方法迁移到Board类后如何获取JPanel添加到JFrame

实现方案

步骤1:新建Board类

直接继承JPanel,把原makeBoard的逻辑放到构造方法里,同时封装棋盘按钮数组,提供访问接口:

package reversi;

import java.awt.*;
import javax.swing.*;

// 直接继承JPanel,本身就是可直接添加的容器组件
public class Board extends JPanel {
    private JButton[][] squares = new JButton[8][8];

    public Board() {
        // 原makeBoard里的初始化逻辑移到这里
        setBackground(Color.GRAY);
        setBorder(BorderFactory.createBevelBorder(1));
        setPreferredSize(new Dimension(750, 700));
        setLayout(new GridLayout(8,8));

        for(int i = 0; i < 8; i++){
            for (int j = 0; j < 8; j++) {
                squares[i][j] = new JButton();
                squares[i][j].setBackground(Color.GREEN);
                add(squares[i][j]);
            }
        }
    }

    // 提供按钮数组的访问方法,方便外层Reversi类操作棋盘按钮
    public JButton[][] getSquares() {
        return squares;
    }
}

步骤2:修改Reversi类

移除原有makeBoard方法,调整成员变量和初始化逻辑:

package reversi;  

import java.awt.*;
import javax.swing.*;

public class Reversi 
{
    public JFrame frame;
    private JLabel userName1 = new JLabel("Enter First player: ");
    private JLabel userName2 = new JLabel("Enter Second player: ");
    private JTextField textUsername1 = new JTextField(15);
    private JTextField textUsername2 = new JTextField(15);
    private JButton startButton = new JButton("START");
    // 替换原有squares数组,改用Board实例
    private Board gameBoard;

    public Reversi()
    {
        makeFrame();
        // 原代码调用的makeMenuBar如果未实现可暂时注释
        // makeMenuBar();
    }

    private void makePlayerPanel()
    {
        JPanel userInterface = new JPanel(new GridBagLayout());
        userInterface.setBackground(Color.LIGHT_GRAY);
        userInterface.setBorder(BorderFactory.createBevelBorder(0));
        GridBagConstraints c = new GridBagConstraints();

        c.gridx = 0;
        c.gridy = 0; 
        userInterface.add(userName1, c);

        c.gridx = 1;
        userInterface.add(textUsername1, c);

        c.gridx = 0;
        c.gridy = 1;
        userInterface.add(userName2, c);

        c.gridx = 1;
        userInterface.add(textUsername2, c);

        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 2;
        c.anchor = GridBagConstraints.SOUTH;
        userInterface.add(startButton, c);

        userInterface.setBorder(BorderFactory.createTitledBorder(
        BorderFactory.createEtchedBorder(), "Players/Scoreboard"));
        JPanel wrapper = new JPanel();
        wrapper.add(userInterface);
        frame.add(wrapper, BorderLayout.LINE_END);
        frame.pack();
    }

    public void makeFrame()
    {
        frame = new JFrame("Reversi Game");
        frame.setSize(800,800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 实例化Board后直接添加到窗口内容窗格
        gameBoard = new Board();
        frame.add(gameBoard, BorderLayout.CENTER);
        makePlayerPanel();
        frame.setVisible(true);
    }

    // 后续需要操作棋盘按钮时,调用gameBoard.getSquares()即可拿到按钮数组
}

核心逻辑说明

  • Board类直接继承JPanel,属于Swing标准容器组件,实例化后可以直接调用frame.add()添加到任意窗口容器中,不需要额外提取JPanel
  • 按钮数组封装在Board类内部,通过getter方法对外提供访问能力,不破坏封装性的同时保留原有操作逻辑

内容的提问来源于stack exchange,提问作者YoungOne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 05:36:08