簡體   English   中英

在Java applet中加載面板的內容

[英]load contents of panel in java applet

我試圖將面板加載到Java小程序中,但是面板中的內容沒有填充。 從下面的代碼中可以看到,我設置了一個測試以查看面板中的代碼無法運行的位置,並且測試的結果表明getRootPane()。add(MyLabel)是觸發代碼的行例外。

下面包含重新創建此問題所需的所有代碼。 誰能告訴我如何更改下面的代碼,以便將面板的內容加載到applet中?

這是TestApplet.java的代碼:

import javax.swing.JApplet;
import javax.swing.SwingUtilities;

public class TestApplet extends JApplet {

    public void init(){//Called when this applet is loaded into the browser.
        //Execute a job on the event-dispatching thread; creating this applet's GUI.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    createGUI();
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }
    private void createGUI(){
        TestPanel myPanel = new TestPanel();
        myPanel.setOpaque(true);
        setContentPane(myPanel);
    }
}

這是TestPanel.java的代碼:

import javax.swing.JLabel;
import javax.swing.JPanel;

public class TestPanel extends JPanel{

    TestPanel(){
        System.out.println("Running in constructor.  ");
        JLabel myLabel = new JLabel("Hello World");
        getRootPane().add(myLabel);
        System.out.println("Still running in constructor.  ");
    }
}

編輯:

根據到目前為止給出的建議,我對代碼進行了如下編輯。 使用this.add確實會導致JLabel加載,但是,內部類仍未加載,我已將其添加到下面的代碼中。 同樣,下面更改的代碼不再觸發異常; 它僅加載JLabel,而不加載內部類。 關於如何加載內部類有什么建議嗎?

這是新的TestApplet.java:

import javax.swing.JApplet;
import javax.swing.SwingUtilities;

public class TestApplet extends JApplet {

    public void init(){//Called when this applet is loaded into the browser.
        //Execute a job on the event-dispatching thread; creating this applet's GUI.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    createGUI();
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
            System.err.println(e);
            e.printStackTrace();
        }
    }
    private void createGUI(){
        TestPanel myPanel = new TestPanel();
        myPanel.setOpaque(true);
        setContentPane(myPanel);
    }
}  

這是新的TestPanel.java:

import java.awt.Canvas;  
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class TestPanel extends JPanel{
    DrawingLines myDrawingLines = new DrawingLines();  

    TestPanel(){
        System.out.println("Running in constructor.  ");
        JLabel myLabel = new JLabel("Hello World");
        this.add(myLabel);
    this.add(myDrawingLines);  
    myDrawingLines.repaint();  
        System.out.println("Still running in constructor.  ");
    }

//inner class to override paint method
class DrawingLines extends Canvas{
   int width, height;

   public void paint( Graphics g ) {
      width = getSize().width;
      height = getSize().height;
      g.setColor( Color.green );
      for ( int i = 0; i < 10; ++i ) {
         g.drawLine( width, height, i * width / 10, 0 );
      }  
      System.out.println("Running in paint method.");  
   }
}//end of inner class   
}  

根窗格為空,因為尚未將Jpanel添加到任何組件。 並向面板根窗格中添加諸如此類的內容..非常臟。

將方法設置為:

 private void createGUI(){
        TestPanel myPanel = new TestPanel();
        getContentPane().add(myPanel);
    }

和類TestPanel作為

public class TestPanel extends JPanel{
    TestPanel(){
        super();
        System.out.println("Running in constructor.  ");
        JLabel myLabel = new JLabel("Hello World");
        add(myLabel);
        System.out.println("Still running in constructor.  ");
    }
}

讓我們從頭開始...

public class TestPanel extends JPanel{
    TestPanel(){
        System.out.println("Running in constructor.  ");
        JLabel myLabel = new JLabel("Hello World");
        getRootPane().add(myLabel);
        System.out.println("Still running in constructor.  ");
    }
}

使用getRootPane向其中添加組件是錯誤的事情。 您永遠不需要在根窗格中添加任何內容。 相反,您應該使用內容窗格,但這不是您要嘗試執行的操作(或應該在此上下文中執行)。

相反,您只需調用add

public class TestPanel extends JPanel{
    TestPanel(){
        System.out.println("Running in constructor.  ");
        JLabel myLabel = new JLabel("Hello World");
        add(myLabel);
        System.out.println("Still running in constructor.  ");
    }
}

然后將標簽添加到TestPane

讓我們來看看擴展名...

public class TestPanel extends JPanel{
    DrawingLines myDrawingLines = new DrawingLines();  

    TestPanel(){
        System.out.println("Running in constructor.  ");
        JLabel myLabel = new JLabel("Hello World");
        this.add(myLabel);
        this.add(myDrawingLines);  
        myDrawingLines.repaint();  
        System.out.println("Still running in constructor.  ");
    }

    //inner class to override paint method
    class DrawingLines extends Canvas{
       int width, height;

       public void paint( Graphics g ) {
          width = getSize().width;
          height = getSize().height;
          g.setColor( Color.green );
          for ( int i = 0; i < 10; ++i ) {
             g.drawLine( width, height, i * width / 10, 0 );
          }  
          System.out.println("Running in paint method.");  
       }
    }//end of inner class   
}  

首先,您應該避免混合笨重的組件(將Canvas放在JPanel ),這不值得它造成麻煩。

無需在構造函數中調用repaint 在調用構造函數時,無論如何都無法繪制組件。

相反,只需重寫面板的paintComponent方法

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    int width = getWidth;
    int height = getHeight();
    g.setColor( Color.green );
    for ( int i = 0; i < 10; ++i ) {
     g.drawLine( width, height, i * width / 10, 0 );
    }  
}

我強烈建議您看看

暫無
暫無

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

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