導航:首頁 > 編程語言 > java交通燈

java交通燈

發布時間:2022-03-13 20:35:10

① 求文件中實現交通燈例子設計2的java全代碼,

應該是模擬交通燈吧。它肯定不會是寫交通燈的實際應用程序
import java.awt.event.*;

public class JiaoTong extends Frame {
boolean redStatus=false,greenStatus=false,yellowStatus=false;
int j=0;
public void paint(Graphics g) {
Color c=g.getColor();
if(redStatus==true)
{j++;
g.setColor(Color.RED);
g.fillOval(100,50, 50,50);
g.drawString("紅燈亮了"+j+"秒", 100, 120);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100,50, 50,50);
}

if(yellowStatus==true){
j++;
g.setColor(Color.YELLOW);
g.fillOval(100, 150, 50, 50);
g.drawString("黃燈注意啦"+j+"秒", 100, 220);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100, 150, 50, 50);

}
if(greenStatus==true){
j++;
g.setColor(Color.GREEN);
g.fillOval(100, 250, 50, 50);
g.drawString("綠燈行"+j+"秒", 100, 320);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100, 250, 50, 50);
}
g.setColor(c);
}

public void Lauch() {
setTitle("交通燈指示");
setSize(300, 400);
setBackground(Color.BLACK);
addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
System.exit(0);
}

});

setVisible(true);

new Thread(new PaintThread()).start();
}

public static void main(String[] args) {
JiaoTong a=new JiaoTong();
a.Lauch();

}

public class PaintThread implements Runnable
{

public void run() {
for(int i=0;i<80;i++){
switch (i) {
case 0:
j=0;
redStatus=true;
greenStatus=false;
yellowStatus=false;
break;
case 40:
j=0;
redStatus=false;
greenStatus=false;
yellowStatus=true;
break;
case 50:
j=0;
redStatus=false;
greenStatus=true;
yellowStatus=false;
break;
default:
break;
}
repaint();
if(i==79)
i=-1;
try
{
Thread.sleep(1000);

}
catch(InterruptedException e)
{
e.printStackTrace();
}
}

}

}

}

② java 紅綠燈 代碼

給程序一個變數tag 用來記錄按鍵次數~~

tag%2==0時為綠
tag%2==1時為紅
tag%2==2時為黃

③ java控制紅綠燈及模擬車輛運動

寫兩個程序分別模擬紅綠燈和汽車:
1)紅綠燈程序以報文形式通知汽車程序;
2)汽車程序需要用多線程來實現。
3)紅綠燈程序用個循環,每隔n分鍾紅綠燈轉換,同時通知汽車程序;

④ Java紅綠燈 求大神!!急啊

importjava.awt.Color;
importjava.awt.Container;
importjava.awt.Dimension;
importjava.awt.FlowLayout;
importjava.awt.Graphics;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
publicclassWayextendsJPanel{
JButtonred;
publicWay(){
red=newJButton("換燈");
setBackground(Color.yellow);
setSize(newDimension(320,260));
setPreferredSize(newDimension(320,260));
JPanelbtnPanel=newJPanel();
btnPanel.setLayout(newFlowLayout());
red.setLayout(newFlowLayout());//將單選按鈕加入按鈕面板
btnPanel.add(red);
add(red);
}
privatevoidlightRed(Graphicsg){
g.setColor(Color.red);
g.fillOval(getWidth()/2,30,15,15);
g.setColor(Color.black);
g.fillOval(getWidth()/2,50,15,15);
g.fillOval(getWidth()/2,70,15,15);
}
privatevoidlightYellow(Graphicsg){
g.setColor(Color.yellow);
g.fillOval(getWidth()/2,50,15,15);
g.setColor(Color.black);
g.fillOval(getWidth()/2,30,15,15);
g.fillOval(getWidth()/2,70,15,15);
}
privatevoidlightGreen(Graphicsg){
g.setColor(Color.green);
g.fillOval(getWidth()/2,70,15,15);
g.setColor(Color.black);
g.fillOval(getWidth()/2,30,15,15);
g.fillOval(getWidth()/2,50,15,15);
}
protectedvoidpaintComponent(Graphicsg){
super.paintComponents(g);
Waya=newWay();
g.clearRect(0,0,getWidth(),getHeight());
g.drawRect(getWidth()/2,30,30,80);
red.addActionListener(newActionListener(){
intf1=0;
publicvoidactionPerformed(ActionEvente){
Graphicsg=getGraphics();
switch(f1){
case0:
a.lightRed(g);
break;
case1:
a.lightYellow(g);
break;
case2:
a.lightGreen(g);
break;
}
f1++;
if(f1>2)f1=0;
};
});
}
publicstaticvoidmain(String[]args){
JFramefr=newJFrame("郵件界面模擬");
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//點擊x結束程序
ContainercontentPane=fr.getContentPane();
//得到窗口內容面板
contentPane.add(newWay());
fr.pack();
fr.setVisible(true);//設置窗口可見
}
}
大致幫你改了下,不知道符合不符合你的要求,有問題請追問

