1

I was wondering how I would be able to make my buttons switch in this class which I call on from a different class. But it goes in and does only one change of the buttons and that's it..

package code;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;


public class Something implements ActionListener {

    private Game G;
    private int random;
    private JButton a;
    private JButton b;
    private JButton c;
    private Font i;

    public Something(Game g, int rand, JButton d, JButton e, JButton f, Font h) {
        G = g;
        random = rand;
        a = d;
        b = e;
        c = f;
        i = h;
    }

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        G.increment();
        if (random == 0) {
            a.setText("A");
            a.setEnabled(true);
            b.setEnabled(false);
            c.setEnabled(false);
            SuperSize(a);
            SmallerSize(b);
            SmallerSize(c);
            random = RandomNum();
            ;
        } else if (random == 1) {
            b.setText("B");
            a.setEnabled(false);
            b.setEnabled(true);
            c.setEnabled(false);
            SuperSize(b);
            SmallerSize(a);
            SmallerSize(c);
            random = RandomNum();
        } else if (random == 2) {
            c.setText("C");
            a.setEnabled(false);
            b.setEnabled(false);
            c.setEnabled(true);
            SuperSize(c);
            SmallerSize(a);
            SmallerSize(b);
            random = RandomNum();
        }
    }

    public int RandomNum() {
        Random r = new Random();
        int rand = 0;
        rand = r.nextInt(3);
        return rand;
    }

    public void SuperSize(JButton a) {
        Font myFont = i.deriveFont(Font.BOLD, i.getSize() * 4);
        a.setFont(myFont);
    }

    public void SmallerSize(JButton a) {
        a.setFont(i);
    }
}

            }

I don't know what to do , can you guys help me?

3
  • 1
    For example. Commented Nov 2, 2015 at 5:03
  • 1
    Don't create a new instance of Random each time you call RandomNum, instead, create a instance in the constructor and continue to re-use it Commented Nov 2, 2015 at 5:09
  • trashgod, how would this be incorporated into my code? Commented Nov 3, 2015 at 0:29

1 Answer 1

1

Don't create a new instance of Random each time you call RandomNum, instead, create a instance in the constructor and continue to re-use it.

public class Something implements ActionListener {

    //...

    private Random rnd;

    public Something(Game g, int rand, JButton d, JButton e, JButton f, Font h) {
        //...
        rnd = new Random();
    }

    public int RandomNum() {
        return rnd.nextInt(3);
    }
Sign up to request clarification or add additional context in comments.

7 Comments

It's still the same thing , after the second switch of the buttons, it doesn't work anymore
I'll need more code to test with. Consider providing a runnable example which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses
I'm still new to this site, so I should just provide like the same problem in the smallest code but including all the classes?
Check the linked in the previous comment to understand which a "runnable example' should provide. I stripped out all the UI code and run your RandomNum method through a 100 iteration loop and it works fine, the problem is likely somewhere else
@AndriyUkraine We need something is compilable, which can be run with out modification and only includes those parts which demonstrate your general workflow and problem
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.