java 窗口 倒计时 关闭

java 窗口 倒计时 关闭,第1张

package mainWindow;

import javaawt;

import javaxswing;

import javautil;

import javaioIOException;

import javaawtevent;

import javaxswingevent;

/程序文件:ShutDownWindowjava

程序功能:程序的定时关机窗体

更新时间:20091026

/

public class ShutDownWindow extends JFrame implements ActionListener,DocumentListener{

/

/

private static final long serialVersionUID = 2084958924713854452L;

JTextField 时,分,秒;

JLabel 现在时间,设置时,设置分,设置秒;

JButton 关机,重启,注销,清空,取消;

Box 左,中,右,中部; //Box是使用 BoxLayout 对象作为其布局管理器的一个轻量级容器

JPanel 底部;

Container 容器;

int 标志,nh,nm,ns,wh,wm,ws;

ShutDownWindow(String s){

super(s);

setVisible(true);

setResizable(false); //设置窗体是否允许用户调整窗体大小

setSize(300,200);

Dimension 屏幕=ToolkitgetDefaultToolkit()getScreenSize(); //获得屏幕大小

setLocation(屏幕width-300,0);

现在时间=new JLabel();

现在时间setForeground(ColorRED);

现在时间setText(nowTime());

时=new JTextField(4);

时setForeground(ColorBLUE);

时setText(null);

分=new JTextField(4);

分setForeground(ColorBLUE);

分setText(null);

秒=new JTextField(4);

秒setForeground(ColorBLUE);

秒setText(null);

设置时=new JLabel("设定小时");

设置分=new JLabel("设定分钟");

设置秒=new JLabel("设定秒钟");

关机=new JButton("关机");

重启=new JButton("重启");

注销=new JButton("注销");

取消=new JButton("取消");

清空=new JButton("清空");

关机setEnabled(false);

重启setEnabled(false);

取消setEnabled(false);

注销setEnabled(false);

清空setEnabled(false);//按钮不可用

中部=BoxcreateHorizontalBox(); //创建一个从左到右显示其组件的 Box

validate();

容器=getContentPane();

容器setLayout(new BorderLayout());

左=BoxcreateVerticalBox(); //创建一个从左到右显示其组件的 Box

中=BoxcreateVerticalBox();

右=BoxcreateVerticalBox();

左add(设置时);

左add(BoxcreateVerticalStrut(20)); //创建一个不可见的、固定高度的组件

左add(设置分);

左add(BoxcreateVerticalStrut(20));

左add(设置秒);

中add(时);

中add(BoxcreateVerticalStrut(9));

中add(分);

中add(BoxcreateVerticalStrut(9));

中add(秒);

右add(关机);

右add(BoxcreateVerticalStrut(10));

右add(重启);

右add(BoxcreateVerticalStrut(10));

右add(注销);

validate(); //使用 validate 方法会使容器再次布置其子组件

中部add(左);

中部add(BoxcreateHorizontalStrut(15));

中部add(中);

中部add(BoxcreateHorizontalStrut(15));

中部add(右);

中部validate();

底部=new JPanel();

底部setBackground(ColorGREEN);

底部add(清空);

底部add(取消);

底部validate();

容器add(现在时间,BorderLayoutNORTH);

容器add(中部,BorderLayoutCENTER);

容器add(底部,BorderLayoutSOUTH);

容器validate();

容器setBackground(ColorGRAY);

关机addActionListener(this); //为各按钮标签添加监听对象

重启addActionListener(this);

注销addActionListener(this);

取消addActionListener(this);

清空addActionListener(this);

时getDocument()addDocumentListener(this);

分getDocument()addDocumentListener(this);

秒getDocument()addDocumentListener(this);

// 匿名线程,实现时间动态显示 //

new Thread(new Runnable() {

public void run()

{

while(true) //为了实时监听所以得用死循环

{

try

{

Threadsleep(1000); //条件合适后线程休眠1秒钟

}

catch(Exception e)

{

eprintStackTrace(); //输入异常到指定地方

}

nowTime(); //调用nowTime()方法获得当前时间

}

}

})start(); //调用start()后线程处于就绪状态

}

// 得到当前系统时间并显示 //

public String nowTime(){

Calendar 日历=CalendargetInstance(); //使用默认时区和语言环境获得一个日历

int ny=日历get(CalendarYEAR);

int nmo=日历get(CalendarMONTH)+1; //因为月是从0开始算起的,所以得加上1

int nd=日历get(CalendarDAY_OF_MONTH);

nh=日历get(CalendarHOUR_OF_DAY);

nm=日历get(CalendarMINUTE);

ns=日历get(CalendarSECOND);

String ss=new String(" 现在是"+ny+"年"+nmo+"月"+nd+"日 "+nh+"时"+nm+"分"+ns+"秒");

现在时间setText(ss); //把获得的时间赋给“现在时间”标签

return ss;

}

// 响应Document事件,需重写三个方法 //

public void changedUpdate(DocumentEvent e){

if(时getText()equals("")||分getText()equals("")||秒getText()equals(""))

{

关机setEnabled(false);

重启setEnabled(false);

清空setEnabled(false);

注销setEnabled(false);

取消setEnabled(false);

}

else

{

关机setEnabled(true);

重启setEnabled(true);

清空setEnabled(true);

注销setEnabled(true);

}

}

public void removeUpdate(DocumentEvent e){

changedUpdate(e);

}

public void insertUpdate(DocumentEvent e){

changedUpdate(e);

}

// 响应事件要执行的方法 //

public void 关机程序(){ //关机

try{RuntimegetRuntime()exec("shutdownexe -s -t 5");}

catch(IOException e){

JOptionPaneshowMessageDialog(this,"执行失败!");

}

}

public void 重启程序(){ //重启

try{RuntimegetRuntime()exec("shutdownexe -r -t 5");}

catch(IOException e){

JOptionPaneshowMessageDialog(this,"执行失败!");

}

}

public void 注销程序(){ //注销

try{RuntimegetRuntime()exec("shutdownexe -l");}

catch(IOException e){

JOptionPaneshowMessageDialog(this,"执行失败!");

}

}

public void 取消程序(){ //取消

try{RuntimegetRuntime()exec("shutdownexe -a");}

catch(IOException e){

JOptionPaneshowMessageDialog(this,"执行失败!");

}

}

//时间到就执行的方法//

public void 到时执行(){

if(nh==wh && nm==wm && ns==ws){ //如果设定的时间和现在时间相同就执行

if(标志==1){

关机程序();

}

if(标志==2){

重启程序();

}

if(标志==3){

注销程序();

}

}

}

// 响应各按钮的单击事件 //

public void actionPerformed(ActionEvent e)

{

wh=IntegerparseInt(时getText());

wm=IntegerparseInt(分getText());

ws=IntegerparseInt(秒getText());

if(egetSource()==关机){

关机setEnabled(false);

重启setEnabled(false);

注销setEnabled(false);

标志=1;

if(wh>=0&&wh<=23&&wm>=0&&wm<=59&&ws>=0&&ws<=59){

动作();

}

else{

JOptionPaneshowMessageDialog(this,"时间设置满足:0=<小时<24,0=<分钟<60,0=<秒<60!","温馨提示",JOptionPaneWARNING_MESSAGE);

时requestFocus(); //获得焦点

}

}

if(egetSource()==重启){

关机setEnabled(false);

重启setEnabled(false);

注销setEnabled(false);

标志=2;

if(wh>=0&&wh<=23&&wm>=0&&wm<=59&&ws>=0&&ws<=59){

动作();

}

else

{

JOptionPaneshowMessageDialog(this,"时间设置满足:0=<小时<24,0=<分钟<60,0=<秒<60!","温馨提示",JOptionPaneWARNING_MESSAGE);

时requestFocus(); //获得焦点

}

}

if(egetSource()==注销){

关机setEnabled(false);

重启setEnabled(false);

注销setEnabled(false);

标志=3;

if(wh>=0&&wh<=23&&wm>=0&&wm<=59&&ws>=0&&ws<=59){

动作();

}

else

{

JOptionPaneshowMessageDialog(this,"时间设置满足:0=<小时<24,0=<分钟<60,0=<秒<60!","温馨提示",JOptionPaneWARNING_MESSAGE);

时requestFocus(); //获得焦点

}

}

if(egetSource()==取消){

取消程序();

取消setEnabled(false);

关机setEnabled(true);

重启setEnabled(true);

注销setEnabled(true);

}

if(egetSource()==清空){

时setText("");

分setText("");

秒setText("");

时requestFocus();

清空setEnabled(false);

}

}

public void 监听()//时间一到就执行动作

{

new Thread(new Runnable()

{

public void run()

{

while(true)

{

try

{

Threadsleep(1000);

}

catch(Exception e)

{

eprintStackTrace();

}

到时执行();//关机或重启或注销

}

}

})start();

}

public void 动作() //检查用户输入是否正确,在正确情况下时间到便执行

{

if(Test()==false)

{

JOptionPaneshowMessageDialog(this,"时间设定错误,应设定在现在之后!");

时requestFocus();

}

else{

JOptionPaneshowMessageDialog(this,"时间设定成功!");

取消setEnabled(true); //定义时间成功后取消按钮可点击

监听();

}

}

public boolean Test(){

boolean b=true;

if(wh<nh) //判断当前小时是否小于设定小时

b=false;

else if(wh==nh)

{

if(wm<nm) //判断当前分钟是否小于设定分钟

b=false;

else if(wm==nm)

{

if(ws<ns+5) //要有延迟

b=false;

}

}

return b;

}

}

