Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Adding background to frame

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding background to frame

    I couldnt add background color to frame although there is no errors showing.
    hope there would be someone who can guide me as soon as possible
    And the name of the class is a malay word but it means color

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.ActionListener;
    import javax.swing.*;

    public class Warna extends JFrame implements ActionListener {

    private JButton b1;

    Warna() {
    //super(str);
    setSize(310, 300);
    setVisible(true);
    setLayout(new FlowLayout());
    //Label lname = new Label("MyFrame");
    //JButton
    b1 = new JButton("RED");
    add(b1);

    b1.addActionListener(this);

    //ButtonHandler bh = new ButtonHandler()
    }

    public void actionPerformed(ActionEvent e) {
    String a = e.getActionCommand();


    Color c1 = getBackground();
    if (a.equals("red")) {
    c1 = Color.RED;
    }

    setBackground(c1);
    repaint();


    }

    public static void main(String[] args) {
    Warna w = new Warna();
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Adding background to frame

    When posting code, make sure you use highlight tags to preserve formatting.

    Which part of this isn't working? You're doing a few different things here: does the actionPerformed() method trigger? Does the code enter that if statement? Best to create an SSCCE and avoid the uncertainty by just setting the background color outright.

    By the way, there is no reason to extend JFrame here. Best to prefer composition over inheritance.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: Adding background to frame

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.ActionListener;
    import javax.swing.*;
     
    public class Warna extends JFrame implements ActionListener {
     
    private JButton b1;
     
    Warna() {
    //super(str);
    setSize(310, 300);
    setVisible(true);
    setLayout(new FlowLayout());
    //Label lname = new Label("MyFrame");
    //JButton
    b1 = new JButton("RED");
    add(b1);
     
    b1.addActionListener(this);
     
    //ButtonHandler bh = new ButtonHandler()
    }
     
    public void actionPerformed(ActionEvent e) {
    String a = e.getActionCommand();
    System.out.println(a);
     
     
    Color c1 = getBackground();
    if (a.equals("RED")) {/* red != RED */
    c1 = Color.RED;
     
    }
    this.getContentPane().setBackground(c1);/* I think U must select the contentpane before setting the background Color*/
    repaint();
     
     
     
     
    }
     
    public static void main(String[] args) {
    Warna w = new Warna();
    }
    }
    Now it’s ok

Similar Threads

  1. Replies: 3
    Last Post: July 17th, 2012, 11:59 PM
  2. adding background image to a frame???
    By overdriveboy in forum AWT / Java Swing
    Replies: 1
    Last Post: May 14th, 2012, 06:53 PM
  3. adding background image to a frame???
    By overdriveboy in forum AWT / Java Swing
    Replies: 0
    Last Post: May 13th, 2012, 01:23 PM
  4. Adding components to my GUI over a background image
    By derekxec in forum AWT / Java Swing
    Replies: 12
    Last Post: August 23rd, 2011, 03:01 PM
  5. Need help adding background image (noob here)
    By OpX316 in forum AWT / Java Swing
    Replies: 18
    Last Post: July 7th, 2011, 09:10 AM