新聞中心
求java的計(jì)時(shí)器代碼,應(yīng)該比較簡單的,來看看吧。
package?test;
成都創(chuàng)新互聯(lián)專注于遼中網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供遼中營銷型網(wǎng)站建設(shè),遼中網(wǎng)站制作、遼中網(wǎng)頁設(shè)計(jì)、遼中網(wǎng)站官網(wǎng)定制、微信小程序定制開發(fā)服務(wù),打造遼中網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供遼中網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
import?java.util.*;
import?java.awt.*;
import?java.awt.event.*;
import?java.applet.*;
public?class?Test5?extends?Applet?{
private?final?Panel?pan?=?new?Panel();
private?final?Label?time?=?new?Label();
private?final?Button?btnGo?=?new?Button("開始");
private?final?Button?btnPouse?=?new?Button("暫停");
private?final?Button?btnReset?=?new?Button("復(fù)位");
private?final?StopwatchThread?swThread?=?new?StopwatchThread();
private?class?btnGoListener?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
???
swThread.go();
btnGo.setEnabled(false);
}
}
private?class?btnPouseListener?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
???if(btnGo.isEnabled()){
???return?;
???}
?if?(btnPouse.getLabel().equals("繼續(xù)"))?{
swThread.go();
btnPouse.setLabel("暫停");
}?else?if?(btnPouse.getLabel().equals("暫停"))?{
swThread.noGo();
btnPouse.setLabel("繼續(xù)");
}
}
}
private?class?btnResetListener?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
swThread.reset();
btnGo.setEnabled(true);
btnGo.setLabel("開始");
btnPouse.setLabel("暫停");
}
}
private?class?StopwatchThread?extends?Thread?{
private?boolean?going?=?false;
private?long?prevElapsed?=?0;
private?Date?startDate?=?new?Date();
private?long?elapsedTime()?{
return?prevElapsed?+
(going???new?Date().getTime()?-?startDate.getTime()?:?0);
}
private?String?msToString(long?time)?{
???System.out.println(time+"??"+((0*60+2)*1000+999));
if(((99*60+59)*1000+983)=time((99*60+59)*1000+999)=time){//((0*60+2)*1000+983)=time((0*60+2)*1000+999)=time
if?(time?%?1000??990)
time?+=?2;
swThread.noGo();
}
String?ms,?sec,?min;
if?(time?%?10?=?5)
time?+=?5;
ms?=?Long.toString(time?%?1000);
while?(ms.length()??3)
ms?=?"0"?+?ms;
ms?=?ms.substring(0,?ms.length()?-?1);
time?/=?1000;
sec?=?Long.toString(time?%?60);
if?(sec.length()?==?1)?sec?=?"0"?+?sec;
time?/=?60;
min?=?Long.toString(time);
return?min?+?":"?+?sec?+?"."?+?ms;
}
public?void?go()?{
startDate?=?new?Date();
going?=?true;
}
public?void?noGo()?{
prevElapsed?=?elapsedTime();
going?=?false;
}
public?void?reset()?{
going?=?false;
prevElapsed?=?0;
}
public?void?run()?{
while?(true)?{
time.setText(msToString(elapsedTime()));
yield();
}
}
}
public?void?init()?{
setLayout(new?GridLayout(2,2));
setBackground(Color.lightGray);
setForeground(Color.black);
pan.setLayout(new?GridLayout(3,2));
pan.add(new?Label("計(jì)時(shí):"));
time.setForeground(Color.blue);
pan.add(time);
pan.add(btnGo);
pan.add(btnPouse);
pan.add(btnReset);
pan.add(new?Label());
add(pan);
btnGo.addActionListener(new?btnGoListener());
btnReset.addActionListener(new?btnResetListener());
btnPouse.addActionListener(new?btnPouseListener());
swThread.setDaemon(true);
swThread.start();
}
public?static?void?main(String[]?args)?{
Test5?applet?=?new?Test5();
Frame?aFrame?=?new?Frame("計(jì)時(shí)器");
aFrame.addWindowListener(new?WindowAdapter()?{
public?void?windowClosing(WindowEvent?e)?{
System.exit(0);
}
});
aFrame.add(applet,?BorderLayout.CENTER);
aFrame.setSize(400,?200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
}
可以改變有注釋的那個(gè)if語句里面的值來判斷什么時(shí)候停止
用java編寫一個(gè)倒計(jì)時(shí)器代碼。
import java.awt.BorderLayout;import java.awt.Container;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class TimerDemo extends JFrame implements ActionListener { private static final long serialVersionUID = 201306211111L; private JTextField screen = new JTextField("0"); private JButton start = new JButton("開始"); private JButton reset = new JButton("重置"); private JPanel panel = new JPanel(); private boolean isRunning; private int time; private int timeBetween; public TimerDemo(int timeBetween) { super("計(jì)時(shí)器"); this.timeBetween = timeBetween; try { init(); } catch (Exception e) { e.printStackTrace(); } } public TimerDemo() { super("計(jì)時(shí)器"); this.timeBetween = 100; try { init(); } catch (Exception e) { e.printStackTrace(); } } private void init() { panel.setLayout(new GridLayout()); panel.add(start); panel.add(reset); start.addActionListener(this); reset.addActionListener(this); screen.setFont(new Font("幼圓", Font.BOLD, 60)); screen.setHorizontalAlignment(JTextField.CENTER); screen.setEditable(false); Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(panel, BorderLayout.SOUTH); c.add(screen, BorderLayout.CENTER); this.setSize(200, 150); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args) { new TimerDemo(1);// 設(shè)定 1ms/次 // new TimerDemo(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == start) { if (start.getText().equals("開始")) { start.setText("暫停"); isRunning = true; } else if (start.getText().equals("暫停")) { start.setText("開始"); isRunning = false; } } if (e.getSource() == reset) { start.setText("開始"); screen.setText("0"); isRunning = false; time = 0; } new Thread(new TimeZone()).start(); } class TimeZone implements Runnable { @Override public void run() { while (isRunning) { time++; if (time = Integer.MAX_VALUE) { screen.setText("ERROR"); JOptionPane.showMessageDialog(null, "ERROR"); isRunning = false; } screen.setText(String.valueOf(time)); try { Thread.sleep(timeBetween); } catch (Exception e) { e.printStackTrace(); } } } }}
求人用java編寫一條計(jì)時(shí)器代碼。
import?java.awt.BorderLayout;
import?java.awt.Container;
import?java.awt.GridLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.awt.event.WindowEvent;
import?java.awt.event.WindowListener;
import?java.io.*;
import?java.util.*;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JOptionPane;
import?javax.swing.JPanel;
import?javax.swing.JTextField;
import?javax.swing.plaf.OptionPaneUI;
public?class?Demo?{
static?boolean?isRuning=false;
static?boolean?isFirst=true;
@SuppressWarnings("unchecked")
public?static?void?main(String[]?args)?throws?Exception?{
JFrame?form1?=?new?JFrame("Form1");
JTextField?jTextField?=?new?JTextField(10);
jTextField.setSize(10,?10);
jTextField.setText("0");
jTextField.setEditable(false);
JButton?jButton?=?new?JButton("開始");
jButton.setSize(10,?10);
Thread?thread?=?new?Thread(new?Runnable()?{
@Override
public?void?run()?{
while?(true)?{
while(isRuning){
Integer?counter?=?Integer.parseInt(jTextField.getText().trim());
counter++;
jTextField.setText(counter.toString());
try?{
Thread.sleep(1000);
}?catch?(Exception?e2)?{
}
}
}
}
});
jButton.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
???String?text=jButton.getText().equals("開始")?"暫停":"開始";
???jButton.setText(text);
???isRuning=!isRuning;
???if(isFirst){
???thread.start();
???isFirst=false;
???}
}
});
JPanel?panel?=?new?JPanel();
panel.setSize(200,?200);
panel.add(jTextField,?BorderLayout.NORTH);
panel.add(jButton,?BorderLayout.CENTER);
form1.add(panel);
form1.setBounds(200,?100,?250,?150);
form1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form1.addWindowListener(new?WindowListener()?{
@Override
public?void?windowOpened(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowIconified(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowDeiconified(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowDeactivated(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowClosing(WindowEvent?e)?{
//?窗口關(guān)閉前取出文本框的數(shù)字保存到外部文件,代碼在此處寫
JOptionPane.showMessageDialog(null,?"Are?you?sure?closing?");
}
@Override
public?void?windowClosed(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowActivated(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
});
form1.setVisible(true);
}
}
用JAVA編寫計(jì)時(shí)器
計(jì)時(shí)器可以使用timer類也可以使用線程類來操作,下面是Thread做的簡單的計(jì)時(shí)器
public?class?Calculagraph?extends?Thread?{
public?static?void?main(String[]?args)?{
new?Calculagraph().start();
}
private?long?now?=?0l;
private?long?start?=?System.currentTimeMillis();//?程序啟動(dòng)時(shí)間的毫秒值
private?long?time;
public?void?run()?{
while?(true)?{
now?=?System.currentTimeMillis();//?獲取一秒之后的毫秒值
time?=?now?-?start;//?兩個(gè)時(shí)間相減的到毫秒差
System.out.format("%02d:%02d:%02d\n",
time?/?(1000?*?60?*?60)?%?60/*?時(shí)?*/,?
time?/?(1000?*?60)%?60/*?分?*/,?
time?/?1000?%?60/*?秒?*/);//?格式化字符串輸出
try?{
Thread.sleep(1000);
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}
}
}
}
網(wǎng)頁名稱:java計(jì)時(shí)工具代碼 javafx計(jì)時(shí)器
當(dāng)前地址:http://www.ef60e0e.cn/article/dojeioi.html