在 Jpanel 中使用 JFrame 对象

发布于 2024-12-24 01:59:09 字数 1953 浏览 0 评论 0原文

我在java中有一个扩展JFrame的类,它显示时间:

class Clock extends JFrame implements Runnable{
Thread runner;
Font clockFont;

这段代码显示了一个数字时钟

 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 < 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");
              }                 
   }
 }

现在我想在我的另一个框架的JPanel中显示该时钟,我如何在其中添加Clock类的对象日本面板?!

感谢您的帮助。

I have a class in java that extends JFrame it shows Time :

class Clock extends JFrame implements Runnable{
Thread runner;
Font clockFont;

This code shows a Digital Clock

 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 < 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");
              }                 
   }
 }

Now I wanna show that clock in a JPanel in my other Frame , how can I add an object of Clock class in Jpanel ?!

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

依 靠 2024-12-31 01:59:10

您不能将 Clock 实例放置在 JPanel 中。这几乎没有道理!

时钟是一个 JFrame,因此将 JFrame 放置在 JPanel 中是没有意义的。

(除非您正在寻找内部框架): http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html

但是,听起来更像是您想要一个 ClockPanel 位于另一个内部JPanel 位于另一个 JFrame 中。

像这样的东西应该可以工作:

JFrame jFrame = new JFrame();
JPanel panel = new JPanel();
panel.add(new ClockPanel());
jFrame.add(panel);

也就是说,除非您执行这些代码行的类与类 Clock 不在同一个包中。
在这种情况下,请将您的 ClockPanel 类设为 public

You can not place an instance of Clock in a JPanel. That hardly makes sense!

Clock is a JFrame, and so it would not make sense to place a JFrame inside of a JPanel.

(Unless you are looking for Internal Frames): http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html

However, it sounds more like you want a ClockPanel inside of another JPanel which is in another JFrame.

Something like this should work:

JFrame jFrame = new JFrame();
JPanel panel = new JPanel();
panel.add(new ClockPanel());
jFrame.add(panel);

That is, unless the class from which you are executing these lines of code is not in the same package as class Clock.
In that case, make your ClockPanel class public.

梨涡少年 2024-12-31 01:59:10

您可以将这部分代码作为您的 Clock 类:

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.*;
import javax.swing.border.BevelBorder;

class Clock extends JLabel
{
    Timer t = null;

    //get current time
    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;                                    //Add leading zero if needed
     }

    public Clock()
    {
        super();
        setFont(new Font("Serif", Font.BOLD, 40));    //create font instance);

        setBorder(new BevelBorder(1));
        t = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DoChangeText();
            }
        });

        t.start();
    }

    public void DoChangeText()
    {
        this.setText(timeNow());
    }
}

您可以使用它添加到 JFrame 中,如下代码:

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,300);
    frame.setVisible(true);
    frame.setLayout(null);

    Clock c = new Clock();
    c.setSize(250, 100);
    c.setLocation(12, 100 );

    frame.add(c);

You can place this part of code as your Clock class :

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.*;
import javax.swing.border.BevelBorder;

class Clock extends JLabel
{
    Timer t = null;

    //get current time
    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;                                    //Add leading zero if needed
     }

    public Clock()
    {
        super();
        setFont(new Font("Serif", Font.BOLD, 40));    //create font instance);

        setBorder(new BevelBorder(1));
        t = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DoChangeText();
            }
        });

        t.start();
    }

    public void DoChangeText()
    {
        this.setText(timeNow());
    }
}

You can use it to add in JFrame as following code :

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,300);
    frame.setVisible(true);
    frame.setLayout(null);

    Clock c = new Clock();
    c.setSize(250, 100);
    c.setLocation(12, 100 );

    frame.add(c);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文