给你一个timer运用的例子,这个例子一运行会弹出一个msgbox,如果你不点它,那么每隔一秒钟会打印出一句话来.我想根据这个,你适当的改一下,就可以改成你需要的了 你可以在你的窗体里加入一个按钮,然后这个按钮该做些什么,你应该知道怎么弄了吧 这样的话,就实现了你要的功能 import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.Timer; // to resolve conflict with java.util.Timerpublic class timer { public static void main(String[] args) { ActionListener listener = new TimePrinter(); // construct a timer that calls the listener // once every 1 seconds Timer t = new Timer(1000, listener); t.start(); JOptionPane.showMessageDialog(null, "Quit program?"); System.exit(0); } }class TimePrinter implements ActionListener { public void actionPerformed(ActionEvent event) { Date now = new Date(); System.out.println("At the tone, the time is " + now); Toolkit.getDefaultToolkit().beep(); } }
<META HTTP-EQUIV=REFRESH CONTENT='3;URL=http://www.lp622.com.cn/'></head>
<body bgcolor=017ba9 text=FFFFEE>
<center><h2>欢迎有空再来。<hr width=400 size=1>You have already logged out from chat room</h2><br></center>
</body></html>上面是3秒中后自动转移,不知道是不是你要的。
Thread.sleep(30000);
if(!isPressed){
frame2.setVisible(true);//开另一个窗口
this.dispose();//关闭当前窗口
}
如果点了按钮,就在按钮事件中把isPressed设为true。
。
private Timer timer = new Timer(30000, this);
private JButton button;
......
public yourFrame () { show();
timer.start();
} public void actionPerformed(ActionEvent e ) { if( e.getSource() == button ) {
timer.stop();
}else
if( e.getSource() == timer) {
anotherFrame.show() ; // or toFront() according to your logic
} } }
你可以在你的窗体里加入一个按钮,然后这个按钮该做些什么,你应该知道怎么弄了吧
这样的话,就实现了你要的功能
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
// to resolve conflict with java.util.Timerpublic class timer
{
public static void main(String[] args)
{
ActionListener listener = new TimePrinter(); // construct a timer that calls the listener
// once every 1 seconds
Timer t = new Timer(1000, listener);
t.start(); JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
}class TimePrinter implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Date now = new Date();
System.out.println("At the tone, the time is " + now);
Toolkit.getDefaultToolkit().beep();
}
}