⑤ 求java交通燈源代碼

sadadad

⑥ 用java編寫交通信號燈

按照你的要求編寫的紅綠燈程序,你看看吧,比較簡單。
完整的程序如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics;
public class TrafficLight extends JFrame{
JRadioButton jrbYellow,jrbGreen,jrbRed;
int flag=0;
jpNewPanel jpNewPanel;
public static void main(String[] args){
TrafficLight frame=new TrafficLight();
frame.setSize(500,200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("TrafficLight");
frame.setVisible(true);
}
public TrafficLight(){
jpNewPanel=new jpNewPanel();
add(jpNewPanel,BorderLayout.CENTER);
JPanel jpRadioButtons=new JPanel();
jpRadioButtons.setLayout(new GridLayout(1,3));
jpRadioButtons.add(jrbYellow=new JRadioButton("Yellow"));
jpRadioButtons.add(jrbGreen=new JRadioButton("Green"));
jpRadioButtons.add(jrbRed=new JRadioButton("Red"));
add(jpRadioButtons,BorderLayout.SOUTH);
ButtonGroup group=new ButtonGroup();
group.add(jrbYellow);
group.add(jrbGreen);
group.add(jrbRed);

jrbYellow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
flag=2;
jpNewPanel.repaint();
}
});
jrbGreen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
flag=1;
jpNewPanel.repaint();
}
});
jrbRed.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
flag=3;
jpNewPanel.repaint();
}
});
}
class jpNewPanel extends JPanel{
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawRect(0,0,40,100);
g.drawOval(10,10,20,20);
g.drawOval(10,40,20,20);
g.drawOval(10,70,20,20);
if(flag==1){
g.setColor(Color.GREEN);
g.fillOval(10, 70, 20, 20);
}
else if(flag==2){
g.setColor(Color.YELLOW);
g.fillOval(10, 40, 20, 20);
}
else if(flag==3){
g.setColor(Color.RED);
g.fillOval(10, 10, 20, 20);
}
}
}
}

⑦ 請問如何用JAVA多線程寫一個紅黃綠依次亮的交通燈程序

import java.awt.*;
import java.awt.event.*;

public class JiaoTong extends Frame {
boolean redStatus=false,greenStatus=false,yellowStatus=false;
int j=0;
public void paint(Graphics g) {
Color c=g.getColor();
if(redStatus==true)
{j++;
g.setColor(Color.RED);
g.fillOval(100,50, 50,50);
g.drawString("紅燈亮了"+j+"秒", 100, 120);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100,50, 50,50);
}

if(yellowStatus==true){
j++;
g.setColor(Color.YELLOW);
g.fillOval(100, 150, 50, 50);
g.drawString("黃燈注意啦"+j+"秒", 100, 220);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100, 150, 50, 50);

}
if(greenStatus==true){
j++;
g.setColor(Color.GREEN);
g.fillOval(100, 250, 50, 50);
g.drawString("綠燈行"+j+"秒", 100, 320);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100, 250, 50, 50);
}
g.setColor(c);
}

public void Lauch() {
setTitle("交通燈指示");
setSize(300, 400);
setBackground(Color.BLACK);
addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
System.exit(0);
}

});