服务端 用户名 y 密码 1 客户端代码在我的评论里,这敲不了那么多字,没办法。

登陆实现类:class Login

package comserver;

import javaawtColor;

import javaawtFont;

import javaawteventActionEvent;

import javaawteventActionListener;

import javaawteventWindowAdapter;

import javaawteventWindowEvent;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJLabel;

import javaxswingJPasswordField;

import javaxswingJTextField;

public class Login {

boolean f=false;//按登录时设置的一个标志

private JFrame frame = new JFrame("Welcome To MLDN") ;

//设置窗体

private JButton submit = new JButton("登陆");//设置登录按钮

private JButton reset = new JButton("重置");//设置重置按钮

private JLabel nameLab = new JLabel("服务器:") ;//标签实例化

private JLabel passLab = new JLabel("密 码:"); //标签实例化

private JLabel infoLab = new JLabel("服务器登陆系统"); //标签实例化

private JTextField nameText = new JTextField(10);//单行文本输入条

private JPasswordField passText = new JPasswordField() ;//单行密码文本输入条

public Login(){//登录窗口构造方法

Font fnt = new Font("Serief",FontITALIC + FontBOLD,12); //设置字体

infoLabsetFont(fnt) ; // 设置标签的显示文字

submitaddActionListener(new ActionListener(){ //采用内部匿名类

public void actionPerformed(ActionEvent e){

if(egetSource()==submit){ //判断触发器源是否是提交按钮

String tname = nameTextgetText() ; //得到输入的用户名

String tpass = new String(passTextgetPassword()) ;//得到输入的密码,此时通过getPassageword()方法返回的是字符数组

LoginCheck log = new LoginCheck(tname,tpass) ;//实例化LoginCheck对象,传入输入的用户名和密码

if(logvalidate()){//对用户名和密码进行验证

try{ //线程异常处理try

Threadsleep(2000); //2秒后打开聊天窗口

f=true; //登录成功后的表示项为true

framedispose(); //关闭本窗口

}catch(Exception e1){//异常获取

e1printStackTrace();

}

}else{

infoLabsetText("登陆失败,错误的用户名或密码!") ;//登录失败

}

}

}

});

resetaddActionListener(new ActionListener(){ //采用内部匿名类

public void actionPerformed(ActionEvent e){

if(egetSource()==reset){ //判断触发器源是否是提交按钮

nameTextsetText(""); //设置文本框中的内容

passTextsetText(""); //设置文本框中的内容

infoLabsetText("服务器登陆系统"); //恢复标签显示

}

}

});

frameaddWindowListener(new WindowAdapter(){//加入窗口监听

public void windowClosing(WindowEvent e){ }

}); // 加入事件

framesetLayout(null); //使用绝对定位

nameLabsetBounds(5,5,60,20); //设置标签的位置及大小

passLabsetBounds(5,30,60,20); //设置标签的位置及大小

infoLabsetBounds(5,65,220,30); //设置标签的位置及大小

nameTextsetBounds(65,5,100,20); //设置文本域的位置及大小

passTextsetBounds(65,30,100,20); //设置密码域的位置及大小

submitsetBounds(165,5,60,20); //设置按钮的位置及大小

resetsetBounds(165,30,60,20); //设置按钮的位置及大小

frameadd(nameLab); //向窗体加入标签

frameadd(passLab); //向窗体加入标签

frameadd(infoLab); //向窗体加入标签

frameadd(nameText); //向窗体加入文本框

frameadd(passText); //向窗体加入密码框

frameadd(submit); //向窗体加入按钮

frameadd(reset) ;//向窗体加入按钮

framesetSize(280,130); //设置窗体大小

framegetContentPane()setBackground(Colorgreen) ;//设置窗体的背景颜色

framesetLocation(300,200) ;//设置窗体在电脑桌面上的位置

framesetVisible(true); //显示窗口

while(f==false){ //当登录失败时,一直循环运行,

}

new MyServer();//显示窗体页面

}

}

