简体   繁体   中英

Applet works, but swing components won't appear!

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

I see several problems with your code here:

  1. you do not call pack() at the end of your GUI setup
  2. you add several components to the applet's content pane, but without any layout constraints. The default content pane usually is a BorderLayout, so adding two components without any constraints will probably only put the ClientTreePanel on top.

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).

Try to delete your paint method and you'll see it works. The problem might be the fact that since you've got a paint method all the changes are made through it. It's weird though because it does display a JButton.

You shouldn't have to call pack() - the layout will be calculated when the component first becomes realized, which happens when you call pack - but also when the component first becomes visible.

The "adding components without constraints" is on the right track - you should change the code adding components to the content pane to :

getContentPane().add(new JLabel("TESTING!"), BorderLayout.NORTH);
getContentPane().add(treePanel, BorderLayout.CENTER);

The other problem is why your ClientTreePanel component isn't showing up - it could be sizing, or a layout problem, or other things - but without seeing that code, it would just be guesses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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