黑马程序员技术交流社区
标题: 山寨版GUI交通灯管理系统~~~望指教~~ [打印本页]
作者: 黄奕豪 时间: 2012-6-13 21:29
标题: 山寨版GUI交通灯管理系统~~~望指教~~
今天看张老师的交通灯管理视频,看到之前就想自己挑战一下,结果真让我搞了个山寨十足的出来,学Swing没多久,搞出来的界面很挫~~不能笑!!分享下~~
交通灯管理系统
理解:四个路口,其实只用看一个路口,然后分别映射出去就可以了,一个路口的车向右转可以不用考虑,只需考虑左转,前行和停下三种状态,其中停下状态的时间是左转和前行的
总和。
第一版:山寨版(未看视频前自己弄的GUI代码)
程序说明:LampGui是一组单独的等运行。分为两个LampGui1和LampGui2,因为平面上看起来南和北的是相对的。LampThread是实现了Runnable接口的类,它里面实现了交通灯的启动顺序,这个很重要。FramGui为启动类,它建立了四个线程分别运行4个交通灯
代码如下:
package TrafficLamp;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.*;
public class LampGui1 extends JPanel{
privateJButton btnstop;
privateJButton btnturn;
privateJButton btngo;
publicLampGui1(){
initLamp();
}
publicvoid initLamp()
{
this.setLayout(newFlowLayout());
btnstop=new JButton("停下");
btnturn= new JButton("左转弯");
btngo= new JButton("直走");
this.add(btnturn);
this.add(btngo);
this.add(btnstop);
this.setVisible(true);
}
publicvoid lampStart(){
while(true){
btngo.setBackground(Color.GREEN);
btnstop.setBackground(Color.BLACK);
btnturn.setBackground(Color.BLACK);
this.repaint();
try{
Thread.sleep(10000);
}catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
btngo.setBackground(Color.BLACK);
btnstop.setBackground(Color.BLACK);
btnturn.setBackground(Color.GREEN);
this.repaint();
try{
Thread.sleep(10000);
}catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
btngo.setBackground(Color.BLACK);
btnstop.setBackground(Color.GREEN);
btnturn.setBackground(Color.BLACK);
this.repaint();
try{
Thread.sleep(20000);
}catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
----------------------------------------------------------------------------------------------------------------------
作者: 黄奕豪 时间: 2012-6-13 21:30
package TrafficLamp;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.*;
public class LampGui2 extends JPanel{
private JButton btnstop;
private JButton btnturn;
private JButton btngo;
public LampGui2(){
initLamp();
}
public void initLamp()
{
this.setLayout(new FlowLayout());
btnstop= new JButton("停下");
btnturn = new JButton("左转弯");
btngo = new JButton("直走");
this.add(btnstop);
this.add(btngo);
this.add(btnturn);
this.setVisible(true);
}
public void lampStart(){
while(true){
btngo.setBackground(Color.GREEN);
btnstop.setBackground(Color.BLACK);
btnturn.setBackground(Color.BLACK);
this.repaint();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
btngo.setBackground(Color.BLACK);
btnstop.setBackground(Color.BLACK);
btnturn.setBackground(Color.GREEN);
this.repaint();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
btngo.setBackground(Color.BLACK);
btnstop.setBackground(Color.GREEN);
btnturn.setBackground(Color.BLACK);
this.repaint();
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
----------------------------------------------------------------------------------------------------------------------
作者: 黄奕豪 时间: 2012-6-13 21:30
package TrafficLamp;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LampThread implements Runnable {
private JFrame jfra;
private int flag;
public LampThread(JFrame jfra,int flag)
{
this.flag = flag;
this.jfra = jfra;
}
public void run() {
// TODO Auto-generated method stub
if(flag == 0){
LampGui2 lamp = new LampGui2();
lamp.setBounds( 350,350, 70, 250);
jfra.add(lamp);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lamp.lampStart();
}
else if(flag==1){
LampGui1 lamp = new LampGui1();
lamp.setBounds(500,50,250,70);
jfra.add(lamp);
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lamp.lampStart();
}
else if(flag == 2){
LampGui1 lamp = new LampGui1();
lamp.setBounds( 850,350, 70, 250);
jfra.add(lamp);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lamp.lampStart();
}
else
{
LampGui2 lamp = new LampGui2();
lamp.setBounds( 500,650,250,70);
jfra.add(lamp);
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lamp.lampStart();
}
}
}
----------------------------------------------------------------------------------------------------------------------
package TrafficLamp;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.*;
public class FrameGui {
/**
* @param args
*/
private JFrame jfra;
public FrameGui()
{
jfra = new JFrame("交通路线图");
jfra.setLayout(null);
jfra.setBounds(100,20,1280,760);
Thread t1 = new Thread(new LampThread(jfra,0));
Thread t2 = new Thread(new LampThread(jfra,1));
Thread t3 = new Thread(new LampThread(jfra,2));
Thread t4 = new Thread(new LampThread(jfra,3));
t1.start();
t2.start();
t3.start();
t4.start();
jfra.setVisible(true);
jfra.setResizable(false);
jfra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new FrameGui();
}
}
作者: 郭宁 时间: 2012-6-13 21:45
赞一个~
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |