问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

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

发布网友 发布时间:2022-04-29 01:21

我来回答

5个回答

热心网友 时间:2022-06-27 10:57

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();
}
}
}
时间太匆忙,上面的程序还可以再进行优化的。当功能是可以实现的。

热心网友 时间:2022-06-27 10:57

在你那个上面改了下

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);

}
}

热心网友 时间:2022-06-27 10:58

我也凑个热闹.下面是我改的
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();
}
}

热心网友 时间:2022-06-27 10:58

发现你的程序窗体大小每改变一次,都会加上三个RadioButtons。

热心网友 时间:2022-06-27 10:59

需要我们解决什么问题哦?
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
国外留学有用吗 花钱出国留学有用吗 !这叫什么号 百万医疗赔付后是否可以续保 前一年理赔过医疗险还能续保吗? 医疗住院险理赔后还能购买吗? 女生多大后可以不在长身高? 如何不用软件把手机投屏到电脑上手机屏幕怎样投放到电脑上 战时拒绝、故意延误军事订货罪既遂的处罚? 战时故意延误军事订货罪处罚标准 创业板开通要求 谁有做过的HTML饼图的代码?能否给我参考一下 java画线程序drawline怎么都不管用,什么问题 求一个JAVA小程序 本人菜鸟 请问java中drawLine函数的具体用法是什么? 举例如下,怎么才能... blackboard怎么读 lucy ,draw on the blackboard. 改否句用don't ,doesn't? lucy, draw on the blackboard. 改否用don't 还是doesn't? they often draw pictures on the blackboard用now改写 blackdraw是什么意思 blackdraw nvidia控制面板把参数设置好后,禁止开机启动,参数仍然有效吗? 720度拍摄的拍摄方法有什么? 华为手机蓝牙4.2版本可以升级蓝牙5.0版本吗 荣耀x20有克隆功能吗 华为p30pro蓝牙版本 生活中的小镜头作文 生活中的小镜头作文400字 生活中有哪些让你感觉到温暖的小事? 生活中有哪些小事让你感觉生活很美好? 为什么明明装了ARIAL BLACK字体,COREL DRAW 总说缺失 drawline怎么实现已定义2点的连接? winform中同样的一段代码,窗体加载事件里没有效果,而鼠标点击事件或paint有效果,??? C# groupBox上画图问题 淘宝上的咸蛋黄酱是什么? 玩通 乌鲁斯拉格纳 征战的决斗者 的求教 乌鲁斯拉格纳征战的决斗者最终BOSS怎么打 12年7月动漫新番 弑神者里面的这个人的CV是皆川纯子吗 乌鲁斯拉格纳征战的决斗者这个迷宫怎么继续下去 动画弑神者中乌鲁斯拉格纳权能的召唤语。像召唤野猪,白马之类的之前说的话。 求几部类似于夏娃年代记的RPG游戏 夏季番是什么时候 有一个动漫是男主穿越到了异世界那个世界的学校全是亚人就男主是人类然后跟妹子*就会获得妹子的能力? 艾丽卡·布朗特里的角色设定 求rosebleu社的作品大全 找一部动漫,大概男主第一集去一个地方,然后杀了一个神,获得了一个神位,然后开始神与神之间的争斗,还 卡片galgame 除了创刻还有吗 最让人羡慕的男主都有谁? 【MAD接吻专场】谁知道这动漫叫什么?有图