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

来源:百度知道 编辑:UC知道 时间:2024/05/08 22:37:04
以下程序是希望,点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(fi

自动换颜色:

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 java.util.*;

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

public class lightP1 extends JFrame {

int flag=0;
myPanel panel_1 = new myPanel();

public lightP1(){
super();
setTitle("TrafficLight");
getContentPane().setLayout(new BorderLayout());

final JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.NORTH);

final ButtonGroup buttonGroup = new ButtonGroup();

final JRadioButton jrbRed = new JRadioButton();
jrbRed.setText("Red");
panel.add(jrbRed);

final JRad