主题:电子时钟时区问题
这是我写的电子时钟程序
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.font.*;
import java.util.*;
import java.text.*;
import javax.swing.plaf.*;
class DigitalClock extends JPanel
{
DigitalClock()
{
this.setUI(new DigitalClockPanelUI().createPaneUI(this));
this.setOpaque(true);
}
public static void main(String []args)
{
JFrame frame=new JFrame();
frame.setLocation(200,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DigitalClock panel=new DigitalClock();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
class DigitalClockPanelUI extends PanelUI implements ActionListener
{
Font clockfont=new Font("Arial",Font.BOLD,24);
SimpleDateFormat dateformat=new SimpleDateFormat("hh:mm:ss");
FontRenderContext frc=new FontRenderContext(null,true,true);
int f=2;
AttributedString timestring=null;
TextLayout textlayout=null;
javax.swing.Timer timer=new javax.swing.Timer(1000,this);
DigitalClock panel;
DigitalClockPanelUI(){ }
DigitalClockPanelUI(DigitalClock panel)
{
this.panel=panel;
panel.setBackground(Color.black);
actionPerformed(null);
timer.start();
}
public ComponentUI createPaneUI(JComponent component)
{
return new DigitalClockPanelUI((DigitalClock)component);
}
public void paint(Graphics g,JComponent c)
{
if(this.timestring==null||this.textlayout==null) return;
Graphics2D g2=(Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawString(timestring.getIterator(),1,(int)(textlayout.getAscent()));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_DEFAULT);
}
public void actionPerformed(ActionEvent event)
{
timestring=new AttributedString(dateformat.format(new Date()));
timestring.addAttribute(TextAttribute.FONT,clockfont);
timestring.addAttribute(TextAttribute.FOREGROUND,Color.red);
timestring.addAttribute(TextAttribute.FOREGROUND,Color.yellow,2,3);
timestring.addAttribute(TextAttribute.FOREGROUND,Color.yellow,5,6);
textlayout=new TextLayout(timestring.getIterator(),frc);
panel.repaint();
}
public Dimension getPreferredSize(JComponent c)
{
Dimension size=textlayout.getBounds().getBounds().getSize();
size.height+=f;
size.width+=(f+2);
return size;
}
}
}
但是这个显示的时间是以格林威治标准时间为准的,我计算机的时区是北京(GMT+8:00),怎样修改使它自己从系统获得时区,让显示的时间和系统时间一致
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.font.*;
import java.util.*;
import java.text.*;
import javax.swing.plaf.*;
class DigitalClock extends JPanel
{
DigitalClock()
{
this.setUI(new DigitalClockPanelUI().createPaneUI(this));
this.setOpaque(true);
}
public static void main(String []args)
{
JFrame frame=new JFrame();
frame.setLocation(200,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DigitalClock panel=new DigitalClock();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
class DigitalClockPanelUI extends PanelUI implements ActionListener
{
Font clockfont=new Font("Arial",Font.BOLD,24);
SimpleDateFormat dateformat=new SimpleDateFormat("hh:mm:ss");
FontRenderContext frc=new FontRenderContext(null,true,true);
int f=2;
AttributedString timestring=null;
TextLayout textlayout=null;
javax.swing.Timer timer=new javax.swing.Timer(1000,this);
DigitalClock panel;
DigitalClockPanelUI(){ }
DigitalClockPanelUI(DigitalClock panel)
{
this.panel=panel;
panel.setBackground(Color.black);
actionPerformed(null);
timer.start();
}
public ComponentUI createPaneUI(JComponent component)
{
return new DigitalClockPanelUI((DigitalClock)component);
}
public void paint(Graphics g,JComponent c)
{
if(this.timestring==null||this.textlayout==null) return;
Graphics2D g2=(Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawString(timestring.getIterator(),1,(int)(textlayout.getAscent()));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_DEFAULT);
}
public void actionPerformed(ActionEvent event)
{
timestring=new AttributedString(dateformat.format(new Date()));
timestring.addAttribute(TextAttribute.FONT,clockfont);
timestring.addAttribute(TextAttribute.FOREGROUND,Color.red);
timestring.addAttribute(TextAttribute.FOREGROUND,Color.yellow,2,3);
timestring.addAttribute(TextAttribute.FOREGROUND,Color.yellow,5,6);
textlayout=new TextLayout(timestring.getIterator(),frc);
panel.repaint();
}
public Dimension getPreferredSize(JComponent c)
{
Dimension size=textlayout.getBounds().getBounds().getSize();
size.height+=f;
size.width+=(f+2);
return size;
}
}
}
但是这个显示的时间是以格林威治标准时间为准的,我计算机的时区是北京(GMT+8:00),怎样修改使它自己从系统获得时区,让显示的时间和系统时间一致