this is the applet which i want to add it to my JFrame:
import java.awt.Graphics;
import java.util.Date;
public class Clock extends java.applet.Applet implements Runnable {
Thread clockThread = null;
public void start() {
if (clockThread == null) {
clockThread = new Thread(this, "Clock");
clockThread.start();
}
}
public void run() {
// loop terminates when clockThread is set to null in stop()
while (Thread.currentThread() == clockThread) {
repaint();
try {
clockThread.sleep(1000);
} catch (InterruptedException e){
}
}
}
public void paint(Graphics g) {
Date now = new Date();
g.drawString(now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds(), 5, 10);
}
public void stop() {
clockThread = null;
}
}
> this is the applet which i want to add it to my
> JFrame:
>
> import java.awt.Graphics;
> import java.util.Date;
>
> public class Clock extends java.applet.Applet
> implements Runnable {
>
>Thread clockThread = null;
>public void start() {
>if (clockThread == null) {
>clockThread = new Thread(this, "Clock");
>clockThread.start();
>}
>}
>public void run() {
> // loop terminates when clockThread is set to
> null in stop()
> while (Thread.currentThread() == clockThread)
> {
>repaint();
> try {
>clockThread.sleep(1000);
> atch (InterruptedException e){
> }
>}
> public void paint(Graphics g) {
>Date now = new Date();
> g.drawString(now.getHours() + ":" + now.getMinutes()
> + ":" + now.getSeconds(), 5, 10);
>}
> public void stop() {
>clockThread = null;
> }
>
replace 'java.applet.Applet' with 'javax.swing.JPanel' , after that
add that clock object to your JFrame
> i did like , and add the clock object to the Jframe
> but i have and exception :
>
> IllegalArgumentException: adding a window to a
> container
>
> Message was edited by:
> student14
to add object to frame such as jpanel , you cannot use add method .
first your should use method getContentPane() , and next step add that object
for example in jframe class
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(clock, BorderLayout.CENTER);
> > i did like , and add the clock object to the
> Jframe
> > but i have and exception :
> >
> > IllegalArgumentException: adding a window to
> a
> > container
> >
> > Message was edited by:
> > student14
>
> to add object to frame such as jpanel , you cannot
> use add method .
> first your should use method getContentPane() , and
> next step add that object
>
> for example in jframe class
>
> > this.getContentPane().setLayout(new BorderLayout());
> this.getContentPane().add(clock,
> BorderLayout.CENTER);
>
If I read doc-api , if you call method
this.getContentPane().add(clock, BorderLayout.CENTER)
will throws 'NullPointerException' not
'IllegalArgumentException: adding a window to'