?? lifter.java
字號:
package lifter;
import java.lang.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.event.*;
public class Lifter extends JFrame implements Runnable, ActionListener {
public int requestedMoveState = 0;
public int floorsInBuilding = 0;
public int currentFloor = 0;
public int moveState = 0;
public JButton[] a;
public JButton[] b;
public int flag = 1;
public void actionPerformed(ActionEvent e) {
if (moveState == 1&&Integer.parseInt(e.getActionCommand())>this.currentFloor)
this.addDestUp(Integer.parseInt(e.getActionCommand()));
if (moveState == -1&&Integer.parseInt(e.getActionCommand())<this.currentFloor)
this.addDestDown(Integer.parseInt(e.getActionCommand()));
if (moveState == 0&&destFloors.isEmpty()) {
if (Integer.parseInt(e.getActionCommand()) > this.currentFloor) {
this.addDestUp(Integer.parseInt(e.getActionCommand()));
moveState = 1;
} else if (Integer.parseInt(e.getActionCommand()) < this.currentFloor) {
this.addDestDown(Integer.parseInt(e.getActionCommand()));
moveState = -1;
}
}
if (flag == 1) {
new Thread(this).start();
flag = 0;
}
}
public ArrayList<Integer> destFloors = (ArrayList) new java.util.ArrayList<Integer>();;
public Lifter(int fb, int cf, int ms, Container container0) {
Container container = new Container();
Container container1 = new Container();
Container container2 = new Container();
container.setLayout(new GridLayout(fb, 1, 5, 5));
container1.setLayout(new GridLayout(1, 2, 0, 0));
container2.setLayout(new GridLayout(fb, 1, 0, 0));
container0.add(container1);
container1.add(container);
container1.add(container2);
a = new JButton[fb];
b = new JButton[fb];
this.floorsInBuilding = fb;
this.currentFloor = cf;
this.moveState = ms;
for (int j = fb - 1; j >= 0; j--) {
a[j] = new JButton();
container.add(a[j]);
container.setLayout(new GridLayout(fb, 1, 0, 0));
// a[j].addActionListener(this);
a[j].setBackground(Color.blue);
}
for (int j = fb - 1; j >= 0; j--) {
b[j] = new JButton("" + (j + 1));
container2.add(b[j]);
b[j].addActionListener(this);
container2.setLayout(new GridLayout(fb, 1, 0, 0));
}
}
public void addDestUp(Integer receiverequest) {
if (!destFloors.contains(receiverequest)) {
destFloors.add(receiverequest);
}
Collections.sort(destFloors);
}
public void addDestDown(Integer receiverequest) {
if (!destFloors.contains(receiverequest)) {
destFloors.add(receiverequest);
}
Collections.sort(destFloors);
Collections.reverse(destFloors);
}
public void addDest(Integer receiverequest) {
if (!destFloors.contains(receiverequest)) {
destFloors.add(receiverequest);
}
}
public void printDest() {
Iterator e = destFloors.iterator();
System.out.print("dest: ");
while (e.hasNext()) {
System.out.print(e.next() + " ");
}
System.out.println("");
}
public synchronized void run() {
// while (true) {
System.out.print("in run " + this.moveState + " ");
this.printDest();
// moveState = 0;
if (!destFloors.isEmpty()) {
if (moveState == 1) {
while (currentFloor < (Integer) destFloors.toArray()[0]) {
currentFloor++;
a[currentFloor - 1].setBackground(Color.black);
for (int x = 0; x < currentFloor - 1; x++) {
a[x].setBackground(Color.green);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out
.println(Thread.currentThread().getName()
+ currentFloor
+ " "
+ (currentFloor == (Integer) destFloors
.toArray()[0]));
if (currentFloor == (Integer) destFloors.toArray()[0]) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
destFloors.remove(0);
if (destFloors.isEmpty())
break;
}
}
} else if (moveState == -1) {
while (currentFloor > (Integer) destFloors.toArray()[0]) {
currentFloor--;
a[currentFloor - 1].setBackground(Color.black);
a[currentFloor].setBackground(Color.green);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out
.println(Thread.currentThread().getName()
+ currentFloor
+ " "
+ (currentFloor == (Integer) destFloors
.toArray()[0]) + " " + moveState);
if (currentFloor == (Integer) destFloors.toArray()[0]) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
destFloors.remove(0);
if (destFloors.isEmpty())
break;
}
}
} else if (moveState == 0) {
while (currentFloor < (Integer) destFloors.toArray()[0]) {
currentFloor++;
a[currentFloor - 1].setBackground(Color.black);
for (int x = 0; x < currentFloor - 1; x++) {
a[x].setBackground(Color.green);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out
.println(Thread.currentThread().getName()
+ currentFloor
+ " "
+ (currentFloor == (Integer) destFloors
.toArray()[0]));
if (currentFloor == (Integer) destFloors.toArray()[0]) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
destFloors.remove(0);
if (destFloors.isEmpty())
break;
}
}
while (currentFloor > (Integer) destFloors.toArray()[0]) {
currentFloor--;
a[currentFloor - 1].setBackground(Color.black);
a[currentFloor].setBackground(Color.green);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out
.println(Thread.currentThread().getName()
+ currentFloor
+ " "
+ (currentFloor == (Integer) destFloors
.toArray()[0]) + " " + moveState);
if (currentFloor == (Integer) destFloors.toArray()[0]) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
destFloors.remove(0);
if (destFloors.isEmpty())
break;
}
}
}
// System.out.println("waiting");
// }
}
moveState = 0;
flag = 1;
System.out.print(this.moveState);
printDest();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -