如何在我的java代码中添加组合框

时间:2014-11-22 18:05:11

标签: java arrays jcombobox

快速提问,我已经开发了3个A.I每个都有不同的深度。

目前要选择你想要玩的A.I你必须进入名为Main.java的java文件并将其更改为你想要的任何一个。要改变的路线是:

chessGame.setPlayer(Piece.COLOR_BLACK, ai3);//Here is the line where the A.I is assigned cureently it is ai3

我想让用户在游戏开始时有一个选项来选择A.I.我希望对界面有所帮助,

Jcombo是完美的,但我需要帮助实现任何帮助都很棒

(我只是不确定如何为A.I选择做一个)

当前A.I's

ai1 ai2 ai3

public class Main {

    public static void main(String[] args) {

        // Creating the Game 
        ChessGame chessGame = new ChessGame();

        // Creating the Human Player 
        //Human Player is the Object chessGui
        ChessGui chessGui = new ChessGui(chessGame);
        //Creating the A.I's
        SimpleAiPlayerHandler ai1 = new SimpleAiPlayerHandler(chessGame);//Super Dumb
        SimpleAiPlayerHandler ai2 = new SimpleAiPlayerHandler(chessGame);//Dumb
        SimpleAiPlayerHandler ai3 = new SimpleAiPlayerHandler(chessGame);//Not So Dumb

        // Set strength of AI, how far they can see ahead 
        ai1.maxDepth = 1;
        ai1.maxDepth = 2;
        ai3.maxDepth = 3;

        //Assign the Human to White 
        chessGame.setPlayer(Piece.COLOR_WHITE, chessGui);
        //Assign the not so dumb A.I to black 
        chessGame.setPlayer(Piece.COLOR_BLACK, ai3);

        // in the end we start the game
        new Thread(chessGame).start();
    }

}

1 个答案:

答案 0 :(得分:0)

你的最终代码就是这样。

public class Main {

    public static void main(String[] args) {

        // Creating the Game 
        ChessGame chessGame = new ChessGame();

        // Creating the Human Player 
        //Human Player is the Object chessGui
        ChessGui chessGui = new ChessGui(chessGame);
        //Creating the A.I's
        SimpleAiPlayerHandler ai1 = new SimpleAiPlayerHandler(chessGame);//Super Dumb
        SimpleAiPlayerHandler ai2 = new SimpleAiPlayerHandler(chessGame);//Dumb
        SimpleAiPlayerHandler ai3 = new SimpleAiPlayerHandler(chessGame);//Not So Dumb

        // Set strength of AI, how far they can see ahead 
        ai1.maxDepth = 1;
        ai1.maxDepth = 2;
        ai3.maxDepth = 3;

        //Assign the Human to White 
        chessGame.setPlayer(Piece.COLOR_WHITE, chessGui);

        //////////// JCombobox

        String[] comboTypes = { "ai1", "ai2", "ai3" };
        // Create the combo box, and set 2nd item as Default
        JComboBox comboTypesList = new JComboBox(comboTypes);
        comboTypesList.setSelectedIndex(2);
        comboTypesList.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JComboBox jcmbType = (JComboBox) e.getSource();
                String cmbType = (String) jcmbType.getSelectedItem();

                if(cmbType.equals("ai1"))
                   chessGame.setPlayer(Piece.COLOR_BLACK, ai1);
                else if(cmbType.equals("ai2"))
                   chessGame.setPlayer(Piece.COLOR_BLACK, ai2);
                else
                  chessGame.setPlayer(Piece.COLOR_BLACK, ai3);
                startGame(chessGame);
            }
        });




    }

    public void startGame(ChessGame chessGame){
        new Thread(chessGame).start();
    }

}

但请记住,应该JComboBox添加到JFrameJPanel以便GUI工作正常。