因此,我正在创建这个applet,我希望其中包含所有的swing组件。我查看了所有文档,制作了applet,如果覆盖了update(Graphics g)方法,可以在其中显示一些内容,但是仅向contentPane添加组件似乎没有用!我究竟做错了什么?

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import org.steephill.kindlab.LabApp;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class ClientApplet extends JApplet {

    ClientTreePanel treePanel;

    public void destroy() {
        // Put your code here
    }

    public String getAppletInfo() {
        return "KindLab Client Applet";
    }

    public void init() {

        try {
            LabApp.initializeHibernate();
            if (!LabApp.authenticate("user", "pass")) {

                JOptionPane.showMessageDialog(this, "authentication failed");
            } else {

                try {
                    javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
                        public void run() {
                            createGUI();
                        }
                    });
                } catch (Exception e) {
                    System.err.println("createGUI didn't successfully complete");
                }

                    }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "error intitializing applet\r\n" + ex.getMessage());
        }
    }

    protected void createGUI() {

        treePanel = new ClientTreePanel();
        treePanel.setVisible(true);
        getContentPane().add(new JLabel("TESTING!"));
        getContentPane().add(treePanel);

        System.out.println("THIS DOES RUN");
    }

    public void start() {
        // Put your code here
    }

    public void stop() {
        // Put your code here
    }

    /*  if I uncomment this method, it WORKS and I get "Hello World!"
    public void paint(Graphics g) {
        super.paint(g);
        g.drawString("Hello World!",25,25);
    }
     */
}


请帮忙!谢谢你!
约书亚记

最佳答案

我在这里看到您的代码有几个问题:


您不会在GUI设置结束时调用pack()
您可以在applet的内容窗格中添加几个组件,但是没有任何布局限制。默认的内容窗格通常是BorderLayout,因此添加两个没有任何约束的组件可能只会将ClientTreePanel放在顶部。


由于不调用pack(),因此不会计算布局,这可能会导致不显示任何内容(您未提供ClientTreePanel的代码)。

10-08 08:38