
正文
红绿灯java程序代码 红绿灯java程序代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Java红绿灯 求大神!!急啊
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Way extends JPanel {
JButton red;
public Way() {
red= new JButton("换灯");
setBackground(Color.yellow);
setSize(new Dimension(320, 260));
setPreferredSize(new Dimension(320, 260) );
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new FlowLayout());
red.setLayout(new FlowLayout());// 将单选按钮加入按钮面板
btnPanel.add(red);
add(red);
}
private void lightRed(Graphics g) {
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);
}
private void lightYellow(Graphics g) {
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);
}
private void lightGreen(Graphics g) {
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);
}
protected void paintComponent(Graphics g) {
super.paintComponents(g);
Way a = new Way();
g.clearRect(0, 0, getWidth(), getHeight());
g.drawRect(getWidth() / 2, 30, 30, 80);
red.addActionListener(new ActionListener() {
int f1 = 0;
public void actionPerformed(ActionEvent e) {
Graphics g = getGraphics();
switch (f1) {
case 0:
a.lightRed(g);
break;
case 1:
a.lightYellow(g);
break;
case 2:
a.lightGreen(g);
break;
}
f1++;
if(f12) f1=0;
};
});
}
public static void main(String[] args) {
JFrame fr = new JFrame("邮件界面模拟");
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 点击x结束程序
Container contentPane = fr.getContentPane();
// 得到窗口内容面板
contentPane.add(new Way());
fr.pack();
fr.setVisible(true); // 设置窗口可见
}
}大致帮红绿灯java程序代码你改红绿灯java程序代码了下红绿灯java程序代码,不知道符合不符合你的要求,有问题请追问
相关问答
Q1: 【急求】高手帮忙编写红绿灯的JAVA程序,有图形界面灯会跳的那种!
看一下这个行不行红绿灯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) {
}
}
}
}
Q2: java 线程实现一个红绿灯问题
关键是启动一个线程控制颜色。代码如下。
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
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() {
ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
exec.scheduleAtFixedRate(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);
}
}
}
Q3: 用java编写交通信号灯
按照红绿灯java程序代码你的要求编写的红绿灯程序,你看看吧,比较简单。
完整的程序如下红绿灯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程序代码和红绿灯java程序代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







