如何在我的java代码中添加JCombo Box [重复]

时间:2021-11-18 08:09:19

This question already has an answer here:

这个问题在这里已有答案:

Quick question, I have developed 3 A.I's each with a different depth.

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

Currently to choose what A.I you want to play against you have to go into the java file called Main.java and change it to whichever one you want. The line to change is:

目前要选择你想要玩的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

I want to allow the user to have an option at the start of the game to choose the A.I. I was hoping for some help with the interface,

我希望允许用户在游戏开始时选择A.I.我希望对界面有所帮助,

A Jcombo would be perfect but I need help with eh implementation any help would be great

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

(I'm just not sure how to do one for the A.I selection)

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

Current A.I's

目前A.I's

ai1 ai2 ai3

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 个解决方案

#1


0  

Your final code would be like this.

你的最终代码就是这样的。

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

}

But remember this thing that you SHOULD add JComboBox to the JFrame and JPanel so that GUI works fine.

但请记住,您应该将JComboBox添加到JFrame和JPanel,以便GUI工作正常。

#1


0  

Your final code would be like this.

你的最终代码就是这样的。

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

}

But remember this thing that you SHOULD add JComboBox to the JFrame and JPanel so that GUI works fine.

但请记住,您应该将JComboBox添加到JFrame和JPanel,以便GUI工作正常。