验证登陆实现类 class LoginCheck

package comserver;

public class LoginCheck {

private String name ;//用户名

private String password ;//密码

//构造方法

public LoginCheck(String name,String password){

thisname = name ;//传递用户名

thispassword = password ;//传递密码

}

//验证用户名和密码

public boolean validate(){

//验证方法

if("y"equals(name)&&"1"equals(password)){ //判断用户名和密码是否正确

return true; //返回true

}else{

return false ;//返回false

}

}

}

package comserver;

import javaawt;

import javaawteventActionEvent;

import javaawteventActionListener;

import javaioBufferedReader;

import javaioInputStreamReader;

import javaioPrintWriter;

import javanetServerSocket;

import javanetSocket;

import javatextDateFormat;

import javautilDate;

import javaxswing;

聊天实现类 class MyServer

public class MyServer {

protected JLabel lab10;

// 全体变量,做传递的一个组件

String s = "已经成功连接";

// 初始字符串,在两个聊天窗口链接成功时输出,同时作为两个窗口传递字符的一个变量

// 构造方法

public MyServer(){

try{//异常处理

JFrame frame=new JFrame("服务器窗口");

//设置窗体

framesetLayout(null);

//让布局管理器为空,使用绝对定位

Font fnt=new Font("Serief",FontPLAIN,40);//字体设置

Font fnt1=new Font("Serief",FontPLAIN,20);//字体设置

//我的用户名显示

JLabel lab1=new JLabel("服务器名:",JLabelLEFT);

//标签实例化,文本左对齐

lab1setBounds(8, 10, 100, 20);

//设置组件位置及大小

frameadd(lab1);

//添加组件

JTextField text2=new JTextField(30); //单行文本输入组件

text2setBounds(150, 10, 200, 20);//设置组件位置及大小

text2setEnabled(false);//文本条不可编辑

text2setText("笑笑聊天室");//输入内容

text2setFont(fnt1);//设置字体

frameadd(text2);//添加组件

//服务器IP显示

JLabel lab2=new JLabel("当前服务器IP:",JLabelLEFT);

//标签实例化,文本左对齐

lab2setBounds(8, 45, 100, 20);//设置组件位置及大小

frameadd(lab2);//添加组件

JTextField text3=new JTextField(30);//单行文本输入组件

text3setBounds(150, 45, 200, 20);//设置组件位置及大小

text3setEnabled(false);

//文本不可编辑

text3setText("127001");//输入内容

text3setFont(fnt1);//设置字体

frameadd(text3);

//添加组件

//服务器端口显示

JLabel lab3=new JLabel("当前服务器端口:",JLabelLEFT);

//标签实例化,文本左对齐

lab3setBounds(8, 80, 100, 20);//设置组件位置及大小

frameadd(lab3);//添加组件

JTextField text4=new JTextField(30);//单行文本输入组件

text4setBounds(150, 80, 200, 20);//设置组件位置及大小

text4setEnabled(false);//文本不可编辑

text4setText("8888");//输入内容

text4setFont(fnt1);//设置字体

frameadd(text4);//添加组件

//聊天记录显示

JLabel lab4=new JLabel("聊天记录如下:",JLabelLEFT);

//标签实例化 文本左对齐

lab4setBounds(8, 115, 100, 20);//设置组件位置及大小

frameadd(lab4);//添加组件

final JTextArea text1=new JTextArea();//多行文本输入组件

text1setEnabled(false);//文本不可编辑

text1setLineWrap(true);//自动换行

JScrollPane scr=new JScrollPane(text1,JScrollPaneVERTICAL_SCROLLBAR_ALWAYS,JScrollPaneHORIZONTAL_SCROLLBAR_ALWAYS); //设置滚动条,水平和垂直滚动条始终显示

scrsetBounds(8, 150, 450, 350);//设置组件位置及大小

frameadd(scr);//添加组件

//聊天输入窗口及确定

JLabel lab5=new JLabel("请输入聊天内容:",JLabelLEFT); //标签实例化,文本左对齐

lab5setBounds(8, 500, 100, 20);//设置组件位置及大小

frameadd(lab5);//添加组件

final JTextArea text5=new JTextArea();//多行文本输入组件

text5setLineWrap(true);//自动换行

JScrollPane scr2=new JScrollPane(text5,JScrollPaneVERTICAL_SCROLLBAR_ALWAYS,JScrollPaneHORIZONTAL_SCROLLBAR_ALWAYS);//设置滚动条,水平和垂直滚动条始终显示

scr2setBounds(150, 500, 300, 50);//设置组件位置及大小

frameadd(scr2);//添加组件

final JButton but=new JButton("确定");//设置确定按钮

butsetFont(fnt);//添加字体设置

butsetBounds(480, 500, 200, 50);//设置组件位置及大小

butaddActionListener(

new ActionListener(){ //采用内部匿名类

public void actionPerformed(ActionEvent e){

if(egetSource()==but){ //判断触发器源是否是提交按钮

text1append("笑笑: "+lab10getText()+"\n ");//在聊天记录上添加文本

text1append(text5getText()+"\n"); //将输入的聊天内容输出在聊天记录上

s=text5getText(); //得到聊天内容

text5setText(""); // 将聊天窗口内容设置为空

}

}

});

frameadd(but);//添加组件

//当前时间显示

JLabel lab7=new JLabel("时间显示:",JLabelLEFT);//标签实例化,文本左对齐

lab7setBounds(450, 20, 100, 20);//设置组件位置及大小

frameadd(lab7);//添加组件

DateFormat df=DateFormatgetDateTimeInstance();//取得系统时间

String df2= dfformat(new Date()); //将时间转换成字符串

JLabel lab8=new JLabel(df2,JLabelLEFT);//标签实例化,文本左对齐

lab8setBounds(520, 20, 130, 20);//设置组件位置及大小

frameadd(lab8);//添加组件

lab10=lab8; //传递时间显示,以便能在聊天记录上显示记录时间

new Time(lab8); //使时间动态显示

//用户列表显示

JLabel lab6=new JLabel("用户列表:",JLabelLEFT);//标签实例化,文本左对齐

lab6setBounds(500, 40, 100, 20);//设置组件位置及大小

frameadd(lab6);//添加组件

JTextArea text6=new JTextArea();//标签实例化,文本左对齐

text6setEnabled(false);//文本不可编辑

text6setLineWrap(true);//自动换行

JScrollPane scr3=new JScrollPane(text6,JScrollPaneVERTICAL_SCROLLBAR_ALWAYS,JScrollPaneHORIZONTAL_SCROLLBAR_ALWAYS);//设置滚动条,水平和垂直滚动条始终显示

scr3setBounds(460, 70, 220, 420);//设置组件位置及大小

frameadd(scr3);//添加组件

//窗口的属性

framesetSize(700,600);//窗口大小

framegetContentPane()setBackground(Colorpink);//窗口的背景颜色

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);//关闭窗口时关闭程序

framesetLocation(100,50);//在电脑桌面上出现的位置

framesetVisible(true);//显示窗口

//连个窗口连接

//建立Server Socket并等待连接请求

ServerSocket server =new ServerSocket(8889);

Socket socket=serveraccept();

//连接建立,通过Socket获取连接上的输入/输出流

BufferedReader in=new BufferedReader(new InputStreamReader(socketgetInputStream()));

final PrintWriter out =new PrintWriter(socketgetOutputStream());

//先读取Client发送的数据,然后从标准输入读取数据发送给Client当接收到bye时关闭连接

String s1="",s2="";

//标志字符串,作为传递字符串使用

//得到其他窗口传递的字符串,并判断是否结束

while(!(s1=inreadLine())equals("bye")){

text6setText("可可在线"); //当连接成功是在用户列表中 输出用户名字

if(!s1equals("")){ //如果传递的字符串不为 空

text1append("可可: "+lab10getText()+"\n ");//在聊天记录上添加文本

text1append(s1+"\n"); //将输入的聊天内容输出在聊天记录上

}

s2=s;//将在聊天窗口中得到的字符串传递给输出字符串

for(;s2equals("")||s2equals(null);s2=s){//当传递的字符串为空时等待用户输入聊天内容

}

s="";//将在聊天窗口中得到的字符串设为空

if(!s2equals("")&&!s2equals(null)){// s2不为空时做

outprintln(s2); //向其他窗口输出字符串

}

outflush();//输出聊天内容

}

//关闭连接

inclose();

outclose();

socketclose();

serverclose();

}catch(Exception e){

}

}

