用java编一个简单的倒计时表
他倒计时1分钟,但是显示的是几小时几分几秒界面中有三部分一个开始一个暂停。按暂停停止安开始又继续麻烦高手们了。。。谢谢要全面的。。...
他倒计时1分钟,但是显示的是几小时几分几秒 界面中有三部分 一个开始一个暂停。按暂停停止 安开始又继续 麻烦高手们了。。。谢谢 要全面的。。
展开
2个回答
展开全部
先不要关闭问题,给我点时间我编出来,我也想断炼一下。
代码如下:
我这程序有点问题 ,这倒计时你讲的功能都有了,但那个暂停按钮有问题,只能用两次,我怎么也找不出原因, 我想是多线程方面的问题吧,按两下那暂停按钮就失去作用了。
--------------------------MainFrame.java------------------------------------
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MainFrame extends Frame{
Dispose dp= null;
int flag=1; //暂停开始的按钮,1为计时中,0为暂停。
Button button=null;
Label l1 =null;
Label l2 =null;
Label l3 = null;
TextField tfh =null;
TextField tfm =null;
TextField tfs =null;
public void lanchFrame(){
this.setLocation(200,200);
this.setSize(200,200);
this.setLayout(new FlowLayout());
l1=new Label("hour");
tfh = new TextField("1",6);
l2=new Label("minute");
tfm = new TextField("3",6);
l3=new Label("second");
tfs = new TextField("5",6);
button = new Button("stop");
this.add(l1);
this.add(tfh);
this.add(l2);
this.add(tfm);
this.add(l3);
this.add(tfs);
this.add(button);
button.addActionListener(new StartAndStopListener(this));
this.addWindowListener(new MyClosingListener());
this.dp = new Dispose(this);
this.pack();
this.setVisible(true);
}
public static void main(String args[]){
MainFrame mf=new MainFrame();
mf.lanchFrame();
mf.dp.run(mf);
}
private class MyClosingListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
}
--------------------------Dispose.java------------------------------------
public class Dispose {
int hour;
int minute;
int second;
public Dispose(MainFrame mf) {
this.hour = Integer.parseInt(mf.tfh.getText());
this.minute = Integer.parseInt(mf.tfm.getText());
this.second = Integer.parseInt(mf.tfs.getText());
}
public void run(MainFrame mf) {
while (!(hour == 0 && minute == 0 && second == 0) && mf.flag == 1) {
if (second == 0) {
if (minute > 0) {
second = 59;
minute--;
}
}
if (minute == 0) {
if (hour > 0) {
minute = 59;
hour--;
}
}
second--;
mf.tfs.setText(second + "");
mf.tfm.setText(minute + "");
mf.tfh.setText(hour + "");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
------------------------------StarAndStopListener.java-----------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StartAndStopListener implements ActionListener {
MainFrame mf = null;
public StartAndStopListener(MainFrame mf){
this.mf = mf;
}
public void actionPerformed(ActionEvent arg0) {
if(mf.flag==0){
mf.flag =1;
mf.button.setLabel("stop");
mf.dp.run(mf);
}
if(mf.flag == 1){
mf.flag = 0;
mf.button.setLabel("start");
}
}
}
代码如下:
我这程序有点问题 ,这倒计时你讲的功能都有了,但那个暂停按钮有问题,只能用两次,我怎么也找不出原因, 我想是多线程方面的问题吧,按两下那暂停按钮就失去作用了。
--------------------------MainFrame.java------------------------------------
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MainFrame extends Frame{
Dispose dp= null;
int flag=1; //暂停开始的按钮,1为计时中,0为暂停。
Button button=null;
Label l1 =null;
Label l2 =null;
Label l3 = null;
TextField tfh =null;
TextField tfm =null;
TextField tfs =null;
public void lanchFrame(){
this.setLocation(200,200);
this.setSize(200,200);
this.setLayout(new FlowLayout());
l1=new Label("hour");
tfh = new TextField("1",6);
l2=new Label("minute");
tfm = new TextField("3",6);
l3=new Label("second");
tfs = new TextField("5",6);
button = new Button("stop");
this.add(l1);
this.add(tfh);
this.add(l2);
this.add(tfm);
this.add(l3);
this.add(tfs);
this.add(button);
button.addActionListener(new StartAndStopListener(this));
this.addWindowListener(new MyClosingListener());
this.dp = new Dispose(this);
this.pack();
this.setVisible(true);
}
public static void main(String args[]){
MainFrame mf=new MainFrame();
mf.lanchFrame();
mf.dp.run(mf);
}
private class MyClosingListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
}
--------------------------Dispose.java------------------------------------
public class Dispose {
int hour;
int minute;
int second;
public Dispose(MainFrame mf) {
this.hour = Integer.parseInt(mf.tfh.getText());
this.minute = Integer.parseInt(mf.tfm.getText());
this.second = Integer.parseInt(mf.tfs.getText());
}
public void run(MainFrame mf) {
while (!(hour == 0 && minute == 0 && second == 0) && mf.flag == 1) {
if (second == 0) {
if (minute > 0) {
second = 59;
minute--;
}
}
if (minute == 0) {
if (hour > 0) {
minute = 59;
hour--;
}
}
second--;
mf.tfs.setText(second + "");
mf.tfm.setText(minute + "");
mf.tfh.setText(hour + "");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
------------------------------StarAndStopListener.java-----------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StartAndStopListener implements ActionListener {
MainFrame mf = null;
public StartAndStopListener(MainFrame mf){
this.mf = mf;
}
public void actionPerformed(ActionEvent arg0) {
if(mf.flag==0){
mf.flag =1;
mf.button.setLabel("stop");
mf.dp.run(mf);
}
if(mf.flag == 1){
mf.flag = 0;
mf.button.setLabel("start");
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询