按照你的要求編寫(xiě)的紅綠燈程序,你看看吧,比較簡(jiǎn)單。
公司主營(yíng)業(yè)務(wù):成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)建站是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。創(chuàng)新互聯(lián)建站推出水城免費(fèi)做網(wǎng)站回饋大家。
完整的程序如下:
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);
}
}
}
}
//按回車(chē)鍵就可以
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Print {
public static void main(String[] args) {
JFrame jFrame = new JFrame ();
final JLabel jLabel = new JLabel ("按回車(chē)鍵!");
jFrame.setLayout(null);
jLabel.setBounds(80,50,500,80);
jFrame.add(jLabel);
jFrame.setSize(200, 200);
jFrame.setLocation(200, 300);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.addKeyListener(new KeyAdapter () {
int n = 0;
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER) {
n++;
if (n == 1)
jLabel.setText("綠");
else if (n == 2) {
jLabel.setText("紅");
} else if (n == 3) {
jLabel.setText("黃");
n = 0;
}
}
}
});
}
}
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時(shí)表示為紅色,為2時(shí)為黃色,為3時(shí)為綠色
protected void paintComponent(Graphics g) {
super.paintComponent(g);
switch(lightColor){
case 0://默認(rèn)全部燈都為黑色
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();
}
}
}
時(shí)間太匆忙,上面的程序還可以再進(jìn)行優(yōu)化的。當(dāng)功能是可以實(shí)現(xiàn)的。
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);?//?點(diǎn)擊x結(jié)束程序
Container?contentPane?=?fr.getContentPane();
//?得到窗口內(nèi)容面板
contentPane.add(new?Way());
fr.pack();
fr.setVisible(true);?//?設(shè)置窗口可見(jiàn)
}
}大致幫你改了下,不知道符合不符合你的要求,有問(wèn)題請(qǐng)追問(wèn)
按照你的要求,寫(xiě)出的程序如下:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TrafficLight {
JFrame jf=new JFrame("Traffic Light");
JPanel jp=new JPanel();
JComboBox jcb=new JComboBox();
public TrafficLight(){
jcb.addItem("紅燈");
jcb.addItem("黃燈");
jcb.addItem("綠燈");
jcb.addItemListener(new JComboBoxListener());
jf.add(jcb,BorderLayout.NORTH);
jf.add(jp,BorderLayout.CENTER);
jf.setSize(400,400);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new TrafficLight();
}
class JComboBoxListener implements ItemListener{
@Override
public void itemStateChanged(ItemEvent ie) {
Graphics g=jf.getGraphics();
if(ie.getItem().equals("紅燈")){
g.setColor(Color.RED);
g.fillOval(200, 200, 100, 100);
}else if(ie.getItem().equals("黃燈")){
g.setColor(Color.YELLOW);
g.fillOval(200, 200, 100, 100);
}else if (ie.getItem().equals("綠燈")){
g.setColor(Color.GREEN);
g.fillOval(200, 200, 100, 100);
}
}
}
}