Entonces tengo un método makeBoard. Quiero crear una clase llamada Board y mover el método makeBoard a la clase Board, donde aún agrega el panel devuelto al panel de contenido de JFrame. No estoy seguro de cómo obtener el JPanel de la clase Board en el panel de contenido de JFrame en la clase reversi. No estoy seguro de cómo proceder con esto.

package reversi;  
 
import java.io.*;
import java.util.*;
import java.lang.*;
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");
    private JButton [][] squares = new JButton[8][8];
    
     
    public Reversi()
    {
        makeFrame();
        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();
    }
     
    private void makeBoard()
    {
        JPanel board = new JPanel();
        board.setBackground(Color.GRAY);
        board.setBorder(BorderFactory.createBevelBorder(1));
        board.setPreferredSize(new Dimension(750, 700));
        
        board.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);
           board.add(squares[i][j]);
        
           }
        }
         
        frame.add(board, BorderLayout.CENTER);
        frame.pack();
    }
    
    public void makeFrame()
    {
        frame = new JFrame("Reversi Game");
        frame.setSize(800,800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        makeBoard();
        makePlayerPanel();
     }
    
}
0
YoungOne 7 may. 2021 a las 20:06

2 respuestas

La mejor respuesta

Lo hice así:

public class Board {
    
    private JButton [][] squares = new JButton[8][8];
    public JFrame frame;
    public JPanel makeBoard()
    {
        JPanel board = new JPanel();
        board.setBackground(Color.GRAY);
        board.setBorder(BorderFactory.createBevelBorder(1));
        board.setPreferredSize(new Dimension(750, 700));
        
        board.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);
           board.add(squares[i][j]);
        
           }
        }
       return board;  
      
    }
    

}

En Reversi hacer así:

        Board b = new Board();
        
        frame.add(b.makeBoard(), BorderLayout.CENTER);
        frame.pack(); 
1
Ahmed Alhameli 7 may. 2021 a las 17:31
public class Board {

public void makeBoard(JPanel board)
    {
        board.setBackground(Color.GRAY);
        board.setBorder(BorderFactory.createBevelBorder(1));
        board.setPreferredSize(new Dimension(750, 700));
        
        board.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);
           board.add(squares[i][j]);
        
           }
        }
         
        frame.add(board, BorderLayout.CENTER);
        frame.pack();
    }    
}

En clase Reversi

public class Reversi 
{
....
Board board = new Board();
board.makeBoard(new JPanle());
}
1
Syed Mohib Uddin 7 may. 2021 a las 17:19