Results 1 to 4 of 4
  1. #1
    CODinacup is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default 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?

    Java 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
    Java 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));

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default 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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    CODinacup is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default Re: Set font,color for multiple objecta in one line

    I edited the method in my utility class.
    Java 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;
       }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. PdfBox font color
    By BobswYourUcle in forum New To Java
    Replies: 1
    Last Post: 07-13-2011, 05:04 AM
  2. Replies: 0
    Last Post: 08-18-2010, 10:31 AM
  3. font color
    By newbie123 in forum New To Java
    Replies: 2
    Last Post: 03-01-2010, 03:58 PM
  4. Replies: 5
    Last Post: 04-25-2009, 08:00 PM
  5. to set toolItem font color
    By vidyakkk in forum SWT / JFace
    Replies: 0
    Last Post: 01-30-2009, 03:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •