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

📄 traffic1.java

📁 Java经典例程 从外国一大学计算机教授出版物下载的代码 经典
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
public class Traffic1 extends Frame
                      implements ActionListener, ItemListener {

    /*  The second 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 J M Bishop August 2000
     *
     *  Adds options to set the duration for a light to
     *  be on, but choice is merely recorded, not
     *  acted upon at this stage.
     */

    private LightsCanvas lights;
    private TextField duration;
    private Choice colours;
    private Button walkButton;
    private Button closeButton;

    public Traffic1() {
      setTitle("Traffic Lights Version 1");

      add("North",new Label("Savanna Traffic Light Simulation"));
      lights = new LightsCanvas();
      add("Center", lights);

      Panel buttons = new Panel();
        colours = new Choice ();
          colours.addItem("Red");
          colours.addItem("Yellow");
          colours.addItem("Green");
          colours.addItem("Walk");
          buttons.add(colours);
          colours.addItemListener(this);

        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);
        }
      });
   }

    public void actionPerformed(ActionEvent e) {
      if (e.getSource() == closeButton) {
       setVisible(false);
       dispose();
        System.exit (0);
      } else if (e.getSource() == duration) {
        message[light] = duration.getText();
        lights.repaint();
      }
    }

    public void itemStateChanged(ItemEvent e) {
      if (e.getItemSelectable()==colours) {
        String s = (String) e.getItem();
        if (s=="Red")    {light = 0;} else
        if (s=="Yellow") {light = 1;} else
        if (s=="Green")  {light = 2;} else
        if (s=="Walk")   {light = 3;}
      }
    }

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

    private int light = 0;
    String [ ] message = {"default","default","default","default"};

    class LightsCanvas extends Canvas {
      public void paint(Graphics g) {
      g.drawOval(87, 10, 30, 68);
      g.setColor(Color.red);
      g.fillOval(95, 15, 15, 15);
      g.setColor(Color.yellow);
      g.fillOval(95, 35, 15, 15);
      g.setColor(Color.green);
      g.fillOval(95, 55, 15, 15);
        // walk light is also green
      g.fillOval(95, 85, 15, 15);
      g.setColor(Color.black);
      g.drawString("RED", 15 ,28);
      g.drawString("YELLOW", 15, 48);
      g.drawString("GREEN", 15, 68);
      g.drawString("WALK", 15, 98);
      g.drawString(message[0], 135 ,28);
      g.drawString(message[1], 135, 48);
      g.drawString(message[2], 135, 68);
      g.setColor(Color.black);
      g.drawString(message[3], 135, 98);
      }
    }

}

⌨️ 快捷键说明

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