how do i change the colors with button push

0 shanzi own · March 1, 2015


import java.awt.*;
import java.awt.event.*;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class TrafficLights extends JPanel {

private JButton push;
private JLabel labels;

public TrafficLights(){
//panel
setBackground (Color.blue);
setPreferredSize (new Dimension (300,200));
setFont (new Font("Arial",Font.BOLD,14));

push=new JButton("change color");
push.addActionListener(new ButtonListener());
labels=new JLabel ();

add(push);
add(labels);
setPreferredSize (new Dimension(300,40));
setBackground (Color.cyan);
}

public void paintComponent (Graphics page){

super.paintComponent(page);

page.setColor(Color.gray); //rectangle container
page.fillRect(40,80,45,115);
page.drawRect (40,80,45,115);

page.setColor(Color.black); // first light
page.fillOval(46, 83, 30, 30);
page.drawOval(46,83,30,30);

page.setColor(Color.black); //second light
page.fillOval(46, 120, 30, 30);
page.drawOval(46,120,30,30);

page.setColor(Color.black); //third light
page.fillOval(46, 157, 30, 30);
page.drawOval(46,157,30,30);
}

public class ButtonListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent event) {


}
}


}

Post a Reply

Replies

Oldest  Newest  Rating
+1 Mohamed Elkady · March 1, 2015
hi, 
try to use this inside the actionPerformed method
Color selectedColor;
selectedColor = JColorChooser(null,"Dialog title", Color.Black);
0 shanzi own · March 2, 2015
i tried that didn't work i with some help ended up using the update method thanks hun
  • 1

Java / Android Development

107,178 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator