这是一个小应用程序,我怎么才能在T1中显示实时的时间呢? 要求该程序能够完成以下功能:
(1) 在界面上方的文本框中,按照"小时:分钟:秒"的顺序实时显示系统时间;
(2) 当按下界面中间的"Current Time:"按钮时,当前系统时间能够在界面下方的文本框中显示出来。import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;public class ex_time extends Applet
{
Button B1=new Button("Current Time");
TextField T1=new TextField(20);
TextField T2=new TextField(20);
Date now = new Date();
public void init()
{
setLayout(new FlowLayout());
T1.setEnabled(false);
T2.setEnabled(false);
add(T1);
add(B1);
add(T2);
B1.addActionListener(new acl());
String str = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
T1.setText(str); }
class acl implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
T2.setText(T1.getText());
}
}
}
(1) 在界面上方的文本框中,按照"小时:分钟:秒"的顺序实时显示系统时间;
(2) 当按下界面中间的"Current Time:"按钮时,当前系统时间能够在界面下方的文本框中显示出来。import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;public class ex_time extends Applet
{
Button B1=new Button("Current Time");
TextField T1=new TextField(20);
TextField T2=new TextField(20);
Date now = new Date();
public void init()
{
setLayout(new FlowLayout());
T1.setEnabled(false);
T2.setEnabled(false);
add(T1);
add(B1);
add(T2);
B1.addActionListener(new acl());
String str = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
T1.setText(str); }
class acl implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
T2.setText(T1.getText());
}
}
}
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import javax.swing.*;public class ex_time extends JFrame
{
JButton B1=new JButton("Current Time");
JTextField T1=new JTextField(20);
JTextField T2=new JTextField(20);
Date now = new Date();
String str;
public ex_time()
{
setLayout(new FlowLayout());
T1.setEnabled(false);
T2.setEnabled(false);
add(T1);
add(B1);
add(T2);
setSize(300,200);
setVisible(true);
T th=new T();
th.start();
B1.addActionListener(new acl());
str = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
T1.setText(str);
}
class acl implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
T2.setText(T1.getText());
}
}
class T extends Thread
{
public void run()
{
while(true)
{
try{
now=new Date();
str = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
T1.setText(str);
Thread.sleep(1000);
}
catch(Exception ex)
{
}
}
}
}
public static void main(String args[])
{
new ex_time();
}
}
:)
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.text.*;
public class Clock extends Applet implements Runnable {
private volatile Thread timer; // The thread that displays clock
private int lastxs, lastys, lastxm,
lastym, lastxh, lastyh; // Dimensions used to draw hands
private SimpleDateFormat formatter; // Formats the date displayed
private String lastdate; // String to hold date displayed
private Font clockFaceFont; // Font for number display on clock
private Date currentDate; // Used to get date to display
private Color handColor; // Color of main hands and dial
private Color numberColor; // Color of second hand and numbers
private int xcenter = 80, ycenter = 55; // Center position public void init() {
int x,y;
lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy",
Locale.getDefault());
currentDate = new Date();
lastdate = formatter.format(currentDate);
clockFaceFont = new Font("Serif", Font.PLAIN, 14);
handColor = Color.blue;
numberColor = Color.darkGray; try {
setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),
16)));
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
try {
handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),
16));
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
try {
numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),
16));
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
resize(300,300); // Set clock window size
} // Paint is the main part of the program
public void update(Graphics g) {
int xh, yh, xm, ym, xs, ys;
int s = 0, m = 10, h = 10;
String today; currentDate = new Date();
formatter.applyPattern("s");
try {
s = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
s = 0;
}
formatter.applyPattern("m");
try {
m = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
m = 10;
}
formatter.applyPattern("h");
try {
h = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
h = 10;
}
// Set position of the ends of the hands
xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter);
ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter);
xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter);
ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter);
xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
+ xcenter);
yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
+ ycenter);
// Get the date to print at the bottom
formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
today = formatter.format(currentDate); g.setFont(clockFaceFont);
// Erase if necessary
g.setColor(getBackground());
if (xs != lastxs || ys != lastys) {
g.drawLine(xcenter, ycenter, lastxs, lastys);
g.drawString(lastdate, 5, 125);
}
if (xm != lastxm || ym != lastym) {
g.drawLine(xcenter, ycenter-1, lastxm, lastym);
g.drawLine(xcenter-1, ycenter, lastxm, lastym);
}
if (xh != lastxh || yh != lastyh) {
g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
g.drawLine(xcenter-1, ycenter, lastxh, lastyh);
} // Draw date and hands
g.setColor(numberColor);
g.drawString(today, 5, 125);
g.drawLine(xcenter, ycenter, xs, ys);
g.setColor(handColor);
g.drawLine(xcenter, ycenter-1, xm, ym);
g.drawLine(xcenter-1, ycenter, xm, ym);
g.drawLine(xcenter, ycenter-1, xh, yh);
g.drawLine(xcenter-1, ycenter, xh, yh);
lastxs = xs; lastys = ys;
lastxm = xm; lastym = ym;
lastxh = xh; lastyh = yh;
lastdate = today;
currentDate = null;
} public void paint(Graphics g) {
g.setFont(clockFaceFont);
// Draw the circle and numbers
g.setColor(handColor);
g.drawArc(xcenter-50, ycenter-50, 100, 100, 0, 360);
g.setColor(numberColor);
g.drawString("9", xcenter-45, ycenter+3);
g.drawString("3", xcenter+40, ycenter+3);
g.drawString("12", xcenter-5, ycenter-37);
g.drawString("6", xcenter-3, ycenter+45); // Draw date and hands
g.setColor(numberColor);
g.drawString(lastdate, 5, 125);
g.drawLine(xcenter, ycenter, lastxs, lastys);
g.setColor(handColor);
g.drawLine(xcenter, ycenter-1, lastxm, lastym);
g.drawLine(xcenter-1, ycenter, lastxm, lastym);
g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
g.drawLine(xcenter-1, ycenter, lastxh, lastyh);
} public void start() {
timer = new Thread(this);
timer.start();
} public void stop() {
timer = null;
} public void run() {
Thread me = Thread.currentThread();
while (timer == me) {
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
}
repaint();
}
} public String getAppletInfo() {
return "Title: A Clock \n"
+ "Author: Rachel Gollub, 1995 \n"
+ "An analog clock.";
}
public String[][] getParameterInfo() {
String[][] info = {
{"bgcolor", "hexadecimal RGB number",
"The background color. Default is the color of your browser."},
{"fgcolor1", "hexadecimal RGB number",
"The color of the hands and dial. Default is blue."},
{"fgcolor2", "hexadecimal RGB number",
"The color of the second hand and numbers. Default is dark gray."}
};
return info;
}
}
package gui;import javax.swing.*;
import java.util.*;
import java.awt.*;
public class ClockLabel extends JLabel implements Runnable{ Thread clock;
public ClockLabel() {
super();
// TODO Auto-generated constructor stub
this.start();
}
public void start(){
if(clock==null){
clock=new Thread(this);
clock.start();
}
}
public void run(){
while(clock!=null){
repaint();
try{
Thread.sleep(200);
}
catch(InterruptedException ex){
ex.printStackTrace();
}
}
}
public void stop(){
clock=null;
}
public void paint(Graphics g){
Graphics2D g2=(Graphics2D)g;
Calendar now=new GregorianCalendar();
String timeInfo="";
int hour=now.get(Calendar.HOUR_OF_DAY);
int minute=now.get(Calendar.MINUTE);
int second=now.get(Calendar.SECOND);
if(hour<=9)
timeInfo+="0"+hour+":";
else
timeInfo+=hour+":";
if(minute<=9)
timeInfo+="0"+minute+":";
else
timeInfo+=minute+":";
if(second<=9)
timeInfo+="0"+second;
else
timeInfo+=second;
g.setColor(Color.DARK_GRAY);
Dimension dim=getSize();
g.fillRect(0,0,dim.width,dim.height);
g.setColor(Color.pink);
g.drawString(timeInfo,35,15);
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub }}