请帮我看下这个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
需要我们解决什么问题哦?