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 7 of 7

Thread: Button does not change Background

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Button does not change Background

    Help, Clicking on jbutton doesn't change the the color of my background . Please help.

     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JOptionPane;
    import javax.swing.JButton;
    import java.awt.Color;
     
     
    public class EventHand {
     
    	public static void main(String[] args) {
     
    		JFrame frame =  new JFrame ();
    		frame.setSize(1000,1000);
    		frame.setVisible(true); 
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		draw Shapes = new draw ();
     
    		frame.add(Shapes);
    		frame.setLocationRelativeTo(null);
    		frame.setTitle("Cool"); 
     
     
    	}
     
     
    }
     
    public class draw extends JPanel  {
    	 public JButton day;
    	 public JButton night;
     
    		public draw()  {
    			setLayout(new FlowLayout());
    			day= new JButton("Day");
    			night = new JButton("Night");
    			add(day);
    			add(night);
     
    		// building the class name The Handler
    			theHandler handler = new theHandler();
    		day.addActionListener(handler);
    		night.addActionListener(handler);
    		}
    		public class theHandler implements ActionListener { 
     
     
    			public  void actionPerformed(ActionEvent event) {
     
    				Color color = getBackground();
    				if(event.getSource()== day)
    					color = Color.YELLOW;
    				else if (event.getSource()== night)
    					 color = Color.BLACK;
    				repaint();
    			}
     
    		}
     
     
    	public void drawing() {
    		repaint();
     
     
    	}
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		this.setBackground(Color.WHITE);
    		g.setColor(Color.BLUE);
    		g.fillRect(600,400,150,125);
     
    		g.setColor(Color.RED);
    		g.fillRoundRect(500,500,350,100,50,40); 
    		//Bumper
    		g.setColor(Color.BLUE);
    		g.fillRect(525,600,300,40);
     
    		// windshield Wiper
    		g.setColor(Color.BLACK);
    		g.drawLine(660, 440, 700, 500);
    		// HeadLights
    		g.setColor(Color.WHITE);
    		g.fillOval(570,560,40,30);
    		g.setColor(Color.WHITE);
    		g.fillOval(750,560,40,30);
     
    		g.setColor(Color.BLACK);
    		g.fillOval(740,640,40,60);
    		// TiRE
    		g.setColor(Color.BLACK);
    		g.fillOval(585,640,40,60);
     
     
     
    	}
     
    }
    Last edited by Noob; November 15th, 2011 at 08:05 PM. Reason: Refine question


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,102
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Button does not change Background

    I have moved your post to a more appropriate forum...

    				Color color = getBackground();
    				if(event.getSource()== day)
    					color = Color.YELLOW;
    				else if (event.getSource()== night)
    					 color = Color.BLACK;
    				repaint();

    What part of the above code makes you think you change the background color of the JPanel? In other words, where do you set the background color of the JPanel?

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Button does not change Background

    I set the BackGround color of the panel in the Paint Component?

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Button does not change Background

    I set the BackGround color of the panel inside the Paint Component?

  5. #5
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,102
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Button does not change Background

    Quote Originally Posted by Noob View Post
    I set the BackGround color of the panel inside the Paint Component?
    And how does this relate to the color that is defined in the actionPerformed method?

  6. #6
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Button does not change Background

    The repaint method inside the inside the theHandler class calls the Paint Component which is set to the default color of white. Then it should allow the users to use the day button and night button to change the color of the panel that is specified. This is how i interpreted the code behavior. Obviously I'm wrong, If you could shed some light that will be very nice.

  7. #7
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,102
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Button does not change Background

    Then it should allow the users to use the day button and night button to change the color of the panel that is specified. This is how i interpreted the code behavior
    But how is it actually changing the color? The code sets the value of a local variable color, but nothing is actually done with that local variable.

Similar Threads

  1. New Background
    By Tjstretch in forum Totally Off Topic
    Replies: 6
    Last Post: November 2nd, 2011, 11:25 PM
  2. Using a button to change the interface
    By kev670 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 16th, 2011, 08:59 PM
  3. Replies: 10
    Last Post: November 15th, 2010, 11:12 PM
  4. background
    By b109 in forum AWT / Java Swing
    Replies: 0
    Last Post: May 24th, 2010, 06:37 AM
  5. how to using button to change linechart
    By NARs in forum AWT / Java Swing
    Replies: 3
    Last Post: October 30th, 2009, 12:53 PM

Tags for this Thread