java - 在java小程序中加载面板内容

标签 java swing applet

我正在尝试将面板加载到 java 小程序中,但面板的内容未填充。从下面的代码中可以看到,我设置了一个测试来查看面板中的代码在哪里无法运行,测试结果表明 getRootPane().add(MyLabel)是触发异常的代码行。

下面包含重现此问题所需的所有代码。谁能告诉我如何更改下面的代码,以便将面板的内容加载到小程序中?

这是 TestApplet.java 的代码:

import javax.swing.JApplet;
import javax.swing.SwingUtilities;

public class TestApplet extends JApplet {

    public void init(){//Called when this applet is loaded into the browser.
        //Execute a job on the event-dispatching thread; creating this applet's GUI.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    createGUI();
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }
    private void createGUI(){
        TestPanel myPanel = new TestPanel();
        myPanel.setOpaque(true);
        setContentPane(myPanel);
    }
}

这是 TestPanel.java 的代码:

import javax.swing.JLabel;
import javax.swing.JPanel;

public class TestPanel extends JPanel{

    TestPanel(){
        System.out.println("Running in constructor.  ");
        JLabel myLabel = new JLabel("Hello World");
        getRootPane().add(myLabel);
        System.out.println("Still running in constructor.  ");
    }
}

编辑:

根据目前给出的建议,我编辑了代码如下。使用 this.add 确实会导致 JLabel 加载,但是,内部类仍未加载,我已将其添加到下面的代码中。此外,下面更改后的代码不再触发异常;它只加载 JLabel 但不加载内部类。关于如何加载内部类有什么建议吗?

这是新的 TestApplet.java:

import javax.swing.JApplet;
import javax.swing.SwingUtilities;

public class TestApplet extends JApplet {

    public void init(){//Called when this applet is loaded into the browser.
        //Execute a job on the event-dispatching thread; creating this applet's GUI.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    createGUI();
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
            System.err.println(e);
            e.printStackTrace();
        }
    }
    private void createGUI(){
        TestPanel myPanel = new TestPanel();
        myPanel.setOpaque(true);
        setContentPane(myPanel);
    }
}  

这是新的 TestPanel.java:

import java.awt.Canvas;  
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class TestPanel extends JPanel{
    DrawingLines myDrawingLines = new DrawingLines();  

    TestPanel(){
        System.out.println("Running in constructor.  ");
        JLabel myLabel = new JLabel("Hello World");
        this.add(myLabel);
    this.add(myDrawingLines);  
    myDrawingLines.repaint();  
        System.out.println("Still running in constructor.  ");
    }

//inner class to override paint method
class DrawingLines extends Canvas{
   int width, height;

   public void paint( Graphics g ) {
      width = getSize().width;
      height = getSize().height;
      g.setColor( Color.green );
      for ( int i = 0; i < 10; ++i ) {
         g.drawLine( width, height, i * width / 10, 0 );
      }  
      System.out.println("Running in paint method.");  
   }
}//end of inner class   
}  

最佳答案

设置方法为:

 private void createGUI(){
        TestPanel myPanel = new TestPanel();
        getContentPane().add(myPanel);
    }

TestPanel 类为

public class TestPanel extends JPanel{
    TestPanel(){
        super();
        System.out.println("Running in constructor.  ");
        JLabel myLabel = new JLabel("Hello World");
        add(myLabel);
        System.out.println("Still running in constructor.  ");
    }
}

关于java - 在java小程序中加载面板内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15373997/

相关文章:

java - 我应该在 netbeans 中创建 Images 文件夹吗

java - Java插件和浏览器如何在内网验证applet的证书?

Java 7u51 : LiveConnect (JavaScript) blocked due to security settings

java - 如何防止 jar 在构建过程中多次编译

java - 递归斐波那契代码

java - 如何在Excel Java中使用POI在另一张纸上书写

java - 如何在 Heroku 上使用 jetty-runner 增加 Jetty 线程池大小?

java - 在 JTable 中添加新行后触发代码

java - 我想在我的简单记事本应用程序中用java实现 "Find Menu"

java - 如何防止Applet跨网页卸载?