该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
做个简单的计时器,只算秒的。按按钮开始,可是按了之后过了我设置的秒数才弹上,这事才显示秒数。
public class t01 extends JFrame {JPanel c;long time2 =0;JLabel jbtime,time;JButton start = new JButton("开始");JButton test = new JButton("ceshi");JPanel MenuPanel = new JPanel();boolean Run = false;BorderLayout borderLayout1 = new BorderLayout();public t01(){setDefaultCloseOperation(EXIT_ON_CLOSE);gui();}public void gui(){ c = (JPanel) getContentPane(); setTitle("计时"); setSize(600, 600); setLayout(new BorderLayout(10,10)); c.setLayout(new FlowLayout()); time = new JLabel(); jbtime = new JLabel("耗时是:"); time.setText(time2+"s"); Run = false; start.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubtime2=0;Run=true;run();}}); c.add(jbtime); c.add(time); c.add(start); c.add(test);}public void run() {time.setText(time2+"s");for(int i=1;i<5;i++) {if(Run==true){time.setText(time2+"sasdf");try{TimeUnit.SECONDS.sleep(1);Thread.sleep(10);}catch (InterruptedException e1){}}time2++;}}public static void main(String[] args) {// TODO Auto-generated method stubt01 t = new t01();t.setVisible(true);}}
帮忙看看
本文介绍了一个使用Java Swing实现的简易计时器程序。该计时器能够按秒计数,并通过按钮控制启动。但存在延迟显示的问题,即按下开始按钮后,计时器会在设定的秒数过后才开始更新显示。
1580

被折叠的 条评论
为什么被折叠?



