📄 traffic0.java
字号:
import java.awt.*;
import java.awt.event.*;
class Traffic0 extends Frame implements ActionListener {
/* The first Traffic light program
* by J M Bishop Oct 1996
* Java 1.1 by T Abbott October 1997
* updated J M Bishop May 2000
* Displays a representation of traffic lights,
* in preparation for a simulation.
*
* NOTE: The walk button is not activated yet.
* Illustrates panels and canvases and the
* BorderLayout manager.
*/
LightsCanvas lights;
Button close;
Button walk;
Traffic0() {
// Add the components
Panel title = new Panel();
title.add (new Label("Savanna Traffic Light Simulation"));
add("North", title);
lights = new LightsCanvas();
add("Center", lights);
Panel buttons = new Panel();
walk = new Button("WALK");
buttons.add(walk);
close = new Button("CLOSE");
close.addActionListener(this);
buttons.add(close);
add("South", buttons);
// Set up the frame
setTitle("Traffic Lights version 0");
setSize(300,200);
setVisible(true);
addWindowListener(new WindowAdapter () {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
// Must be the close button
setVisible(false);
dispose();
System.exit(0);
}
public static void main(String[] args) {
new Traffic0 ();
}
class LightsCanvas extends Canvas {
public void paint(Graphics g) {
g.drawOval(97, 10, 30, 68);
g.setColor(Color.red);
g.fillOval(105, 15, 15, 15); // red
g.setColor(Color.yellow);
g.fillOval(105, 35, 15, 15); // yellow
g.setColor(Color.green);
g.fillOval(105, 55, 15, 15); // green
g.fillOval(105, 85, 15, 15); // walk
g.setColor(Color.black);
g.drawString("RED", 15, 28);
g.drawString("YELLOW", 15, 48);
g.drawString("GREEN", 15, 68);
g.drawString("WALK", 15, 98);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -