java代码模拟灯泡点亮 java开灯问题

用java编写交通信号灯

按照你的要求编写的红绿灯程序,你看看吧,比较简单。

为康保等地区用户提供了全套网页设计制作服务,及康保网站建设行业解决方案。主营业务为成都网站建设、做网站、康保网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

完整的程序如下:

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

}

}

}

}

java一个关于light类【灯】的编程

建议用eclipse工具,这样的一个类,用工具很快就生成了。

public

class

light

{

private

int

watts;

private

boolean

indicator;

/**

*

@param

watts

*

:创建具有watts瓦的对象

*/

public

light(int

watts)

{

this.watts

=

watts;

}

/**

*

@param

watts

*

:创建具有watts瓦的对象

*

*

@param

indicator

*

:创建具有watts瓦,开关状态

为indicator的对象

*/

public

light(int

watts,

boolean

indicator)

{

super();

this.watts

=

watts;

this.indicator

=

indicator;

}

/**

*

开灯,即灯的状态置为开

*/

public

void

swithoh()

{

this.indicator

=

true;

}

/**

*

关灯

*/

public

void

printlnoff()

{

this.indicator

=

false;

}

/**

*

输出灯的瓦数信息和开灯的状态·

*/

public

void

printlnfo()

{

system.out.println("灯的瓦数是:"

+

this.watts

+

"\n开关状态是:"

+

this.indicator);

}

}

如何用java来实现灯泡异常问题

模拟了一下。。设置成B1按下时B4弹起,B4按下时B1弹起,不然有的时候电源是开是关不好判定。。如果B1/B4是一个按钮就好了 = =。为方便观察,间隔时间设为0.5s。另外加了两个按钮用来控制灯泡1、2是否故障。import java.awt.*;

import java.awt.event.*;

import javax.swing.*;class Bulb extends JFrame

{ private final int INTER_SECOND = 500; // 间隔时间 private BulbButton b1 = new BulbButton("B1");

private BulbButton b2 = new BulbButton("B2");

private BulbButton b3 = new BulbButton("B3");

private BulbButton b4 = new BulbButton("B4"); private BulbLight l1 = new BulbLight(1);

private BulbLight l2 = new BulbLight(2); private JButton bb1 = new BuldStateButton(l1);

private JButton bb2 = new BuldStateButton(l2); private JLabel state = new JLabel("关闭"); private CheckLightThread thread; public Bulb() {

this.setTitle("灯泡模拟");

this.setSize(300, 200);

this.setLayout(new GridLayout(4, 1)); this.add(new JPanel() {

{

this.setLayout(new FlowLayout(FlowLayout.CENTER));

this.add(new JLabel("电源状态:"));

this.add(state);

}

}); this.add(new JPanel() {

{

this.setLayout(new FlowLayout(FlowLayout.CENTER));

this.add(l1);

this.add(l2);

}

}); this.add(new JPanel() {

{

this.setLayout(new FlowLayout(FlowLayout.CENTER));

this.add(b1);

this.add(b2);

this.add(b3);

this.add(b4);

}

}); this.add(new JPanel() {

{

this.setLayout(new FlowLayout(FlowLayout.CENTER));

this.add(bb1);

this.add(bb2);

}

}); setButtons(); this.setVisible(true);

} private void setButtons() {

b2.setEnabled(false);

b3.setEnabled(false);

b4.setSelected(true); b1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

b1.changeState();

if(b1.isSelected()) {

b4.setSelected(false);

b2.setEnabled(true);

b3.setEnabled(true);

} else {

b2.setSelected(false);

b3.setSelected(false);

b4.setSelected(true);

b2.setEnabled(false);

b3.setEnabled(false);

}

changeSystemState();

}

}); b2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(!b1.isSelected() b4.isSelected()) return; // 在电源关闭状态下,B2和B3按钮不起作用

b2.changeState();

if(b2.getCount() b3.getCount()) { // 如果B2被按下的次数比B3被按下的次数多,L1亮

l1.change(true);

l2.change(false);

}

}

}); b3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(!b1.isSelected() b4.isSelected()) return; // 在电源关闭状态下,B2和B3按钮不起作用

b3.changeState();

if(b3.getCount() = b2.getCount()) { // 如果B2被按下的次数比B3被按下的次数少,L2亮

l1.change(false);

l2.change(true);

}

}

}); b4.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

b4.changeState();

