求java高手解决一下以下javaSE程序问题 保证马上给分
我下面是做的是一个秒表程序为什么我能按开始键计开始计时按暂停键能暂停却不能(再一次按暂停键)继续计时?packagea.b;publicclassWextendsJFra...
我下面是做的是一个秒表程序 为什么我能按开始键计开始计时 按暂停键能暂停却不能(再一次按暂停键)继续计时?
package a.b;
public class W extends JFrame implements Runnable{
int j=0;
JFrame jf = new JFrame("计时器");
JButton jb1 = new JButton("开始");
JButton jb2 = new JButton("暂停或继续");
JTextField jt = new JTextField(18);
Calendar c = Calendar.getInstance();
Date d;
String s;
SimpleDateFormat ss;
Thread th =new Thread(W.this);;
public W() {
jf.setVisible(true);
jf.setBounds(100, 100, 250, 250);
jf.setLayout(new FlowLayout());
jf.setDefaultCloseOperation(3);
jf.add(jt);
jf.add(jb1);
jf.add(jb2);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
d=c.getTime();
ss=new SimpleDateFormat("HH:mm:ss");
s=ss.format(d);
jt.setText(s);
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
d=ss.parse( s);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
th.start();
System.out.println(W.this.th);
}
});
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
++j;
}
});
}
public void run(){
int i=0;
while(j%2==0){
c.setTime(d);
c.add(Calendar.SECOND,++i);
Date dd=c.getTime();
jt.setText(ss.format(dd));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
public static void main(String[] args) {
new W();
}
} 展开
package a.b;
public class W extends JFrame implements Runnable{
int j=0;
JFrame jf = new JFrame("计时器");
JButton jb1 = new JButton("开始");
JButton jb2 = new JButton("暂停或继续");
JTextField jt = new JTextField(18);
Calendar c = Calendar.getInstance();
Date d;
String s;
SimpleDateFormat ss;
Thread th =new Thread(W.this);;
public W() {
jf.setVisible(true);
jf.setBounds(100, 100, 250, 250);
jf.setLayout(new FlowLayout());
jf.setDefaultCloseOperation(3);
jf.add(jt);
jf.add(jb1);
jf.add(jb2);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
d=c.getTime();
ss=new SimpleDateFormat("HH:mm:ss");
s=ss.format(d);
jt.setText(s);
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
d=ss.parse( s);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
th.start();
System.out.println(W.this.th);
}
});
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
++j;
}
});
}
public void run(){
int i=0;
while(j%2==0){
c.setTime(d);
c.add(Calendar.SECOND,++i);
Date dd=c.getTime();
jt.setText(ss.format(dd));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
public static void main(String[] args) {
new W();
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询