//main函数

public static void main(String args[]){

new Login();//登录实现 } }

}

}

时间类 class Time

package comserver;

import javaawteventActionEvent;

import javaawteventActionListener;

import javatextSimpleDateFormat;

import javautilDate;

import javaxswingJLabel;

import javaxswingTimer; //时间类

public class Time {

public Time(JLabel time){

//构造方法 thissetTimer(time);

//设置时间

}

public void setTimer(JLabel time){

//设置时间方法

final JLabel varTime = time;

//传递组件

Timer timeAction = new Timer(1000, new ActionListener() {

//时间监听

public void actionPerformed(ActionEvent e) {

long timemillis = SystemcurrentTimeMillis();//得到系统时间

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//转换日期显示格式

varTimesetText(dfformat(new Date(timemillis)));//输出得到的时间

}

});

timeActionstart(); //开启线程

}

}

import javaxswingJTextField;

import javaappletApplet;

import javaawtButton;

import javaawteventActionListener;

import javaawteventActionEvent;

import javatextSimpleDateFormat;

import javautilDate;

public class Showtime extends Applet {

private JTextField textField;

public Showtime() {

thissetLayout(null);

textField = new JTextField();

textFieldsetBounds(32, 58, 122, 21);

thisadd(textField);

textFieldsetColumns(10);

Button button = new Button("\u663E\u793A\u65F6\u95F4");

buttonaddActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

Date date = new Date();

SimpleDateFormat sdf = new SimpleDateFormat(

"yyyy-MM-dd HH:mm:ss");

String time = sdfformat(date);

textFieldsetText(time);

}

});

