Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将方法从一个类移动到另一个类_Java - Fatal编程技术网

Java 将方法从一个类移动到另一个类

Java 将方法从一个类移动到另一个类,java,Java,所以我有一个方法makeBoard。我想创建一个名为Board的类,并将makeBoard方法移动到class Board中,在那里它仍然会将返回的面板添加到JFrame的内容窗格中。我不知道如何将JPanel从Board类放到reversi类的JFrame内容窗格上。我不知道该怎么做 package reversi; import java.io.*; import java.util.*; import java.lang.*; import java.awt.*; import j

所以我有一个方法makeBoard。我想创建一个名为Board的类,并将makeBoard方法移动到class Board中,在那里它仍然会将返回的面板添加到JFrame的内容窗格中。我不知道如何将JPanel从Board类放到reversi类的JFrame内容窗格上。我不知道该怎么做

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();
     }
    
}
package-reversi;
导入java.io.*;
导入java.util.*;
导入java.lang.*;
导入java.awt.*;
导入javax.swing.*;
公共类反向
{
公共框架;
私有jlabelusername1=新JLabel(“输入第一个播放器:”);
私有jlabelusername2=新JLabel(“输入第二个播放器:”);
私有JTextField textUsername1=新的JTextField(15);
私有JTextField textUsername2=新的JTextField(15);
私有JButton startButton=新JButton(“开始”);
私有JButton[][]正方形=新JButton[8][8];
公共资源(i)
{
makeFrame();
makeMenuBar();
}
私有void makePlayerPanel()
{
JPanel userInterface=newjpanel(newgridbaglayout());
用户界面.背景(颜色.浅灰色);
userInterface.setboorder(BorderFactory.createBevelOrder(0));
GridBagConstraints c=新的GridBagConstraints();
c、 gridx=0;
c、 gridy=0;
添加(userName1,c);
c、 gridx=1;
添加(textUsername1,c);
c、 gridx=0;
c、 gridy=1;
添加(userName2,c);
c、 gridx=1;
添加(textUsername2,c);
c、 gridx=0;
c、 gridy=2;
c、 网格宽度=2;
c、 锚点=GridBagConstraints.SOUTH;
添加(startButton,c);
userInterface.setBorder(BorderFactory.createTitledBorder(
createEtchedBorder(),“玩家/记分板”);
JPanel包装器=新的JPanel();
add(userInterface);
添加(包装,边框布局,线条结束);
frame.pack();
}
私有void makeBoard()
{
JPanel board=新的JPanel();
板。立根背景(颜色。灰色);
board.SetBordOrder(BorderFactory.createBevelBorder(1));
板。设置首选尺寸(新尺寸(750700));
board.setLayout(新网格布局(8,8));
对于(int i=0;i<8;i++){
对于(int j=0;j<8;j++)
{
正方形[i][j]=新的JButton();
正方形[i][j].退根(颜色.绿色);
加上(方格[i][j]);
}
}
添加(板、边框布局、中心);
frame.pack();
}
公共void makeFrame()
{
框架=新的JFrame(“反向游戏”);
框架设置尺寸(800800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
makeBoard();
makePlayerPanel();
}
}
我是这样做的:

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;  
      
    }
    

}
        Board b = new Board();
        
        frame.add(b.makeBoard(), BorderLayout.CENTER);
        frame.pack(); 
我是这样做的:

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;  
      
    }
    

}
        Board b = new Board();
        
        frame.add(b.makeBoard(), BorderLayout.CENTER);
        frame.pack();