import java.awt.*;
import javax.swing.*;
import java.util.*;public class Foo
{
public static void main(String[] args)
{
JFrame f = new JFrame();
f.getContentPane().add(new PanelTest());
f.setSize(200,200);
f.setVisible(true);
}
}class PanelTest extends Panel implements Runnable{
private Thread thd;
public PanelTest(){
thd=new Thread(this);
thd.start();
}
public void run(){
while(thd!=null){
repaint();
try{
thd.sleep(1000);
}catch(InterruptedException e){}
}
}
public void paint(Graphics g){
Date now=new Date();
g.drawString(now.getHours()+":"+now.getMinutes()+":"+now.getSeconds(),20,20);
}
}
import javax.swing.*;
import java.util.*;public class Foo
{
public static void main(String[] args)
{
JFrame f = new JFrame();
f.getContentPane().add(new PanelTest());
f.setSize(200,200);
f.setVisible(true);
}
}class PanelTest extends Panel implements Runnable{
private Thread thd;
public PanelTest(){
thd=new Thread(this);
thd.start();
}
public void run(){
while(thd!=null){
repaint();
try{
thd.sleep(1000);
}catch(InterruptedException e){}
}
}
public void paint(Graphics g){
Date now=new Date();
g.drawString(now.getHours()+":"+now.getMinutes()+":"+now.getSeconds(),20,20);
}
}
解决方案 »
- 请教个简单问题,是斐波那契数列
- serial中的getInputString与getOutputString的具体用法。
- 组件的问题~~
- 账号和密码这种系统登录的方式,不安全
- Netbeans里面的JFrame怎么设置紧张改变大小
- 菜鸟问:在win xp系统中,没有classpath环境变量
- 如果定义像C++一样的操作符生载? 如^ | & &= |=等?
- 能不能用java播放mp3
- jb8装好了,用第二种方法也注册"成功"了,可当再次点击jb启动的图标怎么又让我注册了啊?请问这改怎么办?谢谢
- 简单问题:jbuilder7中的jTextArea怎么设置scrollbar属性
- 再问:如何实现我的想法:(
- Java中使用字节
这是我的现实时间的程序,可以运行,在jcreator上编译运行,就是用到线程
/**
* My_Time.java 19 July 2004
* @version:1.0
* @author:Yun_feng Gao
* Last modified 20/July/2004
*/import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
/**
* A small java programe that display the nowTime and date
*/
public class My_Time
{
public static void main(String args[])
{
JFrame mainFrame=new TestFrame();
mainFrame.show();
}
}
class TestFrame extends JFrame
{
private TimePanel timePanel;
private TimeAction timeAction;
public TestFrame()
{
setSize(400,600);
setTitle("TEST");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e){
System.exit(0);
}
});
timePanel=new TimePanel();
timeAction=new TimeAction(timePanel);
timePanel.setActionLitener(timeAction); Container contentPane=getContentPane();
contentPane.add(timePanel,BorderLayout.CENTER);
//contentPane.add(new MemoryMonitor(),BorderLayout.SOUTH);
}
}
class TimePanel extends JPanel
{
private JButton button;
private JLabel label;
private JButton autoChangeTime;
private JTextField text;
private JPanel panel; public TimePanel()
{
button=new JButton("Change time");
autoChangeTime=new JButton("AutoChangeTime");
label=new JLabel(Calendar.getInstance().getTime().toString());
text=new JTextField(Calendar.getInstance().getTime().toString()); panel=new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
panel.add(button);
panel.add(autoChangeTime); setLayout(new BorderLayout());
add(panel,BorderLayout.NORTH);
add(text,BorderLayout.CENTER);
add(label,BorderLayout.SOUTH);
}
/**
* method setTime
* @param s used to set the text of label
*/
public void setTime(String s)
{
label.setText(s);
label.revalidate();
}
public void setText(String s)
{
text.setText(s);
}
public void setActionLitener(TimeAction timeAction)
{
button.addActionListener(timeAction);
autoChangeTime.addActionListener(timeAction);
}
public String getActionSource(Object actionSource)
{
String name=null;
if(actionSource.equals(button))
name="button";
if(actionSource.equals(autoChangeTime))
name="autoChangeTime";
return name;
}
}
class TimeAction implements ActionListener
{
private Calendar time;
private TimePanel timePanel;
private String buttonName; public TimeAction(TimePanel timePanel)
{
super();
this.timePanel=timePanel;
}
public void actionPerformed(ActionEvent evt)
{
String s=Calendar.getInstance ().getTime ().toString ();
buttonName=timePanel.getActionSource(evt.getSource());
if(buttonName.equals("button"))
{
timePanel.setText("Now time is:"+s);
timePanel.setTime(s); if(Thread.currentThread()!=null)
{
timePanel.setText(s);
timePanel.setTime(s);
}
}
if(buttonName.equals("autoChangeTime"))
{
TestThread thread=new TestThread(timePanel);
thread.setPriority(1);
thread.start();
}
}
}
class TestThread extends Thread
{
private TimePanel test; public TestThread(TimePanel test)
{
this.test=test;
}public void run()
{
try
{
while(true)
{
test.setText(Calendar.getInstance().getTime().toString());
test.setTime(Calendar.getInstance().getTime().toString());
Thread.currentThread().sleep(100);
}
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
}