buttonsetBounds(52, 112, 76, 23);

thisadd(button);

}

}

import javautil;

import javaawt;

import javaapplet;

import javatext;

import javaawtevent;

public class Alarm extends Applet implements Runnable

{

Thread timer=null; //创建线程timer

Image gif1; //clockp:闹钟的外壳,闹铃和报时物

boolean setflag=false,stopflag=false,cancelflag=false;

Panel setpanel;

//获取声音文件

AudioClip ring=getAudioClip(getCodeBase(), "1mid");

Button setbutton=new Button("SET");

Button cancelbutton=new Button("CANCEL");

Button stopbutton=new Button("STOP");

//响应按钮事件

private ActionListener setli=new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

setflag=true;

}

};

private ActionListener cancelli=new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

setflag=true;

}

};

private ActionListener stopli=new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

ringstop();

//清除的方法

//gclearRect(83,280,20,30);

}

};

Label note1=new Label("Alarm clock:");

//GregorianCalendar提供的是一个日历式的东东,上面又多了很多的参数,是方便 *** 作了不少。而Date类的功能远不及其,求个和日期有联系的还要自己计算。

GregorianCalendar cal=new GregorianCalendar();

GregorianCalendar cal2=new GregorianCalendar();

SimpleDateFormat df=new SimpleDateFormat("yyyy MM dd HH:mm:ss");//设置时间格式

Date dummy=new Date(); //生成Data对象

String lastdate=dfformat(dummy);

Font F=new Font("TimesRoman",FontPLAIN,14);//设置字体格式

Date dat=null;

Date timeNow;

Color fgcol=Colorblue;

Color fgcol2=ColordarkGray;

Color backcolor=Colorblue;

Label hlabel2,mlabel2,slabel2;//显示时间单位时所用的标签(时、分、秒)

int i;

int s,m,h;

TextField sethour,setmin,setsec;//显示当前时间文本框和定时文本框

//在Applet程序中,首先自动调用初始化完成必要的初始化工作,紧接着自动调用start,在进入执行程序和返回到该页面时被调用,而从该页面转到别的页面时,stop被调用,关闭浏览器时,执行destroy。

public void init()//初始化方法

{

int fieldx=50,fieldy1=120,fieldy2=220,fieldw=30,fieldh=20,space=50;//显示时间和定时文本框的定位参数

setLayout(null); //将布局管理器初始化为null

setpanel=new Panel();

setpanelsetLayout(null);

setpaneladd(note1);

note1setBounds(30,100,60,20);

note1setBackground(backcolor);

note1setForeground(Colorblack);

//定时用的文本框(时、分、秒)

sethour=new TextField("00",5);

setmin=new TextField("00",5);

setsec=new TextField("00",5);

hlabel2=new Label();

mlabel2=new Label();

slabel2=new Label();

//定时的小时文本框的位置、大小

setpaneladd(sethour);

sethoursetBounds(fieldx,fieldy2,fieldw,fieldh);

sethoursetBackground(Colorwhite);

//在文本框后加入单位“时”

setpaneladd(hlabel2);

hlabel2setText("h");

hlabel2setBackground(backcolor);

hlabel2setForeground(Colorblack);

hlabel2setBounds(fieldx+fieldw+3,fieldy2,14,20);

fieldx=fieldx+space;

//定时的分钟文本框的位置、大小

setpaneladd(setmin);

setminsetBounds(fieldx,fieldy2,fieldw,fieldh);

setminsetBackground(Colorwhite);

//在文本框后加入单位“分”

setpaneladd(mlabel2);

mlabel2setText("m");

mlabel2setBackground(backcolor);

mlabel2setForeground(Colorblack);

mlabel2setBounds(fieldx+fieldw+3,fieldy2,14,20);

fieldx=fieldx+space;

//定时的秒文本框的位置、大小

setpaneladd(setsec);

setsecsetBounds(fieldx,fieldy2,fieldw,fieldh);

setsecsetBackground(Colorwhite);

//在文本框后加入单位“秒”

setpaneladd(slabel2);

slabel2setText("s");

slabel2setBackground(backcolor);

slabel2setForeground(Colorblack);

slabel2setBounds(fieldx+fieldw+3,fieldy2,14,20);

//设置闹钟控制按钮(on,off)

setpaneladd(cancelbutton);

setpaneladd(setbutton);

setpaneladd(stopbutton);

cancelbuttonsetBounds(90,180,40,20);

setbuttonsetBounds(140,180,40,20);

stopbuttonsetBounds(522,180,40,20);

setbuttonaddActionListener(setli);

cancelbuttonaddActionListener(cancelli);

stopbuttonaddActionListener(stopli);

stopbuttonsetVisible(false);

//将面板加入当前容器中,并设置面板的大小和背景色

add(setpanel);

setpanelsetBounds(300,1,250,420);

setpanelsetBackground(backcolor);

/int xcenter,ycenter,s,m,h;

//闹钟中心点所在位置

xcenter=145;

ycenter=162;

s=(int)calget(CalendarSECOND);

m=(int)calget(CalendarMINUTE);

h=(int)calget(CalendarHOUR_OF_DAY);

//初始化指针位置

lastxs=(int)(Mathcos(s314f/30-314f/2)30+xcenter);

lastys=(int)(Mathsin(s314f/30-314f/2)30+ycenter);

lastxm=(int)(Mathcos(m314f/30-314f/2)25+xcenter);

lastym=(int)(Mathsin(m314f/30-314f/2)25+ycenter);

lastxh=(int)(Mathcos((h30+m/2)314f/180-314f/2)18+xcenter);

lastyh=(int)(Mathsin((h30+m/2)314f/180-314f/2)18+ycenter);

lasts=s; /

MediaTracker mt=new MediaTracker(this);//为给定组件创建一个跟踪媒体的MediaTracker对象,把添加到被跟踪的组

//Java允SappletHTML所在的位置(decument base)下dY料,也允Sapplet钠涑淌酱a所在的位置(code base)下dY料。藉由呼叫getDocumentBase()cgotCodeBase()可得到URL物件。@些函湍阏业侥阆胂螺d的n案的位置

//clockp=getImage(getDocumentBase(),"11png");

gif1=getImage(getCodeBase(),"2gif");

//i为id号

mtaddImage(gif1,i++);

try

{

mtwaitForAll();

}

catch(InterruptedException e)

{};//等待加载结束

resize(600,420);//设置窗口大小

}

