资源简介
编程模拟电梯运行程序,能够通过按钮实现上下楼梯,要求程序简洁,使用简单线程,绘图技术
代码片段和文件信息
import javax.swing.Jframe;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Elevator extends JPanel {
private JButton b[] = new JButton[8];
static int thisfloor = 0;
private int a[] = {0 0 0 0 0 0 0 0};
int x2 = 0 x1 = 460;
public Elevator() {
setPreferredSize(new Dimension(360 650));
setBackground(new Color(255 218 152));
setLayout(null);
this.add(getPanel());
new Thr();
}
public JPanel getPanel() {
JPanel p = new JPanel();
p.setBounds(210 300 100 200);
p.setLayout(new GridLayout(4 2));
for (int i = 0; i < 8; i++) {
b[i] = new JButton();
b[i].setBackground(new Color(204 255 204));
b[i].setText(String.valueOf(i + 1));
final int j = i;
b[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
b[j].setBackground(new Color(255 204 204));
a[j] = 1;
}
});
}
for (int i = 7; i >= 0; i--) {
p.add(b[i]);
}
return p;
}
public void openDoor() {
x2 = x2 + 10;
repaint();
}
public void closeDoor() {
x2 = x2 - 10;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.green);
g2.drawLine(70 500 150 500);
g2.drawLine(70 450 150 450);
g2.drawLine(70 400 150 400);
g2.drawLine(70 350 150 350);
g2.drawLine(70 300 150 300);
g2.drawLine(70 250 150 250);
g2.drawLine(70 200 150 200);
g2.drawLine(70 150 150 150);
g2.drawLine(70 100 150 100);
g2.drawLine(70 100 70 500);
g2.drawLine(150 100 150 500);
g2.setColor(Color.black);
g2.drawString(“1 F“ 20 500);
g2.drawString(“2 F“ 20 450);
g2.drawString(“3 F“ 20 400);
g2.drawString(“4 F“ 20 350);
g2.drawString(“5 F“ 20 300);
g2.drawString(“6 F“ 20 250);
g2.drawString(“7 F“ 20 200);
g2.drawString(“8 F“ 20 150);
g2.setColor(new Color(153 153 255));
g2.fillRect(90 - x2 x1 20 40);
g2.fillRect(110 + x2 x1 20 40);
}
class Thr {
public Thr() {
thisthread t = new thisthread();
t.start();
}
}
class thisthread extends Thread {
@Override
public void run() {
while (true) {
execute();
try {
Thread.sleep(500);
} catch (InterruptedExcep
- 上一篇:Xpath jar包
- 下一篇:Android USB 摄像头 Camera
评论
共有 条评论