Gui()
{
JPanel j = new JPanel();
getContentPane().add(j);
j.setLayout(null);
int width = 35;
JButton[] buttons = new JButton[4];
for(int i = 0; i < 4; i++)
{
buttons[i] = new JButton();
buttons[i].setBackground(getNextColor());
buttons[i].setBounds(width,300,30,30);
width += 60;
buttons[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
j.add(buttons[i]);
}
setTitle("Color-Changer");
setSize(300,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private static Color getNextColor()
{
Color[] color = {Color.WHITE, Color.BLUE, Color.RED, Color.DARK_GRAY};
for(int i = 0; i < 4; i++)
return color[i];
return Color.BLUE;
}
5 replies to this topic
#1
Posted 29 January 2011 - 09:31 PM
Hey guys, I'm having trouble getting my JButtons to display the way I want them to. I want to have 4 buttons of different colors. The way I'm doing it now is to create an array of 4 buttons and give them values inside a for loop; the only problem is they all stay the same color. Here's my code: I think I have an error in my getNextColor method but I can't find it.
|
|
|
#2
Posted 29 January 2011 - 10:10 PM
If I'm reading this correctly, the getNextColor() method will always return Color.WHITE;
#3
Posted 29 January 2011 - 10:14 PM
lethalwire said:
If I'm reading this correctly, the getNextColor() method will always return Color.WHITE;
Yeah, that's where my problem is. I don't know how to get the next call of getNextColor() to return the next index in the array.
#4
Posted 29 January 2011 - 11:50 PM
If the function can return a different value each time it is called with the same arguments, this is known as a "state", and as such you need to be saving some sort of value. I would recommend using a ColorDispenser object instead of this static method:
Then use that in your code as a replacement to getNextColor() in your method. You'll need to construct a new instance of this class to use these methods of course!
class ColorDispenser
{
public ColorDispenser()
{
this(new Color[] {Color.WHITE, Color.BLUE, Color.RED, Color.DARK_GRAY});
}
public ColorDispenser(Color[] setToChooseFrom)
{
colors = setToChooseFrom;
curSelection = 0;
}
public Color getNextColor()
{
if (curSelection >= colors.length) curSelection = 0;
return colors[curSelection++];
}
private Color[] colors;
private int curSelection;
}
Then use that in your code as a replacement to getNextColor() in your method. You'll need to construct a new instance of this class to use these methods of course!
Wow I changed my sig!
#5
Posted 30 January 2011 - 12:04 AM
ZekeDragon's code will work.
If you would like to keep your code contained within one class I might modify getNextColor to:
Either case works, but ZekeDragon's example is very elegant.
If you would like to keep your code contained within one class I might modify getNextColor to:
getNextColor( int index ) {
return color[ index ]; // add this line and be sure to remove any extra unnecessary lines
You can then slightly modify the existing code where you create the JButtons to buttons[i].setBackground(getNextColor( i ));
Either case works, but ZekeDragon's example is very elegant.
#6
Posted 30 January 2011 - 09:51 AM
Thanks Zeke and lethalwire, I went with Zeke's approach and it worked great, thanks a lot.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









