Ⅰ java 線程實現一個紅綠燈問題
關鍵是啟動一個線程式控制制顏色。代碼如下。
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheledExecutorService;
import java.util.concurrent.TimeUnit;
public class Signal extends Applet {
int width = 200, height = 240;
int w = 50, h = 50;
int x = (width - w) / 2, y1 = (height - h * 3) / 3, y2 = y1 + h, y3 = y2 + h;
Color c = Color.RED;
@Override
public void init() {
ScheledExecutorService exec = Executors.newScheledThreadPool(1);
exec.scheleAtFixedRate(new Runnable() {
@Override
public void run() {
if (c == Color.RED) {
c = Color.YELLOW;
} else if (c == Color.YELLOW) {
c = Color.GREEN;
} else if (c == Color.GREEN) {
c = Color.RED;
}
repaint();
}
}, 5, 5, TimeUnit.SECONDS);
}
@Override
public void paint(Graphics g) {
setBackground(Color.white);
// all gray
g.setColor(Color.LIGHT_GRAY);
g.fillOval(x, y1, w, h);
g.fillOval(x, y2, w, h);
g.fillOval(x, y3, w, h);
if (c == Color.RED) {
g.setColor(Color.RED);
g.fillOval(x, y1, w, h);
} else if (c == Color.YELLOW) {
g.setColor(Color.YELLOW);
g.fillOval(x, y2, w, h);
} else if (c == Color.GREEN) {
g.setColor(Color.GREEN);
g.fillOval(x, y3, w, h);
}
}
}
Ⅱ 使用java多線程和圖形編程,完成紅綠燈模擬軟體
boolean flag = true;
while(flag){
//綠燈顯示
//sleep(3000);
//黃燈顯示
sleep(3000);
//紅燈顯示
sleep(3000);
}
如果點擊結束,退出while
Ⅲ 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程序,有圖形界面燈會跳的那種!
看一下這個行不行?
--------------------------------------------------------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class App extends JFrame {
public App() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(107, 252);
setLocationRelativeTo(null);
getContentPane().setLayout(new BorderLayout(0, 0));
MyColorPanel panel = new MyColorPanel();
getContentPane().add(panel, BorderLayout.CENTER);
setVisible(true);
new Thread(panel).start();
}
public static void main(String[] args) {
new App();
}
}
class MyColorPanel extends JPanel implements Runnable {
private Color[] colors = { Color.RED, Color.YELLOW, Color.GREEN };
private int[] y = { 10, 70, 130 };
private int[] time = { 3, 1, 5 };
private int index = 2;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = 50;
int x = 30;
for (int i = 0; i < y.length; i++) {
g.fillOval(x, y[i], width, width);
}
g.setColor(colors[index]);
g.fillOval(x, y[index], width, width);
}
public void run() {
while (true) {
try {
Thread.sleep(time[index] * 1000);
index--;
repaint();
if (index < 0) {
index = 2;
}
} catch (Exception e) {
}
}
}
}
Ⅳ 求一個簡易的JAVA程序---紅綠燈,有兩個部分,用下拉表控制燈的顏色,選擇紅,畫布中的燈就變紅的那種
按照你的要求,寫出的程序如下:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TrafficLight {
JFrame jf=new JFrame("Traffic Light");
JPanel jp=new JPanel();
JComboBox jcb=new JComboBox();
public TrafficLight(){
jcb.addItem("紅燈");
jcb.addItem("黃燈");
jcb.addItem("綠燈");
jcb.addItemListener(new JComboBoxListener());
jf.add(jcb,BorderLayout.NORTH);
jf.add(jp,BorderLayout.CENTER);
jf.setSize(400,400);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new TrafficLight();
}
class JComboBoxListener implements ItemListener{
@Override
public void itemStateChanged(ItemEvent ie) {
Graphics g=jf.getGraphics();
if(ie.getItem().equals("紅燈")){
g.setColor(Color.RED);
g.fillOval(200, 200, 100, 100);
}else if(ie.getItem().equals("黃燈")){
g.setColor(Color.YELLOW);
g.fillOval(200, 200, 100, 100);
}else if (ie.getItem().equals("綠燈")){
g.setColor(Color.GREEN);
g.fillOval(200, 200, 100, 100);
}
}
}
}
Ⅵ java 紅綠燈 代碼
給程序一個變數tag 用來記錄按鍵次數~~
tag%2==0時為綠
tag%2==1時為紅
tag%2==2時為黃
Ⅶ 請幫我看下這個JAVA的程序(簡易紅綠燈)
發現你的程序窗體大小每改變一次,都會加上三個RadioButtons。
Ⅷ 用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 紅綠燈 Thread 要求 運行時 下邊出現 紅燈亮 黃燈暗 綠燈暗 文字 按照紅黃綠順序 循環出現10次即可!
如果是使用多線程來做分別有 紅 \黃 \ 綠 三個線程,是無法控制輸出的順序的,多線程的概念里有CPU隨機執行的.
使用單線程式控制制吧 用System.println.out() 列印還差不多
Code:
public class TrafficLightThread implements Runnable {
final static String RED = "紅燈亮 ";
final static String YELLOW = "黃燈暗";
final static String GREEN = "綠燈暗";
public void run() {
for (int i = 10; i > 0; i--) {
System.out.println("---第"+i+"次---");
System.out.println(RED);
System.out.println(YELLOW);
System.out.println(GREEN);
}
}
public static void main(String[] args) {
new Thread(new TrafficLightThread()).start();
}
}
Ⅹ java控制紅綠燈及模擬車輛運動
寫兩個程序分別模擬紅綠燈和汽車:
1)紅綠燈程序以報文形式通知汽車程序;
2)汽車程序需要用多線程來實現。
3)紅綠燈程序用個循環,每隔n分鍾紅綠燈轉換,同時通知汽車程序;