I was tryin to create a program to demonstate Threads and i found this example for a clock in my school textbook. I ve copied i out exactly the same but I keep gettin an error, it says it expects a "{" at line 26.
I'm not sure how to paste the code.. so here it is I guess....
Quote
import java.awt.*;
import javax.swing.*;
import java.util.*;
class Clock extends JFrame implements Runnable
{
Thread runner;
Font clockFont;
public Clock()
{
super("Java Clock"); setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
clockFont= new Font("Serif", Font.BOLD, 40);
Container contentSrea = getContentPane();
ClockPanel timeDisplay = new ClockPanel();
contentArea.add(timeDisplay);
setContentPane(contentArea);
start();
}
class ClockPanelextends JPanel (<<line 26) {
public void paintComponent(Graphics painter)
{
painter.setColor(Color.white);
painter.fillRect(0,0,300,100);
painter.setColor(Color.black);
painter.drawSring( timeNow(), 60, 40);
}
}
public String timeNow()
{
Calender now = Calender.getInstance();
int hrs = now.get(Calender.HOUR_OF_DAY);
int min = now.get(Calender.MINUTE);
int sec = now.get(Calender.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;
}
public void start()
{
if(runer == null) runner = new Thread(this);
runner.start();
}
public void run()
{
while (runner == Thread.currentThread() )
{
repaint();
try { Thread.sleep(1000); }
catch(InterruptedException e) {}
}
}
public static void main(String[] args)
{
Clock eg = new Clock();
}
}
import javax.swing.*;
import java.util.*;
class Clock extends JFrame implements Runnable
{
Thread runner;
Font clockFont;
public Clock()
{
super("Java Clock"); setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
clockFont= new Font("Serif", Font.BOLD, 40);
Container contentSrea = getContentPane();
ClockPanel timeDisplay = new ClockPanel();
contentArea.add(timeDisplay);
setContentPane(contentArea);
start();
}
class ClockPanelextends JPanel (<<line 26) {
public void paintComponent(Graphics painter)
{
painter.setColor(Color.white);
painter.fillRect(0,0,300,100);
painter.setColor(Color.black);
painter.drawSring( timeNow(), 60, 40);
}
}
public String timeNow()
{
Calender now = Calender.getInstance();
int hrs = now.get(Calender.HOUR_OF_DAY);
int min = now.get(Calender.MINUTE);
int sec = now.get(Calender.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;
}
public void start()
{
if(runer == null) runner = new Thread(this);
runner.start();
}
public void run()
{
while (runner == Thread.currentThread() )
{
repaint();
try { Thread.sleep(1000); }
catch(InterruptedException e) {}
}
}
public static void main(String[] args)
{
Clock eg = new Clock();
}
}
P.S. can you reply as sson as possible want to use in assignment due this week. Thanks


Sign In
Create Account

Back to top









