0
我想在此數字時鐘的每個刻度上添加變化的背景顏色,但我不知道從哪裏開始。下面的代碼在每個時鐘刻度上更改背景顏色
public void start()
{
if (runner==null) runner =new Thread(this);
runner.start();
}
public void run()
{
while(runner==Thread.currentThread())
{
repaint();
try {Thread.sleep(1000);}
catch(InterruptedException e) {}
}
}
其他信息我剔一部分,如果這能幫助:
public Clockclass()
{
super ("javaclock"); setSize(300,100);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible(true);
clockFont= new Font("Serif", Font.BOLD,50);
Container contentArea =getContentPane();
ClockPanel timeDisplay = new ClockPanel();
contentArea.add(timeDisplay);
setContentPane(contentArea);
start();
}
class ClockPanel extends JPanel
{
public void paintComponent (Graphics painter)
{
painter.setColor(Color.cyan);
painter.fillRect(0,0,300,100);
painter.setFont(clockFont);
painter.setColor(Color.white);
painter.drawString(timeNow(), 60, 40);
}
}
public String timeNow(){
Calendar now =Calendar.getInstance();
int hrs=now.get(Calendar.HOUR_OF_DAY);
int min=now.get(Calendar.MINUTE);
int sec=now.get(Calendar.SECOND);
String time= zero(hrs)+":"+zero(min)+":"+zero(sec);
return time;
}
public String zero(int num)
{
String number= (num<10)?("0"+num):(""+num);
return number;
}
提前感謝!