简体   繁体   English

在JPanel中实例化JApplet实例?

[英]Instantiate instance of JApplet in JPanel?

Right so my team mate has created a JApplet which contains a countdown 是的,所以我的队友创建了一个包含倒计时的JApplet

I have to take his JApplet and fit it inside a JPanel/JTab (Which its inside a JFrame ), something from the NetBeans Palette. 我必须带他的JApplet并将其安装在JPanel/JTab (将其安装在JFrame ),这是NetBeans Palette中的内容。

Simply speaking i have a page with many components and I just want the countdown at the top right. 简而言之,我有一个包含许多组件的页面,我只想在右上角倒数。

Using the GUI builder in netbeans, i created a JPanel and an event. 使用netbeans中的GUI构建器,我创建了一个JPanel和一个事件。 In the event handler, i put: 在事件处理程序中,我输入:

private void jPanel1ComponentShown(java.awt.event.ComponentEvent evt) {                                       
    // TODO: Countdown

    Package.Packagename.Countdown.init();
}                                      

The error message i get is: 我收到的错误消息是:

non-static method init() cannot be referenced from a static context. 非静态方法init()不能从静态上下文中引用。

The Applet works fine on its own, i just cant put it in here. Applet本身可以很好地工作,我不能将它放在这里。

"I have to take his JApplet and fit it inside a JPanel" “我必须拿他的JApplet并将其安装在JPanel中”

JApplet is a top level container and should not be put inside of a JPanel . JApplet是顶级容器,不应放在JPanel You can however take the JApplets component contents and put them inside the JPanel 但是,您可以获取JApplets组件的内容并将其放入JPanel

-OR- -要么-

Instead of trying to use your friend's applet code, just create your own. 无需尝试使用朋友的applet代码,只需创建自己的即可。 Implementing a countdown is really not that difficult. 实施倒计时确实没有那么困难。 Just hand code it. 只需手动编码即可。

  1. Drag and drop a label to the corner of your frame ( jLabel ) 将标签拖放到框架的一角( jLabel

  2. Then use something like this 然后用这样的东西

     public class MyFrame extends javax.swing.JFrame { int time = 100; public MyFrame () { initComponents(); jLabel.setText(String.valueOf(count)); javax.swing.Timer timer = new javax.swingTimer(1000, new java.awt.event.ActionListener(){ public void actionPerformed(java.awt.event.ActionEvent e) { if (count == 0) { ((javax.swing.Timer)e.getSource()).stop(); } else { count--; jLabel.setText(String.valueOf(count)); } } }); timer.start(); } } 

If you want, just make a Timer a class member that you can call from another actionPerformed to start() or stop() it 如果需要,只需将Timer设为可以从另一个actionPerformed调用的类成员即可执行为start()stop()

An Applet is a web component that has nothing to do inside a desktop app. Applet是与桌面应用程序无关的Web组件。

Create a widget (something wrapped in a JPanel or whatever) that you will both use in your applet and your desktop app. 创建一个小部件(包裹在JPanel东西或其他东西),您将在小程序和桌面应用程序中使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM