Untitled
unknown
plain_text
a year ago
2.0 kB
6
Indexable
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TrafficLight extends JFrame implements ActionListener { JButton buttonRed,buttongreen,buttonyellow; Signal green=new Signal(color.green); Signal yellow=new Signal(color.yellow); Signal red=new Signal(color.red); public TrafficLight() { super("java Traffic light program"); getContentPane().setLayout(new GridLayout(2,1)); button Red=new JButton("Red"); button Green=new JButton("Green"); button yellow=new JButton("Yellow"); buttonRed.addActionListener(this);buttonGreen.addActionListener(this); buttonYellow.addActionListener(this); Green.turnOn(false); Red.turnOn(false); Yellow.turnOn(false); JPanel trafficPanel=new Jpanel(newGridLayout(3,1)); trafficPanel.add(gree); trafficPanel.add(yellow); JPanel lightPanel=new Jpanel(newFlowLayout()); lightPanel.add(buttonRed); getContentPane().add(trafficPanel); getContentPane().add(lightPanel); pack(); } public static void main(String[] args) { TrafficLight trafficLight=new TrafficLight(); trafficLight.setVisible(true); } public void actionPerformed(Action Evente) { if(e.getSource()==buttonRed) { green.turnOn(false);red.turnOn(true);yellow.turnOn(false); } else if(e.getSource()==buttonYellow) { yellow.turnOn(true);green.turnOn(false); red.turnOn(false); } else if(e.getSource()==buttonGreen) { green.turnOn(true); red.turnOn(false); yellow.turnOn(false); } } class signal extends Jpanel { color on; int radius=4; int border=1; boolean change; signal(color color) { on=color; change=time; } public void turnOn(boolean a){ change a; repaint(); } public Dimension getPreferedSize() { int size=(radius+border)*2; return new Dimension(size,size); } public void printComponent(Graphics graphics) { graphics.setColor(color.black); graphics.fillRect(0,0,getWidth(),getHeight()); if(change) { graphics.setColor(on); } else { graphics.setColor(on.darker().darker().darker()); } graphics.fillOval(border,border,2*radius,2*radius); } } }
Editor is loading...
Leave a Comment