运用java多线程技术,通过实现Runnable接口写一个电子时钟的应用程序RunnableClock
packagecom.task01;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Fon...
package com.task01;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Clock2 extends JFrame implements Runnable
{
JFrame frame = new JFrame();
JPanel pane;
String time;
Date timer;
public Clock2()
{
pane = (JPanel)frame.getContentPane();
pane.setLayout(new BorderLayout());
pane.setSize(280,40);
pane.setBackground(Color.white);
pane.add(this,BorderLayout.CENTER);
frame.setVisible(true);
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g)
{
Font f = new Font("黑体",Font.BOLD, 16);
SimpleDateFormat SDF = new SimpleDateFormat("yyyy'年'MM'月'dd'日'hh:mm:ss");
Calendar now = Calendar.getInstance();
time = SDF.format(now.getTime());
g.setFont(f);
g.setColor(Color.blue);
g.drawString(time, 45, 25);
int dayOFWeek = now.get(Calendar.DAY_OF_WEEK);
switch(dayOFWeek)
{
case 1: g.drawString("星期日", 120, 60);
break;
case 2: g.drawString("星期一", 120, 60);
break;
case 3: g.drawString("星期二", 120, 60);
break;
case 4: g.drawString("星期三", 120, 60);
break;
case 5: g.drawString("星期四", 120, 60);
break;
case 6: g.drawString("星期五", 120, 60);
break;
case 7: g.drawString("星期六", 120, 60);
break;
}
}
public void run()
{
while(true)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
repaint();
}
}
public static void main(String[] args)
{
(new Clock2()).run();
}
}
O O。出现了什么问题..求帮助 展开
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Clock2 extends JFrame implements Runnable
{
JFrame frame = new JFrame();
JPanel pane;
String time;
Date timer;
public Clock2()
{
pane = (JPanel)frame.getContentPane();
pane.setLayout(new BorderLayout());
pane.setSize(280,40);
pane.setBackground(Color.white);
pane.add(this,BorderLayout.CENTER);
frame.setVisible(true);
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g)
{
Font f = new Font("黑体",Font.BOLD, 16);
SimpleDateFormat SDF = new SimpleDateFormat("yyyy'年'MM'月'dd'日'hh:mm:ss");
Calendar now = Calendar.getInstance();
time = SDF.format(now.getTime());
g.setFont(f);
g.setColor(Color.blue);
g.drawString(time, 45, 25);
int dayOFWeek = now.get(Calendar.DAY_OF_WEEK);
switch(dayOFWeek)
{
case 1: g.drawString("星期日", 120, 60);
break;
case 2: g.drawString("星期一", 120, 60);
break;
case 3: g.drawString("星期二", 120, 60);
break;
case 4: g.drawString("星期三", 120, 60);
break;
case 5: g.drawString("星期四", 120, 60);
break;
case 6: g.drawString("星期五", 120, 60);
break;
case 7: g.drawString("星期六", 120, 60);
break;
}
}
public void run()
{
while(true)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
repaint();
}
}
public static void main(String[] args)
{
(new Clock2()).run();
}
}
O O。出现了什么问题..求帮助 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询