Note that there are some explanatory texts on larger screens.

plurals
  1. POUse an object of JFrame in a Jpanel
    primarykey
    data
    text
    <p>I have a class in java that extends JFrame it shows Time :</p> <pre><code>class Clock extends JFrame implements Runnable{ Thread runner; Font clockFont; </code></pre> <p>This code shows a Digital Clock</p> <pre><code> public Clock() { super("Java clock"); setSize( 350, 100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setResizable(false); clockFont = new Font("Serif", Font.BOLD, 40); Container contentArea = getContentPane(); ClockPanel timeDisplay = new ClockPanel(); contentArea.add(timeDisplay); setContentPane(contentArea); start(); } class ClockPanel extends JPanel { public void paintComponent(Graphics painter ) { Image pic = Toolkit.getDefaultToolkit().getImage("background.jpg"); if(pic != null){ painter.drawImage(pic, 0, 0, this); painter.setFont(clockFont); painter.setColor(Color.black); 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 &lt; 10) ? ("0"+num) : (""+num); return number; } 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) { System.out.println("Thread failed"); } } } </code></pre> <p>Now I wanna show that clock in a <code>JPanel</code> in my other Frame , how can I add an object of Clock class in Jpanel ?!</p> <p>Thanks for your help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload