while(true)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("now:"
+ new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
.format(new Date()));
}上面是动态时间
auto=new Button("显示上面的时间");
auto.addActionListener(this);
add(auto,BorderLayout.NORTH);我用的是先创建按钮
然后在对按钮上面的值赋为时间
失败了....
大家指点下
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("now:"
+ new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
.format(new Date()));
}上面是动态时间
auto=new Button("显示上面的时间");
auto.addActionListener(this);
add(auto,BorderLayout.NORTH);我用的是先创建按钮
然后在对按钮上面的值赋为时间
失败了....
大家指点下
{
public void run()
{
while(true)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String text = "now:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
btn.setText(text);
}}
};
t.start();
这样不行吗?
用这也相当于是个死循环
程序就只执行这了
下面的代码就不执行了....
有什么更好的办法?
那是另外的问题,你的问题要求动态设值。需要退出就设置一个判断条件撒~给个例子你看看吧。import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;public class InsertKeyDialog extends JDialog
{ int time = 10; public InsertKeyDialog()
{
super(TKLApp.getApplication().getMainFrame(), "请插入USB-KEY", true);
setSize(300, 100);
setLocationRelativeTo(getParent());
final JLabel lblTip = new JLabel("请插入USB-KEY,否则10秒钟后系统将退出。");
lblTip.setHorizontalAlignment(JLabel.CENTER);
final JLabel lblTime = new JLabel();
lblTime.setForeground(Color.red);
lblTime.setHorizontalAlignment(JLabel.CENTER);
lblTime.setFont(new Font("", Font.BOLD, 20));
final JPanel pnl = new JPanel(new GridLayout(2, 1, 2, 2));
pnl.add(lblTip);
pnl.add(lblTime);
setContentPane(pnl); Thread t = new Thread()
{
public void run()
{
while (true)
{
if (time == 0)
{
InsertKeyDialog.this.setVisible(false);
}
lblTime.setText(String.valueOf(time));
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
time--;
}
}
};
t.start();
} // protected void processWindowEvent(WindowEvent evt)
// {
// if (evt.getID() == 201)
// {
// return;
// }
// }
}
public class BalanceTimer{
String url = "jdbc:odbc:NetBarDataSource";
float NowBalance = 0;
float Price = 0;
int warning = 0;//发出余额不足警告的次数
public final static int ONE_MINUTE = 60000;
public BalanceTimer(final JFrame jframe,final String RecordID,final String ClientID,
final String ClientName,final String ComputerID,
final String BeginTime,final String BeginBalance){
//为计时器添加监听器,每分钟执行一次
Timer timer = new Timer(ONE_MINUTE, new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try {
//每分钟更新一次Balance
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// 创建指定数据库的URL,StuInfo是建立的ODBC数据源
Connection con1 = DriverManager.getConnection(url, "sa",
"");
// 建立连接,url是前面的数据源地址,sa是用户名,密码为空
PreparedStatement stmt1 = con1.prepareStatement("select Balance,Price " +
"from Client,Rank where ID =? and Client.Rank = Rank.Rank");
stmt1.setString(1, ClientID);
ResultSet rs1 = stmt1.executeQuery();
if(rs1.next()){
NowBalance = Float.parseFloat(rs1.getString("Balance"));
Price = Float.parseFloat(rs1.getString("Price"));
}//获取上机前的Balance
Connection con2 = DriverManager.getConnection(url, "sa","");
NowBalance = NowBalance - Price;//每分钟扣掉price的金额
//用四舍五入处理Balance
BigDecimal bd = new BigDecimal(NowBalance);
NowBalance = (float) bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
PreparedStatement stmt2 = con2.prepareStatement("update Client " +
"set Balance = ? where ID = ?");//更新数据库中的Balance
stmt2.setFloat(1, NowBalance);
stmt2.setString(2, ClientID);
stmt2.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
//每分钟判断一次Balance是否充足,如果不足会给出警告,但是只警告一次
if((NowBalance <= 0.5)&&(warning == 0)){
if(jframe != null && jframe.getState() == Frame.ICONIFIED)
{//如果消息框依附的框架不为空,并且该框架状态为图标化(最小化)
jframe.setState(Frame.NORMAL);
//设消息框依附的框架为正常状态(否则消息出现的地方是左上角,不好看)
}
JOptionPane.showMessageDialog(jframe, "余额不足,请速到到前台充值,以免影响使用",
"系统提示", JOptionPane.WARNING_MESSAGE);
warning = 1;//警告过一次后,将警告次数置为1,则不再发出警告
}
if(NowBalance <= 0){//余额为零时,强制退出系统,不需给出整个上机信息
SimpleDateFormat HMFromat = new SimpleDateFormat("yyyy/MM/dd/HH:mm");//时间输出的格式
String EndTime = new String(HMFromat.format(new Date()));
// //在强制下机的时候,获取当前时间为EndTime即可
// //计算Fee
float Fee = NowBalance - Float.parseFloat(BeginBalance);
BigDecimal bd = new BigDecimal(NowBalance);
Fee = (float) bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//更新日营业额
new UpdateDayTurnover(Fee);
//更新月营业额
new UpdateMonthTurnover(Fee);
//生成用户使用计算机记录
new Record(RecordID,ClientID,ClientName,ComputerID,BeginTime,EndTime,Fee);
//改变用户登录状态和计算机使用状态为false
new ChangeState(false,ClientID,ComputerID);
System.exit(0);//退出系统
}
}
});
timer.start();//计时器监听器便写完后,立刻启动计时器
}
}
public class Clock {
JFrame jframe = new JFrame("clock");
JButton jbutton = new JButton();
public Clock(){
jframe.setLocation(500, 200);
jframe.setSize(300, 100);
jframe.add(jbutton);
Timer timer = new Timer(1000,new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String time = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
jbutton.setText("Now:"+time);
}
});
timer.start();
jframe.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Clock();
}}