小程序可以工作,但不会出现摆动组件! [英] Applet works, but swing components won't appear!

查看:20
本文介绍了小程序可以工作,但不会出现摆动组件!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在创建这个小程序,我希望在其中包含完整的摆动组件.我已经查看了所有文档,我已经制作了小程序,如果我覆盖 update(Graphics g) 方法,我可以在其中显示一些内容,但只需将组件添加到 contentPane 似乎没有这样做!我做错了什么?

So I am creating this applet which I want to have full on swing components in it. I have looked at all the docs, I have made the applet, and I can get something to show up in it if I override the update(Graphics g) method, but simply adding components to the contentPane doesn't seem to be doing it! What am I doing wrong?

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);
    }
     */
}

请帮忙!谢谢!约书亚

推荐答案

我发现你的代码有几个问题:

I see several problems with your code here:

  1. 不要在 GUI 设置结束时调用 pack()
  2. 您将多个组件添加到小程序的内容窗格,但没有任何布局限制.默认的内容窗格通常是一个 BorderLayout,因此添加两个没有任何约束的组件可能只会将 ClientTreePanel 放在顶部.

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

Since you do not call pack(), the layout will not be calculated, which for your case probably results in nothing being displayed (you did not provide the code for ClientTreePanel).

这篇关于小程序可以工作,但不会出现摆动组件!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