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?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366

1 Answers1

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);
    }
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • It's still the same thing , after the second switch of the buttons, it doesn't work anymore – Andriy Ukraine Nov 03 '15 at 00:25
  • I'll need more code to test with. Consider providing a [runnable example](https://stackoverflow.com/help/mcve) 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 – MadProgrammer Nov 03 '15 at 00:26
  • 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? – Andriy Ukraine Nov 03 '15 at 00:28
  • 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 – MadProgrammer Nov 03 '15 at 00:29
  • @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 – MadProgrammer Nov 03 '15 at 00:31
  • Honestly I don't think there's a way to show a simpler version , but then again I am a new user – Andriy Ukraine Nov 03 '15 at 00:42
  • @AndriyUkraine Then there is little more I can do. I've tested you `RandomNum` method and it works fine – MadProgrammer Nov 03 '15 at 04:06