「javaswing记时」swing进行时怎么写

博主:adminadmin 2022-11-27 17:32:06 83

本篇文章给大家谈谈javaswing记时,以及swing进行时怎么写对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java怎么在swing文本框中实现一个时间倒计时的界面?项目需要,谢谢

通常可以设置某个固定的切换时间,之后显示固定的或者是随机的显示某张图片,举例:importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassMousDemoextendsJFrame{MyJPanelmp;intindex;ImageIcon[]imgs={newImageIcon("C:\\Users\\lenovo\\Desktop\\a.png"),newImageIcon("C:\\Users\\lenovo\\Desktop\\b.png")};publicMousDemo(){mp=newMyJPanel(false);this.add(mp);this.setSize(300,200);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setTitle("鼠标窗口");this.setVisible(true);/***方式一,使用TImer来切换图片*Swing下的Timer组件,个人觉得非常适合*Timer(200,newActionListener());意思就是每200毫秒执行一次ActionListener里面的方法**/Timertimer=newTimer(200,newActionListener(){@OverridepublicvoidactionPerformed(ActionEvente){mp.flag=!mp.flag;mp.repaint();}});timer.start();}publicstaticvoidmain(String[]args){newMousDemo();}classMyJPanelextendsJPanel{booleanflag;publicMyJPanel(booleanflag){this.flag=flag;}@Overridepublicvoidpaint(Graphicsg){super.paint(g);if(flag==false){g.drawImage(imgs[0].getImage(),0,0,this);}else{g.drawImage(imgs[1].getImage(),0,0,this);}}}}

JAVA秒表倒计时程序,用swing界面显示,请大家一定要帮帮小弟哈,我实在做不来这个,谢谢了。120分!!!

package com;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Timer;

import java.util.TimerTask;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Tidy extends JFrame

{

private static final long serialVersionUID = 1L;

private static final String[] NS =

        { "秒表倒计时器", "时", "分", "秒", "请输入你倒计时的时间(分钟数):", "你一共用:", "时", "分", "秒", "开始倒计时", "暂停计时", "停止计时" };

private static int index = 0;

private long ms = 0;

private boolean isPaused = false;

private Timer timer;

public Tidy(String title)

{

setTitle(title);

}

private Tidy addComponents()

{

final JTextField[] ts = new JTextField[7];

for(int i = 0; i  7; i++)

{

ts[i] = new JTextField();

ts[i].setPreferredSize(new Dimension(50, 20));

if(i != 3)

{

ts[i].setEditable(false);

}

}

JLabel[] ls = new JLabel[9];

for(int i = 0; i  9; i++)

{

ls[i] = new JLabel(NS[index++]);

}

final JButton[] bs = new JButton[3];

for(int i = 0; i  3; i++)

{

bs[i] = new JButton(NS[index++]);

}

bs[0].addActionListener(new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

try

{

if(!isPaused)

{

long min = Long.parseLong(ts[3].getText());

long h = min / 60;

h = h  0 ? 0 : h;

long m = min - h * 60 - 1;

m = m  0 ? 0 : m;

long s = min == 0 ? 0 : 60;

ts[0].setText(h + "");

ts[1].setText(m + "");

ts[2].setText(s + "");

}

timer = new Timer();

timer.schedule(new TimerTask()

{

@Override

public void run()

{

long h = Long.parseLong(ts[0].getText());

long m = Long.parseLong(ts[1].getText());

long s = Long.parseLong(ts[2].getText());

s--;

ms++;

if((h != 0 || m != 0)  s == 0)

{

m--;

s = 59;

}

if(h != 0  m == 0)

{

h--;

m = 59;

}

h = h  0 ? 0 : h;

m = m  0 ? 0 : m;

s = s  0 ? 0 : s;

ts[0].setText(h + "");

ts[1].setText(m + "");

ts[2].setText(s + "");

long ph = ms / 60 / 60;

long pm = ms / 60;

long ps = ms % 60;

ts[4].setText(ph + "");

ts[5].setText(pm + "");

ts[6].setText(ps + "");

if(h == 0  m == 0  s == 0)

{

bs[2].doClick();

}

}

}, 0, 1000);

bs[0].setEnabled(false);

}

catch(NumberFormatException nfe)

{

JOptionPane.showConfirmDialog(Tidy.this, "输入错误,请重新输入分钟的整数。", "友情提示", JOptionPane.PLAIN_MESSAGE,

                JOptionPane.ERROR_MESSAGE);

}

}

});

bs[1].addActionListener(new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

if(null != timer)

{

timer.cancel();

timer = null;

}

bs[0].setEnabled(true);

isPaused = true;

}

});

bs[2].addActionListener(new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

if(null != timer)

{

timer.cancel();

timer = null;

}

bs[0].setEnabled(true);

isPaused = false;

ms = 0;

}

});

JComponent[][] cs = { { ls[0] }, { ts[0], ls[1], ts[1], ls[2], ts[2], ls[3] }, { ls[4], ts[3] },

        { ls[5], ts[4], ls[6], ts[5], ls[7], ts[6], ls[8] }, { bs[0], bs[1], bs[2] } };

JPanel[] ps = new JPanel[5];

JPanel wrap = new JPanel();

wrap.setLayout(new BoxLayout(wrap, BoxLayout.Y_AXIS));

for(int i = 0; i  5; i++)

{

ps[i] = new JPanel(new FlowLayout(FlowLayout.CENTER));

for(int j = 0; j  cs[i].length; j++)

{

ps[i].add(cs[i][j]);

}

wrap.add(ps[i]);

}

add(wrap);

return this;

}

private Tidy init()

{

pack();

setResizable(false);

setLocationRelativeTo(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

return this;

}

public static void main(String[] args)

{

Tidy tidy = new Tidy(NS[index]);

tidy.addComponents().init();

}

}

用了java.swing.Timer的类为什么计时器没反应?

Timer tim=new Timer(1000,this);//实例话一个间隔为1000毫秒,就启动这个

= tim=new Timer(1000,this);//实例话一个间隔为1000毫秒,就启动这个

grs.drawLine的 参数是常量,当然不会动了。

javaswing记时的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于swing进行时怎么写、javaswing记时的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。