if(b4.isSelected()) {

b1.setSelected(false);

b2.setSelected(false);

b3.setSelected(false);

b2.setEnabled(false);

b3.setEnabled(false);

} else {

b1.setSelected(true);

b2.setEnabled(true);

b3.setEnabled(true);

}

changeSystemState();

}

});

} private void changeSystemState() {

if(!b1.isSelected() b4.isSelected()) { // 在电源关闭状态下,灯应不亮

state.setText("关闭");

l1.change(false);

l2.change(false);

thread.pause();

}

if(b1.isSelected() !b4.isSelected()) { // 从最近一次电源打开状态算起

state.setText("开启");

b2.clearCount();

b3.clearCount();

thread = new CheckLightThread();

thread.start();

}

} public static void main(String[] args)

{

new Bulb();

} private class BulbButton extends JToggleButton

{

private int count = 0; public BulbButton(String text) {

super(text);

} public void changeState() {

if(isSelected()) {

++count;

}

} public void clearCount() {

count = 0;

} public int getCount() {

return count;

} } private class BulbLight extends JPanel

{ private int id; private boolean bad; private boolean lightState; private JLabel label; private JLabel state = new JLabel("【暗】"); private JLabel state2 = new JLabel("-正常"); public BulbLight(int id) {

this.id = id;

label = new JLabel("灯泡" + id);

this.add(label);

this.add(state);

this.add(state2);

} public int getId() {

return id;

} public void light() {

this.state.setText("【" + ("【暗】".equals(this.state.getText())?"亮":"暗") + "】");

} public void change(boolean light) {

if(bad)return;

lightState = light;

this.state.setText("【" + (lightState?"亮":"暗") + "】");

} public boolean isBad() {

return bad;

} public void setState(boolean bad) {

this.bad = bad;

state2.setText((bad?"-故障":"-正常"));

if(bad) {

this.state.setText("【暗】");

} else {

this.state.setText("【" + (lightState?"亮":"暗") + "】");

}

} } private class BuldStateButton extends JButton implements ActionListener

{

private BulbLight light; public BuldStateButton(BulbLight light) {

super("破坏灯泡" + light.getId());

this.light = light;

this.addActionListener(this);

} public void actionPerformed(ActionEvent e) {

if(("破坏灯泡" + light.getId()).equals(this.getActionCommand())) {

this.setText("修复灯泡" + light.getId());

light.setState(true);

}

else {

this.setText("破坏灯泡" + light.getId());

light.setState(false);

}

}

} private class CheckLightThread extends Thread

{ private boolean runFlag = true; public void pause() {

this.runFlag = false;

} public void run() {

while(runFlag) {

try

{

if (l1.isBad() !l2.isBad())

{

l2.light();

}

if (!l1.isBad() l2.isBad())

{

l1.light();

}

sleep(INTER_SECOND);

} catch (Exception e) {

System.out.println(e.getMessage());

}

}

}

}

}截图如下:

java创建灯泡类的一道题

public class Light {

boolean state;//灯的当前状态,false为关,true为开

int brightness;//灯的亮度,数值为0~100

/*初始化灯为关闭状态,亮度为0*/

public Light()

{

this.state=false;

this.brightness=0;

}

/*开关方法*/

public void turn()

{

if(this.state==true)

this.state=false;

else this.state=true;

}

/*获取状态方法*/

public boolean getState() {

return state;

}

/*获取灯泡亮度*/

public int getBrightness() {

return brightness;

}

/*调解灯泡亮度*/

public void setBrightness(int bn) {

if(this.state==true)//判断灯泡状态

if(bn=0bn=100) {//判断亮度调节值是不是合法

this.brightness = bn;

if(bn==0)//判断亮度是不是调到0

this.state=false;

}

}

public static void main(String[] args)

{

Light light=new Light();

light.turn();

System.out.println("当前灯泡状态:"+light.getState());

light.setBrightness(0);

System.out.println("当前灯泡亮度:"+light.getBrightness());

System.out.println("当前灯泡状态:"+light.getState());

light.setBrightness(10);

System.out.println("当前灯泡亮度:"+light.getBrightness());

System.out.println("当前灯泡状态:"+light.getState());

light.turn();

System.out.println("当前灯泡状态:"+light.getState());

light.setBrightness(10);

System.out.println("当前灯泡亮度:"+light.getBrightness());

System.out.println("当前灯泡状态:"+light.getState());

}

}

我建议你学习一下面向对象编程的思想方法


当前标题:java代码模拟灯泡点亮 java开灯问题
当前网址:http://scyanting.com/article/dosoghp.html