简体   繁体   中英

How to keep this skip in memory GoTo Next Iteration in For Loop in java

I am wondering how can you keep this iteration in memory. I forgot to say that player one can choose any of the five button I was just using button 1 as a example for player 1. this is where my problem is cause I can't figure out a way to keep this chosen button which can be any to stay disable(false) after player 2 as chosen is button.

It works if I don't disable all button after player 1 chose is, but the other 4 are enable and if player 1 press one of those button when its player 2 turns everything is mess up.

that's what I'm trying to fix. Help please.

I have 5 button for player 1 and 5 button for player 2. Let's say button 1 is press for player 1, after its press all the other button is deactivated for player 1 and the others are activated for player 2

When player 2 press it chooses is button, all other of his is deactivated

Here is the tricky part, after player 2 press his button, player 1 button reactivate minus button 1 which was already chosen. This means only 2,3,4 and 5 are activated.

and so on. This must alternate until all 5 button was chosen. I just can't seem to find, its the tricky part that I can't figure out. Can anyone help me solve this or any suggestion. Thanks.

here is the code

//The 2 first for loop are for just showing the 10 buttons 
for(int i=0;i<5;i++)
     {
         x=100*i;
         jbChoixJun[i]=new JButton(String.valueOf("Choisir"));
         jbChoixJun[i].setBounds(x+30,160,80,30);
         add(jbChoixJun[i]);
         jbChoixJun[i].addActionListener(this); 
         jbChoixJun[i].setEnabled(false);           
     }

     for(int i=0;i<5;i++)
     {
         x=100*i;
         jbChoixJdeux[i]=new JButton(String.valueOf("Choisir"));
         jbChoixJdeux[i].setBounds(x+30,390,80,30);
         add(jbChoixJdeux[i]);
         jbChoixJdeux[i].addActionListener(this); 
         jbChoixJdeux[i].setEnabled(false);
     }
 //The next 2 for loop, when the player its the button it becomes false and
 // stay false until the end, this works ok
 for(i=0;i<5;i++)
        {
            if(source==jbChoixJun[i])
            {
                mainJoueur1.getCarte(compteurI1);
                jbChoixJun[i].setEnabled(false);

                compteur1=1;
                compteurI1=i;
                comparaison1=true;
                System.out.println("compteur des images");
                System.out.println("compteurI1="+compteurI1);
                comparaisonBataille1=true;

            }
            //This turn all button off after the player chose is
            if(jbChoixJun[compteurI1].isEnabled()==false)
                jbChoixJun[i].setEnabled(false);
        }


        for(int i=0;i<5;i++)
        {
            if(source==jbChoixJdeux[i])
            {
                mainJoueur2.getCarte(compteurI2);
                jbChoixJdeux[i].setEnabled(false);
                compteur2=1;
                compteurI2=i;
                comparaison2=true;
                comparaisonBataille2=true;

                jbChoixJun[compteurI1].setEnabled(false);   
            }
            if(jbChoixJdeux[compteurI1].isEnabled()==false)
                jbChoixJdeux[i].setEnabled(false);
            if(i==compteurI1)
                continue;
            else
                jbChoixJun[i].setEnabled(true);

The problem, is that the first button chosen by player comes active and only the second one that becomes disable. Can of hard to explain Hope you guys can help me

As asked by the OP here is how you can extend JButton and use it according to your needs by adding new properties, this gives you more flexibility

public class ExtendedJButton extends JButton{

    private static final long serialVersionUID = 1L;

    /**
     * We use this boolean for some case and set it to true or false according to out need
     */
    private boolean someCheck;

    //Add more propterties according to your need and create conditions to enable and disable them accordingly

    public boolean isSomeCheck() {
        return someCheck;
    }

    public void setSomeCheck(boolean someCheck) {
        this.someCheck = someCheck;
    }

    public static void main(String[] args) {
        JFrame frameForExample = new JFrame();
        ExtendedJButton button = new ExtendedJButton();
        frameForExample.add(button);

        //Now you can access properties of JButton class as well as new properties of your extended class
        button.setEnabled(false);
        ////.........

        //An example case
        if(button.isSomeCheck()){
            //Do what you want to
            //if you want to disable the button when this case is true do it, 
        }
    }

This is not an answer but more a guidance on how you can proceed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM