Reputation: 29
So I have a method makeBoard. I want to create a class called Board and move the makeBoard method into class Board, where it still adds the returned panel to the content pane of JFrame. Im not sure how to get the JPanel from Board class onto the JFrame content pane on the reversi class. Not sure how to proceed about this.
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();
}
}
Upvotes: 0
Views: 83
Reputation: 63
i did it like this:
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;
}
}
in Reversi do like this:
Board b = new Board();
frame.add(b.makeBoard(), BorderLayout.CENTER);
frame.pack();
Upvotes: 1
Reputation: 716
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();
}
}
In Reversi class
public class Reversi
{
....
Board board = new Board();
board.makeBoard(new JPanle());
}
Upvotes: 1