//窗口显示有改变的时候调用paint

public void paint(Graphics g)

{//重写paint()方法

int xh,yh,xm,ym,xs,ys,strike_times;

int xcenter,ycenter;

String today;

xcenter=148;

ycenter=186;

dat=new Date();

//用当前时间初始化日历时间

calsetTime(dat);

//读取当前时间

s=(int)calget(CalendarSECOND);

m=(int)calget(CalendarMINUTE);

h=(int)calget(CalendarHOUR_OF_DAY);

//换一种时间表达形式

today=dfformat(dat);

//指针位置

xs=(int)(Mathcos(s314f/30-314f/2)30+xcenter);

ys=(int)(Mathsin(s314f/30-314f/2)30+ycenter);

xm=(int)(Mathcos(m314f/30-314f/2)25+xcenter);

ym=(int)(Mathsin(m314f/30-314f/2)25+ycenter);

xh=(int)(Mathcos((h30+m/2)314f/180-314f/2)12+xcenter);

yh=(int)(Mathsin((h30+m/2)314f/180-314f/2)12+ycenter);

//设置字体和颜色

gsetFont(F);

//前景色

gsetColor(getBackground()); //取背景色的

gdrawImage(gif1,75,110,this);

//以数字方式显示年、月、日和时间

gdrawString(today,55,415);

//画指针

gdrawLine(xcenter,ycenter,xs,ys);

gdrawLine(xcenter,ycenter-1,xm,ym); //(x1,y1,x2,y2)

gdrawLine(xcenter-1,ycenter,xm,ym);

gdrawLine(xcenter,ycenter-1,xh,yh);

gdrawLine(xcenter-1,ycenter,xh,yh);

int timedelta;//记录当前时间与闹铃定时的时差

Integer currh,currm,currs;//分别记录当前的时、分、秒

Date dat2=new Date();

cal2setTime(dat2);

//读取当前时间

currh=(int)cal2get(CalendarSECOND);

currm=(int)cal2get(CalendarMINUTE);

currs=(int)cal2get(CalendarHOUR_OF_DAY);

//这样做的话说我API已过时

//timeNow=new Date();

//currh=new Integer(timeNowgetHours());

//currm=new Integer(timeNowgetMinutes());

//currs=new Integer(timeNowgetSeconds());

if(setflag)

{ //判断是否设置了闹钟

//判断当前时间是否为闹钟所定的时间

if((currhintValue()==IntegervalueOf(sethourgetText())intValue())&&(currmintValue()==IntegervalueOf(setmingetText())intValue())&&(currsintValue()==IntegervalueOf(setsecgetText())intValue()))

{

ringplay();

gdrawImage(gif1,83,280,this);

stopbuttonsetVisible(true);

}

timedelta=currmintValue()60+currsintValue()-IntegervalueOf(setmingetText())intValue()60-IntegervalueOf(setsecgetText())intValue();

if((timedelta>=30))

{

//若当前时间与闹钟相差时间超过30秒,闹钟自动停

ringstop();

//清除的方法

gclearRect(83,280,20,30);

}

}

dat=null;

}

