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

Threaded View

  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 09:05 PM. Reason: Refine question


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 16th, 2010, 12:12 AM
  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