import?java.awt.Color;
網(wǎng)站的建設(shè)創(chuàng)新互聯(lián)公司專注網(wǎng)站定制,經(jīng)驗豐富,不做模板,主營網(wǎng)站定制開發(fā).小程序定制開發(fā),H5頁面制作!給你煥然一新的設(shè)計體驗!已為發(fā)電機維修等企業(yè)提供專業(yè)服務(wù)。
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,?"請輸入要實現(xiàn)效果的文字:");
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;//設(shè)置移動的速度
window.setViewPosition(anchor);
if?(anchor.x??viewSize.width)
anchor.x?=?-extSize.width;
}
}
模擬了一下。。設(shè)置成B1按下時B4彈起,B4按下時B1彈起,不然有的時候電源是開是關(guān)不好判定。。如果B1/B4是一個按鈕就好了 = =。為方便觀察,間隔時間設(shè)為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("關(guān)閉"); 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("電源狀態(tài):"));
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; // 在電源關(guān)閉狀態(tài)下,B2和B3按鈕不起作用
b2.changeState();
if(b2.getCount() b3.getCount()) { // 如果B2被按下的次數(shù)比B3被按下的次數(shù)多,L1亮
l1.change(true);
l2.change(false);
}
}
}); b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!b1.isSelected() b4.isSelected()) return; // 在電源關(guān)閉狀態(tài)下,B2和B3按鈕不起作用
b3.changeState();
if(b3.getCount() = b2.getCount()) { // 如果B2被按下的次數(shù)比B3被按下的次數(shù)少,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()) { // 在電源關(guān)閉狀態(tài)下,燈應(yīng)不亮
state.setText("關(guān)閉");
l1.change(false);
l2.change(false);
thread.pause();
}
if(b1.isSelected() !b4.isSelected()) { // 從最近一次電源打開狀態(tài)算起
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("修復(fù)燈泡" + 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());
}
}
}
}
}截圖如下:
public class Light {
boolean state;//燈的當(dāng)前狀態(tài),false為關(guān),true為開
int brightness;//燈的亮度,數(shù)值為0~100
/*初始化燈為關(guān)閉狀態(tài),亮度為0*/
public Light()
{
this.state=false;
this.brightness=0;
}
/*開關(guān)方法*/
public void turn()
{
if(this.state==true)
this.state=false;
else this.state=true;
}
/*獲取狀態(tài)方法*/
public boolean getState() {
return state;
}
/*獲取燈泡亮度*/
public int getBrightness() {
return brightness;
}
/*調(diào)解燈泡亮度*/
public void setBrightness(int bn) {
if(this.state==true)//判斷燈泡狀態(tài)
if(bn=0bn=100) {//判斷亮度調(diào)節(jié)值是不是合法
this.brightness = bn;
if(bn==0)//判斷亮度是不是調(diào)到0
this.state=false;
}
}
public static void main(String[] args)
{
Light light=new Light();
light.turn();
System.out.println("當(dāng)前燈泡狀態(tài):"+light.getState());
light.setBrightness(0);
System.out.println("當(dāng)前燈泡亮度:"+light.getBrightness());
System.out.println("當(dāng)前燈泡狀態(tài):"+light.getState());
light.setBrightness(10);
System.out.println("當(dāng)前燈泡亮度:"+light.getBrightness());
System.out.println("當(dāng)前燈泡狀態(tài):"+light.getState());
light.turn();
System.out.println("當(dāng)前燈泡狀態(tài):"+light.getState());
light.setBrightness(10);
System.out.println("當(dāng)前燈泡亮度:"+light.getBrightness());
System.out.println("當(dāng)前燈泡狀態(tài):"+light.getState());
}
}
我建議你學(xué)習(xí)一下面向?qū)ο缶幊痰乃枷敕椒?/p>
這是警告的意思。一般只要你用集合沒有用泛型就會出來。或者有些過時的方法也會這樣的情況發(fā)生。