Set font,color for multiple objecta in one line
How would i set the font and background color of all of these objects to be the same in one command?
Code:
Button bGeography = addButton("Geography 100",50,100,250,75,this);
Button bSports = addButton("People 100",300,100,250,75,this);
Button bVocab = addButton("Vocab 100",550,100,250,75,this);
Instead of this
Code:
bGeography.setFont(new Font("Arial",1,20));
bVocab.setFont(new Font("Arial",1,20));
bSports.setFont(new Font("Arial",1,20));
bGeography.setBackground(new Color(139,121,94));
bSports.setBackground(new Color(139,121,94));
bVocab.setBackground(new Color(139,121,94));
Re: Set font,color for multiple objecta in one line
I presume you would write an addButton() method that takes those parameters and does the appropriate things with them. This looks like a school assignment to me.
Re: Set font,color for multiple objecta in one line
I edited the method in my utility class.
Code:
public Button addButton(String text, int left, int top, int width, int height, ActionListener listener, int one, int two, int three)
{
Button newComp = new Button(text);
add(newComp);
newComp.setBounds(left,top,width,height);
newComp.setBackground(new Color(one,two,three));
newComp.setFont(new Font("Arial",1,18));
if (listener!=null)newComp.addActionListener(listener);
return newComp;
}
Re: Set font,color for multiple objecta in one line
Comments:
1. Learn to indent and otherwise format code for readability: Code Conventions for the Java Programming Language: Contents
2. There's no need for a null check. Calling addActionListener(null) does nothing.
3. Get rid of all the setBounds(...) garbage and learn to use layout managers correctly: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
db