Applet的工作原理,但Swing组件将不会出现! [英] Applet works, but swing components won't appear!

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

问题描述

所以我创建这个小程序,我希望能有充分在它的swing组件。我已经看了所有的文档,我做了小程序,我可以得到的东西在里面显示出来,如果​​我重写更新(图形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);
    }
     */
}

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

Please, help! And thank you! Joshua

推荐答案

我看到几个问题你code位置:

I see several problems with your code here:


  1. 您不要叫你的GUI安装结束包()

  2. 您添加几个组件到ap​​plet的内容窗格中,但没有任何布局约束。默认的内容窗格通常是一个BorderLayout的,所以增加了两个部件不受任何约束,很可能只会把ClientTreePanel之上。

既然你不叫包(),布局将不被计算出来,这对于你的情况可能会导致没有显示(你没有提供的code代表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).

这篇关于Applet的工作原理,但Swing组件将不会出现!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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