簡體   English   中英

Applet可以運行,但不會顯示擺動組件!

[英]Applet works, but swing components won't appear!

因此,我正在創建這個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);
    }
     */
}

請幫忙! 謝謝你! 約書亞

我在這里看到您的代碼的幾個問題:

  1. 您不會在GUI設置結束時調用pack()
  2. 您可以在applet的內容窗格中添加幾個組件,但是沒有任何布局限制。 默認的內容窗格通常是BorderLayout,因此添加兩個沒有任何約束的組件可能只會將ClientTreePanel放在頂部。

由於不調用pack(),因此不會計算布局,這可能會導致不顯示任何內容(您未提供ClientTreePanel的代碼)。

嘗試刪除您的繪畫方法,您會發現它可行。 問題可能是這樣的事實,因為您有了繪畫方法,所有更改都是通過它進行的。 不過這很奇怪,因為它確實顯示了一個JButton。

你不應該叫包() -當組件首次成為實現布局將被計算,當你調用包恰好-而且當組件首次變得可見。

“無限制地添加組件”在正確的軌道上-您應將內容窗格中添加組件的代碼更改為:

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

另一個問題是為什么ClientTreePanel組件沒有顯示-可能是大小調整,布局問題或其他問題-但是卻沒有看到該代碼,這只是猜測。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM