请帮我看下这个JAVA的程序(简易红绿灯)

以下程序是希望,点RED后,第一个灯就亮(变成红色),然后点YELLOW,第二个灯就变成黄色,点GREEN,第三个灯就变成绿色.但是这个程序还没能实现,另外,希望那3个R... 以下程序是希望,点RED后,第一个灯就亮(变成红色),然后点YELLOW,第二个灯就变成黄色,点GREEN,第三个灯就变成绿色. 但是这个程序还没能实现,
另外,希望那3个RADIO BUTTON在灯的下面.
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;

public class lightP1 extends JFrame {
public lightP1 () {
setTitle("TrafficLight");
getContentPane().add(new myPanel());

}

/** Main method */
public static void main(String[] args) {
lightP1 frame = new lightP1();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 250);
frame.setVisible(true);
}
}

class myPanel extends JPanel {
protected void paintComponent(final Graphics g) {
super.paintComponent(g);

JRadioButton jrbRed;
JRadioButton jrbYellow;
JRadioButton jrbGreen;

JPanel jpRadioButtons = new JPanel();
jpRadioButtons.setLayout(new GridLayout(1, 3));
jpRadioButtons.add(jrbRed = new JRadioButton("Red"));
jpRadioButtons.add(jrbYellow = new JRadioButton("Yellow"));
jpRadioButtons.add(jrbGreen = new JRadioButton("Green"));
add(jpRadioButtons, BorderLayout.SOUTH);

//JPanel circles = new JPanel();
//circles.setLayout(new GridLayout(3, 1));
g.drawRect(130, 25, 40, 120);
//g.setColor(Color.red);
g.fillOval(135, 30, 30, 30);
//g.setColor(Color.yellow);
g.fillOval(135, 70, 30, 30);
//g.setColor(Color.green);
g.fillOval(135, 110,30, 30);

// Register listeners for check boxes
jrbRed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
g.setColor(Color.red);
}
});
jrbYellow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
g.setColor(Color.yellow);
}
});
jrbGreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
g.setColor(Color.green);
}
});
}
}
SORRY啊,能不能改成不用点,每次10秒自动换颜色,一打开红灯就亮,然后10秒后黄灯亮,10秒后绿灯亮................
展开
 我来答
魏蕤
2009-11-27 · TA获得超过426个赞
知道小有建树答主
回答量:231
采纳率:0%
帮助的人:242万
展开全部
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;

public class lightP1 extends JFrame {
private JRadioButton jrbRed;
private JRadioButton jrbYellow;
private JRadioButton jrbGreen;
private ButtonGroup gb = new ButtonGroup();
private myPanel mp = new myPanel();

public lightP1 () {
setTitle("TrafficLight");
JPanel jpRadioButtons = new JPanel();
jpRadioButtons.setLayout(new GridLayout(1, 3));
jrbRed = new JRadioButton("Red");
jrbYellow = new JRadioButton("Yellow");
jrbGreen = new JRadioButton("Green");

gb.add(jrbRed);
gb.add(jrbYellow);
gb.add(jrbGreen);

jrbRed.addActionListener(mp);
jrbYellow.addActionListener(mp);
jrbGreen.addActionListener(mp);

jpRadioButtons.add(jrbRed);
jpRadioButtons.add(jrbYellow);
jpRadioButtons.add(jrbGreen);

this. getContentPane().add(jpRadioButtons, BorderLayout.SOUTH);
getContentPane().add(mp);
}

/** Main method */
public static void main(String[] args) {
lightP1 frame = new lightP1();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 250);
frame.setVisible(true);
}

class myPanel extends JPanel implements ActionListener{
private int lightColor = 0;//该值保存的是color值,值为1时表示为红色,为2时为黄色,为3时为绿色

protected void paintComponent(Graphics g) {
super.paintComponent(g);
switch(lightColor){
case 0://默认全部灯都为黑色
g.drawRect(130, 25, 40, 120);
g.fillOval(135, 30, 30, 30);
g.fillOval(135, 70, 30, 30);
g.fillOval(135, 110,30, 30);
break;
case 1://红色灯亮
g.drawRect(130, 25, 40, 120);
g.setColor(Color.RED);
g.fillOval(135, 30, 30, 30);
g.setColor(Color.BLACK);
g.fillOval(135, 70, 30, 30);
g.fillOval(135, 110,30, 30);
break;
case 2://黄色灯亮
g.drawRect(130, 25, 40, 120);
g.setColor(Color.BLACK);
g.fillOval(135, 30, 30, 30);
g.setColor(Color.YELLOW);
g.fillOval(135, 70, 30, 30);
g.setColor(Color.BLACK);
g.fillOval(135, 110,30, 30);
break;
case 3://绿色灯亮
g.drawRect(130, 25, 40, 120);
g.setColor(Color.BLACK);
g.fillOval(135, 30, 30, 30);
g.setColor(Color.BLACK);
g.fillOval(135, 70, 30, 30);
g.setColor(Color.GREEN);
g.fillOval(135, 110,30, 30);
break;
default:break;
}
}

public void actionPerformed(ActionEvent e){
if(e.getSource() == jrbRed){ lightColor = 1;}
else if(e.getSource() == jrbYellow) {lightColor = 2;}
else if(e.getSource() == jrbGreen) {lightColor = 3;}
repaint();
}
}
}
时间太匆忙,上面的程序还可以再进行优化的。当功能是可以实现的。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
AiPPT
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图... 点击进入详情页
本回答由AiPPT提供
ヤマザキ266e77
2009-11-27 · TA获得超过2052个赞
知道小有建树答主
回答量:721
采纳率:0%
帮助的人:987万
展开全部
在你那个上面改了下

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;

