正在学习java GUI,想做模拟一下电梯的运行,如何用timer来实现电梯的平滑上升和下降啊???用线程也可以。。

解决方案 »

  1.   

    今天有点闲情,给你写段代码吧,但愿对你有帮助import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class ElevatorTest {
    public static void main(String[] args) {
    new ElevatorFrame();
    }
    }class ElevatorFrame extends JFrame { public ElevatorFrame() {
    super("Elevator Test");
    setSize(400, 300);
    self = this;

    Container c = getContentPane();
    c.setLayout(null);

    nowLbl = new JLabel("Current Floor");
    nowLbl.setBounds(180, 20, 120, 20);
    c.add(nowLbl);

    nowTfd = new JTextField("6");
    nowTfd.setBounds(300, 20, 80, 20);
    c.add(nowTfd);

    goLbl = new JLabel("Destination Floor");
    goLbl.setBounds(180, 50, 120, 20);
    c.add(goLbl);

    goTfd = new JTextField("2");
    goTfd.setBounds(300, 50, 80, 20);
    c.add(goTfd);

    goBtn = new JButton("Go");
    goBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {

    if (waitTimer.isRunning()) {
    waitTimer.stop();
    }

    // get current floor require
    try {
    current = Integer.parseInt(nowTfd.getText());
    if (current > 6 || current < 1) {
    throw new Exception("invalid floor");
    }
    } catch (Exception e) {
    JOptionPane.showMessageDialog(
    self, 
    (Object)"Invalid Floor.\nyous should input a number between 1 and 6", 
    "Elevator Test",
    JOptionPane.WARNING_MESSAGE);
    nowTfd.requestFocus(true);
    return;
    }

    // get destination floor require
    if (goTfd.getText().trim().length() != 0) {
    try {
    destination = Integer.parseInt(goTfd.getText());
    if (destination > 6 || current < 1) {
    throw new Exception("invalid floor");
    }
    } catch (Exception e) {
    JOptionPane.showMessageDialog(
    self, 
    (Object)"Invalid Floor.\nyous should input a number between 1 and 6", 
    "Elevator Test",
    JOptionPane.WARNING_MESSAGE);
    goTfd.requestFocus(true);
    return;
    }
    } else {
    destination = 0;
    }

    // the elevator moves to current require floor first
    posY = (int)elevator.getLocation().getY();
    moveY = 260 - 40 * current;
    goBtn.setEnabled(false);
    reachfloor = false;
    deffloor = false;
    runTimer.start();
    }
    });
    goBtn.setBounds(300, 80, 80, 20);
    c.add(goBtn);

    floors = new FloorPanel[6];
    for (int i=0; i<6; i++) {
    floors[i] = new FloorPanel("f " + (i+1));
    if (i%2 == 0) {
    (floors[i]).setBackground(Color.white);
    (floors[i]).setColor(Color.black);
    } else {
    (floors[i]).setBackground(Color.black);
    (floors[i]).setColor(Color.white);
    }
    floors[i].setBounds(20, 220-i*40, 40, 40);
    c.add(floors[i]);
    }

    elevator = new JPanel();
    elevator.setBackground(Color.red);
    elevator.setBounds(60, 260-20, 20, 20);
    c.add(elevator);

    runTimer = new javax.swing.Timer(30, new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    // the elevator moves to the target floor
    if ((posY-20) < moveY) {
    posY++;
    } else {
    posY--;
    }
    elevator.setLocation(60, posY);

    // when elevator reach the target floor
    if ((posY-20) == moveY) {
    runTimer.stop();

    // does the elevator reach destination floor?
    if (destination == 0) {
    reachfloor = true;
    } else {
    if (moveY == 260 - 40 * destination) {
    reachfloor = true;
    }
    }
    if (reachfloor) {
    goBtn.setEnabled(true);
    if (destination == 1) {
    deffloor = true;
    }
    }

    // is the elevator necessary to move to default floor ?
    if (! deffloor) {
    waittime = 0;
    waitTimer.start();
    }
    }
    }
    });

    waitTimer = new javax.swing.Timer(1000, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    // wait time count
    waittime++;

    // the elevator moves to destination floor after any one comes in 
    if (waittime > 1) {
    if (destination > 0 && !reachfloor) {
    posY = (int)elevator.getLocation().getY();
    moveY = 260 - 40 * destination;
    waitTimer.stop();
    runTimer.start();
    }
    }

    // if no one use the elevator for some times, the elevator moves to default floor itself
    if (waittime > 3) {
    posY = (int)elevator.getLocation().getY();
    moveY = 260 - 40 * 1;
    waitTimer.stop();
    deffloor = true;
    runTimer.start();
    }
    }
    });

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setVisible(true);
    }

    JLabel nowLbl;
    JLabel goLbl;
    JTextField nowTfd;
    JTextField goTfd;
    JButton goBtn;
    JPanel elevator;
    FloorPanel[] floors;
    javax.swing.Timer runTimer;
    javax.swing.Timer waitTimer;

    int current, destination;
    int posY, moveY;
    int waittime;
    boolean reachfloor, deffloor;
    ElevatorFrame self;
    }class FloorPanel extends JPanel {

    public FloorPanel() {
    caption = "";
    }

    public FloorPanel(String s) {
    caption = s;
    }

    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(color);
    g.drawString(caption, 15, 25);
    }

    public void setColor(Color c) {
    if (! c.equals(color)) {
    color = c;
    }
    repaint();
    }

    String caption;
    Color color = Color.black;
    }