會寫程式的貓
2009年5月19日
網路程式設計 2009/05/20 作業
User:
MY BLOG
Class:
Java
,
Java - 網路程式設計 作業
/* 鬧鈴 */ import java.awt.*; import java.awt.event.*; public class h20090520ex1 extends Thread implements TextListener , ActionListener{ Frame frm = new Frame("Text Event"); TextField oldData = new TextField(12); TextField newData = new TextField(12); Label l1 = new Label("現在時間"); Label l2 = new Label("鬧鈴時間"); Label msg = new Label("Msg"); Button btn = new Button("啟動"); Panel p1 = new Panel(new GridLayout(1,2,5,5)); Panel p2 = new Panel(new GridLayout(1,4,5,5)); String s = ""; String newS = ""; int bnum = 0; public void run(){ boolean b = false; boolean b2 = false; try { while(true){ java.util.Date d = new java.util.Date(); sleep(1000); s = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(); oldData.setText(s); if( newS.equals(oldData.getText()) ){ msg.setText("時間到"); b2 = true; } if(b2 == true) if(b==false){ if(bnum%2==1) frm.setBackground(Color.red); else frm.setBackground(Color.white); b = true; } else{ if(bnum%2==1) frm.setBackground(Color.white); else frm.setBackground(Color.red); b = false; } } }catch (InterruptedException e){} } public h20090520ex1(){ frm.setLayout(new FlowLayout(FlowLayout.CENTER)); btn.addActionListener(this); p1.add(l1); p1.add(l2); p2.add(oldData); p2.add(newData); p2.add(btn); p2.add(msg); p1.setBounds(40,40,150,40); p2.setBounds(40,80,300,20); frm.setLayout(null); frm.add(p1); frm.add(p2); frm.setSize(350,150); frm.setVisible(true); getData(); } public void textValueChanged(TextEvent e){ } //按下 啟動 public void actionPerformed(ActionEvent e){ Button b=(Button)e.getSource(); // 取得事件來源物件 if(b == btn){ newS = newData.getText(); bnum++; if(bnum%2 == 0){ btn.setBackground(Color.red); }else{ btn.setBackground(Color.white); } } } private void getData(){ start(); System.out.println(s); } public static void main(String[] args){ new h20090520ex1(); } }
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言