⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 traffic2.java

📁 Java经典例程 从外国一大学计算机教授出版物下载的代码 经典
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;

public class Traffic2 extends Frame
       implements ActionListener {

  /* The third Traffic Light program
   *                              by J M Bishop Oct 1996
   *               Java 1.1 version by T Abbott Oct 1997
   *         enhanced and revised by J M Bishop Oct 1997
   *                      updated by J M Bishop Aug 2000
   * Enables several sets of lights to
   * operate simultaneously.
   *
   * Illustrates threads and graphics.
   */

    private Canvas area;
    private int lightsPosition = 105;
    private static final int lightsWidth = 150;
    private SetOfLights[] lights = new SetOfLights[3];
    private int nLights = 0, setWanted = 0;
    private Choice colours;
    private Button newSetButton;
    private Button walkButton;
    private Button closeButton;
    private TextField duration;

    public Traffic2() {
      setTitle("Traffic Lights Version 2");
      add("North",
           new Label("Savanna Traffic Light Simulation", Label.CENTER));
      area = new Canvas();
        area.addMouseListener(new MouseEvtHandler());
        add("Center",area);

      Panel buttons = new Panel();
        newSetButton = new Button("New Set");
          newSetButton.addActionListener(this);
          buttons.add(newSetButton);
        colours = new Choice ();
          colours.addItem("Red");
          colours.addItem("Yellow");
          colours.addItem("Green");
          colours.addItem("Walk");
          buttons.add(colours);

        buttons.add(new Label("Duration"));

        duration = new TextField("", 3);
          duration.setEditable(true);
          duration.addActionListener(this);
          buttons.add(duration);

        walkButton = new Button("Walk");
          // no action yet
          buttons.add(walkButton);

        closeButton = new Button("Close");
          closeButton.addActionListener(this);
          buttons.add(closeButton);
      add("South", buttons);

      // set up the frame
      setSize(350, 210);
      setVisible(true);
      addWindowListener(new WindowAdapter () {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
        }
      });

    }

    class MouseEvtHandler extends MouseAdapter {
      public void mousePressed(MouseEvent e) {
        int n = e.getX() / lightsWidth;
        if (n < nLights)
        setWanted = n;
      }
    }

    public void actionPerformed(ActionEvent e) {
      Object event = e.getSource();
      if (event == newSetButton) {
        lights[nLights] = new SetOfLights(area, lightsPosition);
        lights[nLights].start();
        lightsPosition += lightsWidth;
        nLights++;
        if (nLights == 3)
        newSetButton.setEnabled(false);
      } else if (event == closeButton) {
        for (int i = 0; i<nLights; i++)
          lights[i].alive = false;
        setVisible(false);
        dispose();
        System.exit(0);
      }
    }

    public static void main(String[] args) {
      new Traffic2();
    }
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -