从另一个类的构造函数中显示JFrame

时间:2018-03-07 17:00:48

标签: java swing jframe

我试图摆弄连接四游戏,我试图修改它以使用户选择他/她喜欢它是双人游戏还是人工智能并选择谁先转弯(在现有代码第一轮是随机的)。所以,我制作了两个jframe,将这些作为用户的输入(单选按钮)。但是,在创建它的实例后,我无法使原始java显示。这就是我现在所拥有的:

这是java中的构造函数,它为JFrame调用方法setVisible(true):

public class testAI2 extends JFrame implements ActionListener
{
    JLabel lblPlayer = new JLabel("Player: ");
    JLabel lblCurrentPlayer = new JLabel("Blue");
    JPanel pnlMenu = new JPanel();
    JPanel pnlBoards = new JPanel();
    JButton btnNewGame2 = new JButton("New Game");

    Board[][] Boards = new Board[7][6];

    boolean winnerExists = false;
    int currentPlayer = 1;
    boolean AI;

    public testAI2(boolean AI)
    {
        super("Four In A Line");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        chooseTurn turn = new chooseTurn();
        currentPlayer = (int) turn.getTurnNumber();

        this.AI = AI;

        btnNewGame2.addActionListener(this);
        switch(currentPlayer)
        {
            case 1:
                lblCurrentPlayer.setForeground(Color.blue);
                lblCurrentPlayer.setText("Blue");
                break;
            case 2:
                lblCurrentPlayer.setForeground(Color.red);
                lblCurrentPlayer.setText("Red");
                break;
        }
        pnlMenu.add(btnNewGame2);
        pnlMenu.add(lblPlayer);
        pnlMenu.add(lblCurrentPlayer);

        pnlBoards.setLayout(new GridLayout(6, 7));

        for(int j = 0; j < 6; j++)
            for(int i = 0; i < 7; i++)
            {
                Boards[i][j] = new Board(i, j);
                Boards[i][j].addActionListener(this);
                pnlBoards.add(Boards[i][j]);
            }

        add(pnlMenu, BorderLayout.NORTH);
        add(pnlBoards, BorderLayout.CENTER);    
        setSize(500, 500);
        setVisible(true);

        if(currentPlayer == 2 && AI) insertTo(minimax());
    }

PlayerorAI.java

private void playerSubmitActionPerformed(java.awt.event.ActionEvent evt) {                                             
        if(twoPlayer.isSelected()){
            setPlayerNumber(2);
            chooseTurn turn = new chooseTurn();
            turn.setVisible(true);
            turn.setLocationRelativeTo(null);
            dispose();
        }
        else if (vsAi.isSelected()){
            setPlayerNumber(1);
            chooseTurn turn = new chooseTurn();
            turn.setVisible(true);
            turn.setLocationRelativeTo(null);
            dispose();
            }
        else
            JOptionPane.showMessageDialog(null, "Please Select One!", "Choose Players Error", JOptionPane.ERROR_MESSAGE);
    }  

chooseTurn.java

private void turnSubmitActionPerformed(java.awt.event.ActionEvent evt) {                                           
        PlayerorAI players = new PlayerorAI();
        int playersNo = players.getPlayerNumber();
        testAI2 test;
        if(firstTurn.isSelected()){
            setTurnNumber(1);            
            if (playersNo == 2){
                test = new testAI2(false);
            }
            dispose();
        }
        else if (secondTurn.isSelected()){
            setTurnNumber(2);
            if (playersNo == 1){
                test = new testAI2(true);
            }
            dispose();
        }
        else
        JOptionPane.showMessageDialog(null, "Please Select One!", "Choose Players Error", JOptionPane.ERROR_MESSAGE);
    }

现在,在原始代码中,他有一个main,他调用这个构造函数(new TestAI2(true) or new TestAI2(false)),它显示得很好,但是当我从chooseTurn.java调用它时,为什么它不会显示? 提前谢谢!

0 个答案:

没有答案