public void start()

{

if(timer==null)

{

timer=new Thread(this);//将timer实例化

timerstart();

}

}

public void stop()

{

timer=null;

}

//给创建线程后start之后自动执行的函数

public void run()

{

//在run()方法中,调用repaint()方法,以重绘小程序区,进行时钟显示的更新。接着调用sleep方法让当前线程(也就是我们创建的线程clockthread)睡眠1000毫秒,因为我们每秒钟要更新一下显示,所以让它睡眠1秒

while(timer!=null)

{

try

{

timersleep(1000);

}

catch(InterruptedException e)

{}

//调用repaint时,会首先清除掉paint方法之前的画的内容,再调用paint方法

repaint();//刷新画面

}

timer=null;

}

//当AWT接收到一个applet的重绘请求时,它就调用applet的 update(),默认地,update() 清除applet的背景,然后调用 paint()。重载 update(),将以前在paint()中的绘图代码包含在update()中,从而避免每次重绘时将整个区域清除

//有两种方法可以明显地减弱闪烁:重载 update()或使用双缓冲。

//使用双缓冲技术:另一种减小帧之间闪烁的方法是使用双缓冲,它在许多动画Applet中被使用。其主要原理是创建一个后台图像,将需要绘制的一帧画入图像,然后调用DrawImage()将整个图像一次画到屏幕上去;好处是大部分绘制是离屏的,将离屏图像一次绘至屏幕上比直接在屏幕上绘制要有效得多,大大提高做图的性能。

// 双缓冲可以使动画平滑,但有一个缺点,要分配一张后台图像,如果图像相当大,这将需要很大一块内存;当你使用双缓冲技术时,应重载 update()。

public void update(Graphics g)

{

Image offscreen_buf=null;

//采用双缓冲技术的update()方法

if(offscreen_buf==null)

offscreen_buf=createImage(600,420);

Graphics offg=offscreen_bufgetGraphics();

offgclipRect(1,1,599,419);

paint(offg);

Graphics ong=getGraphics();

ongclipRect(1,1,599,419);

ongdrawImage(offscreen_buf,0,0,this);

}

/ Creates a new instance of AlarmClock /

}

以上就是关于java 窗口 倒计时 关闭全部的内容,包括:java 窗口 倒计时 关闭、你好,你有用java编写的聊天室的代码吗,基于c/s架构的、编写一个Applet小程序,显示一个文本框和一个按钮,单击按钮,在文本框内显示当前时间。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9389985.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存