返回
基础
分类

用java写一个自己的时钟

日期: 2020-03-23 16:39 浏览次数 : 146

import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
必赢手机登录网址 ,import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DateFormat;
import java.util.Date;

package com.yds.gui.fourpart;

 

public class Test5 {
 public static void main(String arg[]) {
  Frame f=new Frame("时钟");
  Panel p=new Panel();
  Label l=new Label("显示时钟");
  myThread mt=new myThread(l);
  l.setText(mt.showi());
  p.add(l);
  f.add(p);
  f.addWindowListener(new WindowAdapter(){

import java.awt.Button;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.awt.*;
import java.awt.event.*;

   @Override
   public void windowClosing(WindowEvent arg0) {
    System.exit(0);
   }
   
  });
  f.setLocation(200, 30);
  f.setSize(200, 80);
  f.setVisible(true);
  mt.start();
 }
}
class myThread extends Thread {
 private Label label;
 public myThread(Label lab){
  this.label=lab;
 }
     public  String showi(){
      Date date = new Date();
   int year = date.getYear() + 1900;
   int yue = date.getMonth() + 1;
   int re = date.getDate();
   int hh = date.getHours();
   int dd = date.getMinutes();
   int ss = date.getSeconds();
   return year + "年" + yue + "月" + re + "日t" + hh + ":"
     + dd + ":" + ss;// 得到日期
  }
    
 public void run() {
  while (true) {
   label.setText(showi());
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

public class TextAreaBea {
    Frame f;
    TextArea ta;
    Button b;
    TextField tf;

public class TestTextField implements ActionListener{
 TextField name;
 TextField password;
 
 public static void main( String args[]) {
  TestTextField ttf = new TestTextField();
  ttf.createUI();
 }
 
 public void createUI(){
  Frame f = new Frame("登录界面");
  f.add(new Label("请输入您的用户信息:"),"North");
  
  Panel p1 = new Panel();
  p1.setLayout(new BorderLayout());  
  Panel p11 = new Panel();
  p11.setLayout(new GridLayout(2,1));
  p11.add(new Label("用户名:"));
  p11.add(new Label("密  码:"));  
  Panel p12 = new Panel();
  p12.setLayout(new GridLayout(2,1));
  name = new TextField(10);
  name.addActionListener(this);
  password = new TextField(10);
  password.setEchoChar('*');
  password.addActionListener(this);
  p12.add(name);
  p12.add(password);  
  p1.add(p11,"West");
  p1.add(p12,"Center");
  
  Panel p2 = new Panel();  
  Button submit = new Button("提交");
  Button reset = new Button("重置");
  submit.addActionListener(this);
  reset.addActionListener(this);

}

    public void UserUI() {
        f = new Frame("提交事件");
        ta = new TextArea();
        Panel p = new Panel();
        b = new Button("提交");
        tf = new TextField(15);
        
        Monst m = new Monst(ta, tf,b);
        tf.addActionListener(m);
        ta.addMouseMotionListener(m);
        b.setActionCommand("commit");
        b.addActionListener(m);
        p.add(tf);
        p.add(b);
        f.add(ta, "Center");

  p2.add(submit);
  p2.add(reset);
  
  f.add(p1,"Center");
  f.add(p2,"South");
  f.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e){
    System.exit(0); 
   } 
  }); 
  f.setSize(200,130);
  f.setLocation(300,200);
  f.setVisible( true);
 }
 
 public void actionPerformed(ActionEvent e){
  String s = e.getActionCommand();
  if(s.equals("重置")){
   this.clear();
  }else if(s.equals("提交") || (e.getSource()==name) || (e.getSource()==password)){
   this.submit(); 
  }   
 } 
 public void clear(){
  name.setText("");
  password.setText(""); 
 }
 public void submit(){
  String n = name.getText();
  String psw = password.getText();
  System.out.println("用户名:" + n + "t密码:" + psw); 
 }
}

        f.add(p, "South");
        f.pack();
        f.addWindowListener(new WindowAdapter() {

 

            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }

        });
        f.setVisible(true);
    }

    public static void main(String arg[]) {
        TextAreaBea tab = new TextAreaBea();
        tab.UserUI();
    }
}

上面是界面类

package com.yds.gui.fourpart;

import java.awt.Button;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseMotionAdapter;

public class Monst extends MouseMotionAdapter implements ActionListener {
    TextField tf;
    TextArea ta;
    Button b;
    Monst(){
        
    }
    Monst(TextArea ta,TextField tf,Button b){
        this.ta=ta;
        this.tf=tf;
        this.b=b;
    }
    
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand()=="commit"){
          ta.append("你说的是:"+tf.getText()+"n");
          tf.setText(null);
        }

    }

}