public class lightP1 extends JFrame {
JRadioButton jrbRed;

JRadioButton jrbYellow;

JRadioButton jrbGreen;

JPanel jpRadioButtons = new JPanel();

JPanel mainPanel = new JPanel();

myPanel mp = new myPanel();

public lightP1() {
ButtonGroup bg = new ButtonGroup();
jpRadioButtons.setLayout(new GridLayout(1, 3));
jpRadioButtons.add(jrbRed = new JRadioButton("Red"));
jpRadioButtons.add(jrbYellow = new JRadioButton("Yellow"));
jpRadioButtons.add(jrbGreen = new JRadioButton("Green"));
bg.add(jrbRed);
bg.add(jrbYellow);
bg.add(jrbGreen);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(mp, BorderLayout.CENTER);
mainPanel.add(jpRadioButtons, BorderLayout.SOUTH);

mp.repaint();

// Register listeners for check boxes
jrbRed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mp.setColor(1);
mp.repaint();
}
});
jrbYellow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mp.setColor(2);
mp.repaint();
}
});
jrbGreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mp.setColor(3);
mp.repaint();
}
});
setTitle("TrafficLight");
getContentPane().add(mainPanel);

}

/** Main method */
public static void main(String[] args) {
lightP1 frame = new lightP1();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 250);
frame.setVisible(true);
}
}

class myPanel extends JPanel {

Color redColor = Color.BLACK;

Color greenColor = Color.BLACK;

Color yellowColor = Color.BLACK;

public myPanel() {

}

public void setColor(int i) {
if (i == 1) {
redColor = Color.RED;
greenColor = Color.BLACK;
yellowColor = Color.BLACK;
} else if (i == 2) {
redColor = Color.BLACK;
greenColor = Color.BLACK;
yellowColor = Color.YELLOW;
} else if (i == 3) {
redColor = Color.BLACK;
greenColor = Color.GREEN;
yellowColor = Color.BLACK;
}
}

public void paint(Graphics g) {
g.setColor(new Color(238,238,238));
g.fillRect(0, 0, 300, 250);
g.setColor(Color.BLACK);
g.drawRect(130, 25, 40, 120);
g.setColor(redColor);
g.fillOval(135, 30, 30, 30);
g.setColor(yellowColor);
g.fillOval(135, 70, 30, 30);
g.setColor(greenColor);
g.fillOval(135, 110, 30, 30);

}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
jackmawt
2009-11-27
知道答主
回答量:43
采纳率:0%
帮助的人:17.1万
展开全部
我也凑个热闹.下面是我改的
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Light extends JFrame implements ActionListener {
private static final long serialVersionUID = 544830021014804292L;
JButton btnYellow = new JButton();
JButton btnBlue = new JButton();
JButton btnRed = new JButton();
JPanel parentPanel = new JPanel();
JPanel sonPanel = new JPanel();

// XYLayout xYLayout1 = new XYLayout();

public Light() {

}

public static void main(String[] args) {
Light t = new Light();
try {
t.jbInit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setSize(400, 600);
t.setVisible(true);
}

public void jbInit() throws Exception {

btnYellow.setText("Yellow");
btnYellow.setBounds(new Rectangle(35, 23, 97, 29));
this.setLayout(null);
btnBlue.setBounds(new Rectangle(154, 21, 97, 29));
btnBlue.setText("Blue");
btnRed.setBounds(new Rectangle(272, 24, 97, 29));
btnRed.setText("Red");
parentPanel.setBorder(BorderFactory.createRaisedBevelBorder());
parentPanel.setBounds(new Rectangle(27, 68, 358, 227));

this.add(btnYellow, null);
this.add(btnBlue, null);
this.add(btnRed, null);
this.add(parentPanel, null);

btnYellow.addActionListener(this);
btnRed.addActionListener(this);
btnBlue.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {
// this code is used to change the backgracolor
Object source = e.getSource();
Color color = null;// =getBackground();
if (source == btnYellow)
color = Color.yellow;
else if (source == btnRed)
color = Color.red;
else if (source == btnBlue)
color = Color.blue;
parentPanel.setBackground(color);
repaint();
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友0e2e5a0
2009-11-27 · TA获得超过8490个赞
知道小有建树答主
回答量:313
采纳率:0%
帮助的人:229万
展开全部
发现你的程序窗体大小每改变一次,都会加上三个RadioButtons。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
楼市酒客
2009-11-27
知道答主
回答量:29
采纳率:0%
帮助的人:0
展开全部
需要我们解决什么问题哦?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
收起 更多回答(4)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式