setVisible(true);

new Thread(new PaintThread()).start();
}

public static void main(String[] args) {
JiaoTong a=new JiaoTong();
a.Lauch();

}

public class PaintThread implements Runnable
{

public void run() {
for(int i=0;i<80;i++){
switch (i) {
case 0:
j=0;
redStatus=true;
greenStatus=false;
yellowStatus=false;
break;
case 40:
j=0;
redStatus=false;
greenStatus=false;
yellowStatus=true;
break;
case 50:
j=0;
redStatus=false;
greenStatus=true;
yellowStatus=false;
break;
default:
break;
}
repaint();
if(i==79)
i=-1;
try
{
Thread.sleep(1000);

}
catch(InterruptedException e)
{
e.printStackTrace();
}
}

}

}

}

⑧ java交通燈問題

網上找個參考import java.awt.*;
import java.awt.event.*;

public class JiaoTong extends Frame {
boolean redStatus=false,greenStatus=false,yellowStatus=false;
int j=0;
public void paint(Graphics g) {
Color c=g.getColor();
if(redStatus==true)
{j++;
g.setColor(Color.RED);
g.fillOval(100,50, 50,50);
g.drawString("紅燈亮了"+j+"秒", 100, 120);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100,50, 50,50);
}

if(yellowStatus==true){
j++;
g.setColor(Color.YELLOW);
g.fillOval(100, 150, 50, 50);
g.drawString("黃燈注意啦"+j+"秒", 100, 220);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100, 150, 50, 50);

}
if(greenStatus==true){
j++;
g.setColor(Color.GREEN);
g.fillOval(100, 250, 50, 50);
g.drawString("綠燈行"+j+"秒", 100, 320);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100, 250, 50, 50);
}
g.setColor(c);
}

public void Lauch() {
setTitle("交通燈指示");
setSize(300, 400);
setBackground(Color.BLACK);
addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
System.exit(0);
}

});

setVisible(true);

new Thread(new PaintThread()).start();
}

public static void main(String[] args) {
JiaoTong a=new JiaoTong();
a.Lauch();

}

public class PaintThread implements Runnable
{

public void run() {
for(int i=0;i<80;i++){
switch (i) {
case 0:
j=0;
redStatus=true;
greenStatus=false;
yellowStatus=false;
break;
case 40:
j=0;
redStatus=false;
greenStatus=false;
yellowStatus=true;
break;
case 50:
j=0;
redStatus=false;
greenStatus=true;
yellowStatus=false;
break;
default:
break;
}
repaint();
if(i==79)
i=-1;
try
{
Thread.sleep(1000);

}
catch(InterruptedException e)
{
e.printStackTrace();
}
}

}

}

}

⑨ 請幫我看下這個JAVA的程序(簡易紅綠燈)

發現你的程序窗體大小每改變一次,都會加上三個RadioButtons。

⑩ java web jsp 如何實現交通燈

用定時器,根據時間變化對「燈」的背景顏色進行改變就可以

閱讀全文

與java交通燈相關的資料

熱點內容
葉子媚演過尺較大的電影 瀏覽:572
pdf裁邊 瀏覽:193
林正英和大傻演的電影叫什麼 瀏覽:797
搶先電影社區 瀏覽:754
kpzz5. top/ index. php 瀏覽:208
繁殖食人族的電影 瀏覽:488
益智類電影小學 瀏覽:828
電線平方數演算法 瀏覽:197
有一個電影男主叫馬克 瀏覽:779
韓國,補課電影演員表 瀏覽:182
linux查看系統命令是什麼 瀏覽:32
matlab歷史命令 瀏覽:219
主角穿越到工作細胞的小說 瀏覽:102
九十年代香港老太太鬼電影 瀏覽:871
特工劉堅是李連傑的哪部電影 瀏覽:334
極坐標運演算法則 瀏覽:605
十大香港全漏電影 瀏覽:335
小虎還鄉裡面的驢叫什麼 瀏覽:499
誰有小電影網址啊 瀏覽:376
香港滿清十大酷刑一共有幾部電影 瀏覽:709