Java图形界面中在text中显示计时,没反应,多久都是00:00:00 5
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDrawingextends...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Drawing extends Frame{
private int hours;
private int rate;
private double tax;
private int balance=1000;
private int paytax;
private Double charge = null;
private long startpoint;
private static Thread t;
private boolean Switch = true;
private long timeuse;
private final int ThreadSleepTime = 1000;
private StopTime stoptime = null;
JFrame JF = null;
JTextField tf = null;
JTextField th = null;
JButton btn1 = null;
JButton btn2 = null;
Drawing ()
{
setTitle("Client Current Balance");
setVisible(true);
setSize (300,200);
setResizable(false);
setMenuBar( new MenuUp());
setLayout(null);
addWindowListener( new WindowAdapter(){
public void windowClosing (WindowEvent e)
{
System.exit(0);}}
);
Button btn1,btn2,btn3;
btn1=new Button("开始");
btn1.setBounds(20, 150, 50, 30);
btn1.addActionListener(new StartTime());
add(btn1);
btn2=new Button("结束");
btn2.setBounds(120, 150, 50, 30);
btn2.addActionListener(new StopTime());
add(btn2);
btn3=new Button("充值");
btn3.setBounds(220, 150, 50, 30);
btn3.addActionListener(new StopTime());
add(btn3);
Label L1,L2;
TextField tf,th;
L1=new Label("已用时间:");
L1.setBounds(10, 50, 70, 20);
tf=new TextField(String.format("%02d:%02d:%02d", 0,0,0));
tf.setBounds(90, 50, 150, 20);
tf.addActionListener(new StartTime());
tf.setEditable(false);
add(L1);
add(tf);
L2=new Label("充值金额:");
L2.setBounds(10, 90, 70, 20);
th=new TextField(8);
th.setBounds(90, 90, 150, 20);
th.addActionListener(new StartTime());
add(L2);
add(th);
}
class MenuUp extends MenuBar{
public MenuUp(){
Menu m1=new Menu("File");
Menu m2=new Menu("Help");
MenuItem mi1=new MenuItem("Members");
MenuItem mi2=new MenuItem("Consumers");
MenuItem mi3=new MenuItem("Quit");
m1.add(mi1);
m1.add(mi2);
m1.addSeparator();
m1.add(mi3);
add(m1);
add(m2);
setHelpMenu(m2);}}
class StartTime implements ActionListener{ //Set action when you press start button
public void actionPerformed(ActionEvent e) {
try {
CheckBalance(balance);
startpoint = System.currentTimeMillis();
t = new Thread(new Action());
Switch = true;
tf.setText(String.format("%02d:%02d:%02d", 0,0,0));
t.start();
btn1.setEnabled(false);
btn2.setEnabled(true);
} catch (MyException e1) {
JOptionPane.showMessageDialog(null, e1.getErrMessage(), "Error", 0);
} catch (Exception e2){
e2.getStackTrace();
}
}
}
class Action implements Runnable{ //Use thread to calculate
public void run() {
double temp;
while(Switch == true){
timeuse = (System.currentTimeMillis() - startpoint) / 1000;
//timeuse += 1;
tf.setText(String.format("%02d:%02d:%02d", timeuse/3600,(timeuse%3600)/60,timeuse%60));
temp = balance - (timeuse*(rate/3600));
if (temp<=0){
stoptime.actionPerformed(null);
}
try {
Thread.sleep(ThreadSleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
界面中不能计时 展开
import java.awt.event.*;
import javax.swing.*;
public class Drawing extends Frame{
private int hours;
private int rate;
private double tax;
private int balance=1000;
private int paytax;
private Double charge = null;
private long startpoint;
private static Thread t;
private boolean Switch = true;
private long timeuse;
private final int ThreadSleepTime = 1000;
private StopTime stoptime = null;
JFrame JF = null;
JTextField tf = null;
JTextField th = null;
JButton btn1 = null;
JButton btn2 = null;
Drawing ()
{
setTitle("Client Current Balance");
setVisible(true);
setSize (300,200);
setResizable(false);
setMenuBar( new MenuUp());
setLayout(null);
addWindowListener( new WindowAdapter(){
public void windowClosing (WindowEvent e)
{
System.exit(0);}}
);
Button btn1,btn2,btn3;
btn1=new Button("开始");
btn1.setBounds(20, 150, 50, 30);
btn1.addActionListener(new StartTime());
add(btn1);
btn2=new Button("结束");
btn2.setBounds(120, 150, 50, 30);
btn2.addActionListener(new StopTime());
add(btn2);
btn3=new Button("充值");
btn3.setBounds(220, 150, 50, 30);
btn3.addActionListener(new StopTime());
add(btn3);
Label L1,L2;
TextField tf,th;
L1=new Label("已用时间:");
L1.setBounds(10, 50, 70, 20);
tf=new TextField(String.format("%02d:%02d:%02d", 0,0,0));
tf.setBounds(90, 50, 150, 20);
tf.addActionListener(new StartTime());
tf.setEditable(false);
add(L1);
add(tf);
L2=new Label("充值金额:");
L2.setBounds(10, 90, 70, 20);
th=new TextField(8);
th.setBounds(90, 90, 150, 20);
th.addActionListener(new StartTime());
add(L2);
add(th);
}
class MenuUp extends MenuBar{
public MenuUp(){
Menu m1=new Menu("File");
Menu m2=new Menu("Help");
MenuItem mi1=new MenuItem("Members");
MenuItem mi2=new MenuItem("Consumers");
MenuItem mi3=new MenuItem("Quit");
m1.add(mi1);
m1.add(mi2);
m1.addSeparator();
m1.add(mi3);
add(m1);
add(m2);
setHelpMenu(m2);}}
class StartTime implements ActionListener{ //Set action when you press start button
public void actionPerformed(ActionEvent e) {
try {
CheckBalance(balance);
startpoint = System.currentTimeMillis();
t = new Thread(new Action());
Switch = true;
tf.setText(String.format("%02d:%02d:%02d", 0,0,0));
t.start();
btn1.setEnabled(false);
btn2.setEnabled(true);
} catch (MyException e1) {
JOptionPane.showMessageDialog(null, e1.getErrMessage(), "Error", 0);
} catch (Exception e2){
e2.getStackTrace();
}
}
}
class Action implements Runnable{ //Use thread to calculate
public void run() {
double temp;
while(Switch == true){
timeuse = (System.currentTimeMillis() - startpoint) / 1000;
//timeuse += 1;
tf.setText(String.format("%02d:%02d:%02d", timeuse/3600,(timeuse%3600)/60,timeuse%60));
temp = balance - (timeuse*(rate/3600));
if (temp<=0){
stoptime.actionPerformed(null);
}
try {
Thread.sleep(ThreadSleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
界面中不能计时 展开
3个回答
展开全部
额 就是这个tf.setText(String.format("%02d:%02d:%02d", timeuse/3600,(timeuse%3600)/60,timeuse%60));出错的啦 你自己研究吧
追问
刚学不久,我也大概知道是这里出错了,但我知道怎么改就不来提问了。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用javax.swing.Timer 类每秒减一,并更新显示。
使用 TimeUnit转换,不要自己去除3600
使用 TimeUnit转换,不要自己去除3600
追问
嗯嗯。谢谢,那要怎么改呢,它就是不能计时,在界面中显示
追答
看这个链接5L的代码,可以参考
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
3600不要自己去除,让系统自己做,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询