導航:首頁 > 編程語言 > 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交通燈相關的資料

熱點內容
優信二手車解壓後過戶 瀏覽:62
Windows常用c編譯器 瀏覽:778
關於改善國家網路安全的行政命令 瀏覽:833
安卓如何下載網易荒野pc服 瀏覽:654
javainetaddress 瀏覽:104
蘋果4s固件下載完了怎麼解壓 瀏覽:1003
命令zpa 瀏覽:285
python編譯器小程序 瀏覽:944
在app上看視頻怎麼光線調暗 瀏覽:540
可以中文解壓的解壓軟體 瀏覽:593
安卓卸載組件應用怎麼安裝 瀏覽:913
使用面向對象編程的方式 瀏覽:339
程序員項目經理的年終總結範文 瀏覽:929
內衣的加密設計用來幹嘛的 瀏覽:432
淮安數據加密 瀏覽:292
魔高一丈指標源碼 瀏覽:982
松下php研究所 瀏覽:168
c回調java 瀏覽:400
夢幻端游長安地圖互通源碼 瀏覽:746
電腦本地文件如何上傳伺服器 瀏覽:313