Random

+2 Developer John · October 25, 2014
How can I use Java Random to loop through 6 variables?
For an example: 
I have a Gui windows, and I want it to choose one color out of the six declared in Java Random.
-Thanks!

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Developer John · November 15, 2014
Okay, I thought you were creating  new String called button within those parameters. I understand now.
0 Alex Sweps · November 12, 2014
It is because you are passing in button which is the string. So you assign the passed in string to pressedButton.

button1.addActionListener(new changeColor("button1")); //This is your string you are passing in.

public changeColor(String button){ // This is what it is passed into. So "button1" is now the variable button.

buttonPressed = button; //So now buttonPressed is assigned to button. aka: "button1"


Now you can compare buttonPressed and check what changeColor method to run.
0 Developer John · November 12, 2014
Alex, I don't understand why you made a String called buttonPressed and then converted that into button. Idk, I'm so confused.
0 Developer John · November 12, 2014
It's okay, as long as you respond, that way my help hasn't died on me lol.:P Sorry to ask for the code though, I haven't had any notifications this past week here, so I feel like people died. 
+1 Alex Sweps · November 12, 2014
Yeha, sorry iv been busy the last few days. Go back to your original problem of your handler running all of the change colors at the same time. All you need is a way to make them do it for the button you pressed.

Try some things out and let me know what you come up with. Watch some more tutorials. If I keep posting working code you will never learn how to solve the problems.

Think about using if statements and having it check what button is being pressed.
0 Developer John · November 11, 2014
Hello?
0 Developer John · November 10, 2014
Alex I don't know how to fix your code.
0 Alex Sweps · November 7, 2014
Yeah that code was just typed in without checking to show you a way that you could achieve your goal. There are plenty of other ways so try and figure out how you can separate all each buttons action instead of running them all at once inside the handler.
0 Developer John · November 7, 2014
On the if statement and the else if statement, you forgot to put an extra parenthesis, and I'm getting an error saying that changeColor cannot be resolved as a type when you add the Action Listener to button1. 
0 Alex Sweps · November 5, 2014
Look what is in your action performed method. Each buttons changeColor is being run when you click any of the buttons.

You need a way to distinguish which button was pressed ;)    What I would do is pass in a string to the new changeColor class you are creating inside your addActionListener. Then id create an if/else statement in the action performed.


Example:
button1.addActionListener(new changeColor("button1"));


class changeColor implements ActionListener{

String buttonPressed;

public changeColor(String button){
        buttonPressed = button;
}

@Override
public void actionPerformed(ActionEvent arg0) {
if(buttonPressed.equals("button1"){
         button1.setBackground(setColor());
}else if(buttonPressed.equals("button2"){
button2.setBackground(setColor());
}
}
}



I just typed that an have not compiled and tested it. Give it a go and let me know if it works.

Java / Android Development

107,072 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator