在Java中使用按钮轮流

在Java中使用按钮轮流,java,user-interface,loops,button,actionlistener,Java,User Interface,Loops,Button,Actionlistener,我正在制作一个游戏,玩家轮流玩,他们会有一组按钮,每个按钮在轮到他们时都可以点击。下面是一个遵循我所说逻辑的示例代码。但当我点击“btn1”时,它会打印三个1,我仍然可以点击第二个按钮 //this loop is in the main for(int i=0; i<3;i++){ if(player==1){ player1(); } else if (player==

我正在制作一个游戏,玩家轮流玩,他们会有一组按钮,每个按钮在轮到他们时都可以点击。下面是一个遵循我所说逻辑的示例代码。但当我点击“btn1”时,它会打印三个1,我仍然可以点击第二个按钮

 //this loop is in the main
        for(int i=0; i<3;i++){
            if(player==1){
               player1();
            }
           else if (player==2){
              player2();
           }
       }

    public void player1(){
            btn1.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent arg0) {
                    System.out.println("\n1");
                    player=2;
                }});

        }

        public void player2(){
            btn2.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent arg0) {
                    System.out.println("\n2");
                    player=1;
                }});

        }
//此循环位于主循环中
对于(int i=0;i更换回路

for(int i=0; i<3;i++){
        if(player==1){
           player1();
        }
       else if (player==2){
          player2();
       }
   }
不要向按钮添加3次相同的侦听器,而是只添加一次,替换循环

for(int i=0; i<3;i++){
        if(player==1){
           player1();
        }
       else if (player==2){
          player2();
       }
   }
不要向按钮添加3次相同的侦听器,而是只添加一次,替换循环

for(int i=0; i<3;i++){
        if(player==1){
           player1();
        }
       else if (player==2){
          player2();
       }
   }
不要向按钮添加3次相同的侦听器,而是只添加一次,替换循环

for(int i=0; i<3;i++){
        if(player==1){
           player1();
        }
       else if (player==2){
          player2();
       }
   }

与其向按钮添加三次相同的侦听器,不如只添加一次。如果您只想启用和禁用按钮,为什么不这样做:

    public void player1(){
        btn1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("\n1");
                player=2;
                btn1.setEnabled(false);
                btn2.setEnabled(true);
            }});

    }

    public void player2(){
        btn2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("\n2");       
                player=1;
                btn1.setEnabled(true);
                btn2.setEnabled(false);
            }});

    }       

如果您只想启用和禁用按钮,为什么不:

    public void player1(){
        btn1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("\n1");
                player=2;
                btn1.setEnabled(false);
                btn2.setEnabled(true);
            }});

    }

    public void player2(){
        btn2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("\n2");       
                player=1;
                btn1.setEnabled(true);
                btn2.setEnabled(false);
            }});

    }       

如果您只想启用和禁用按钮,为什么不:

    public void player1(){
        btn1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("\n1");
                player=2;
                btn1.setEnabled(false);
                btn2.setEnabled(true);
            }});

    }

    public void player2(){
        btn2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("\n2");       
                player=1;
                btn1.setEnabled(true);
                btn2.setEnabled(false);
            }});

    }       

如果您只想启用和禁用按钮,为什么不:

    public void player1(){
        btn1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("\n1");
                player=2;
                btn1.setEnabled(false);
                btn2.setEnabled(true);
            }});

    }

    public void player2(){
        btn2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("\n2");       
                player=1;
                btn1.setEnabled(true);
                btn2.setEnabled(false);
            }});

    }       

但我希望他们重复轮换,这就是为什么他们处于循环中player1和player2方法只定义单击按钮后发生的事情。你需要继续编写游戏逻辑,可能在这些方法中。请参阅@npinti的答案。但我希望他们重复轮换,这就是为什么他们处于循环中player1和player2方法会影响我方法只定义单击按钮后发生的事情。您需要继续编写游戏逻辑,可能在这些方法中。请参阅@npinti的答案。但我希望它们重复轮换,这就是为什么它们处于循环中player1和player2方法只定义单击按钮后发生的事情。您需要继续编写游戏逻辑逻辑,可能在这些方法中。请参阅@npinti的答案。但我希望他们轮流重复,这就是为什么他们处于循环中。player1和player2方法仅定义单击按钮后发生的事情。您需要继续编写游戏逻辑,可能在这些方法中。请参阅@npinti的答案。