RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
java灯泡动画的源代码 eclipse中灯泡+错什么意思

使用Java做一个走马灯,源代码?

import java.awt.Color;

网站的建设创新互联公司专注网站定制,经验丰富,不做模板,主营网站定制开发.小程序定制开发,H5页面制作!给你焕然一新的设计体验!已为发电机维修等企业提供专业服务。

import java.awt.Dimension;

import java.awt.Font;

import java.awt.HeadlessException;

import java.awt.Point;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.ComponentAdapter;

import java.awt.event.ComponentEvent;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JViewport;

import javax.swing.Timer;

public class Test84 extends JFrame {

private Timer timer;

private JLabel view;

private JViewport window;

public static void main(String[] args)

{

JFrame frm = new Test84("跑马灯");

frm.setDefaultCloseOperation(EXIT_ON_CLOSE);

frm.pack();

frm.setVisible(true);

}

public Test84(String title) throws HeadlessException

{

super(title);

initComponents();

addComponentListener(new ComponentAdapter() {

public void componentResized(ComponentEvent e)

{

anchor = new Point();

anchor.x = -window.getExtentSize().width;

timer.start();

}

});

timer = new Timer(100, new ActionListener() {

public void actionPerformed(ActionEvent e)

{

animate();

}

});

timer.setInitialDelay(0);

}

private void initComponents()

{

String s = JOptionPane.showInputDialog(null, "请输入要实现效果的文字:");

view = new JLabel(s);

view.setFont(Font.decode("Dialog-BOLD-36"));

view.setForeground(Color.BLUE);

window = new JViewport();

window.setView(view);

getContentPane().add(window);

}

Point anchor;

private void animate()

{

Dimension extSize = window.getExtentSize();

Dimension viewSize = view.getPreferredSize();

anchor.x += 5;//设置移动的速度

window.setViewPosition(anchor);

if (anchor.x  viewSize.width)

anchor.x = -extSize.width;

}

}

如何用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());

}

}

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

用eclipse编写Java源程序时,出现一个黄色的灯泡和感叹号是怎么回事啊?请前辈们指导下,谢谢了!

这是警告的意思。一般只要你用集合没有用泛型就会出来。或者有些过时的方法也会这样的情况发生。


分享题目:java灯泡动画的源代码 eclipse中灯泡+错什么意思
标题来源:http://lswzjz.com/article/dosgees.html