Button does not change Background
Help, Clicking on jbutton doesn't change the the color of my background . Please help.
Code :
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);
}
}
Re: Button does not change Background
I have moved your post to a more appropriate forum...
Code :
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?
Re: Button does not change Background
I set the BackGround color of the panel in the Paint Component?
Re: Button does not change Background
I set the BackGround color of the panel inside the Paint Component?
Re: Button does not change Background
Quote:
Originally Posted by
Noob
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?
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.
Re: Button does not change Background
Quote:
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.