在 Jpanel 中使用 JFrame 对象
我在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能将
Clock
实例放置在JPanel
中。这几乎没有道理!时钟是一个
JFrame
,因此将JFrame
放置在JPanel
中是没有意义的。(除非您正在寻找
内部框架
): http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html但是,听起来更像是您想要一个
ClockPanel
位于另一个内部JPanel
位于另一个JFrame
中。像这样的东西应该可以工作:
也就是说,除非您执行这些代码行的类与类
Clock
不在同一个包中。在这种情况下,请将您的
ClockPanel
类设为public
。You can not place an instance of
Clock
in aJPanel
. That hardly makes sense!Clock is a
JFrame
, and so it would not make sense to place aJFrame
inside of aJPanel
.(Unless you are looking for
Internal Frames
): http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.htmlHowever, it sounds more like you want a
ClockPanel
inside of anotherJPanel
which is in anotherJFrame
.Something like this should work:
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
classpublic
.您可以将这部分代码作为您的
Clock
类:您可以使用它添加到 JFrame 中,如下代码:
You can place this part of code as your
Clock
class :You can use it to add in JFrame as following code :