2012-01-02 86 views
0

我看了很多網站。沒有面板,標籤顯示正確,面板出現錯誤:你如何在數組中創建JPanel並向其中添加Jlabel?

Exception in thread "main" java.lang.NullPointerException 

所以我能做些什麼來解決這個問題?

這裏是源代碼:

JLabel button[] = new JLabel[100]; 
JPanel[] panel = new JPanel[100]; 

    for (int i = 0; i < button.length; i++) { 
     a = a + 50; 

     if (a > 549) { 
      b = b + 50; 
      a = 50; 
     } 
     button[i] = new JLabel("hi"); 
     frame.add(button[i]); //is this necessary? 
     button[i].setVisible(true); // is this necessary? 
     button[i].setSize(50,50); 
     panel[i].add(button[i]); 
     panel[i].setVisible(true); 
     panel[i].setBounds(a, b, 50, 50); 
     frame.add(panel[i]); 

    } 

請告訴我錯,我怎麼能解決這個問題?只是你知道,它應該有100個標籤在10乘10陣列中打招呼。 這是什麼樣子: this is what it looks like

+0

你初始化框架變量?哪一個是ecxeption提升的路線? – 2012-01-02 08:48:15

+0

問題:panel [i] .add(button [i]);幀初始化如下:JFrame frame = new JFrame(D&D)\t \t JFrame.setDefaultLookAndFeelDecorated(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); \t \t frame.setBounds(0,25,1250,750); \t \t frame.setLayout(null); \t \t frame.setVisible(true); \t \t frame.setBackground(Color.black); \t \t frame.setResizable(false); – Russell 2012-01-02 08:51:17

+0

你的意思是'新的JFrame(「D&D」)? 'JFrame'的構造函數都沒有采用按位運算的結果。 – flesk 2012-01-02 09:06:07

回答

5

創建的JPanel數組僅創建陣列。它不會創建任何JPanel來填充陣列。陣列因此被填充爲nulls。必須爲陣列的每個元素創建一個JPanel:

panel[i] = new JPanel(); 
panel[i].add(button[i]); 

此外,組件可以只有一個祖先。該按鈕必須添加到框架或面板,但不能同時添加到兩者。如果您需要面板中的按鈕,則必須將其添加到面板中。

組件在默認情況下是可見的(除了必須使其可見的框架或對話框之類的頂級框架外)。您無需致電button.setVisible(true)

您應該學會使用佈局管理器,而不是明確地設置組件的大小和邊界。這是擁有漂亮,便攜式GUI應用程序的唯一方法。閱讀http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

+0

在他的代碼末尾有一個小包()不會受到傷害。 – Raveline 2012-01-02 08:58:54

+0

非常感謝,我不知道該陣列不會創建它們,謝謝。 – Russell 2012-01-02 09:03:21

3

不使用frame.setLayout(null);使用frame.setLayout(new GridLayout(10,10,10,10));代替,例如

import java.awt.*; 
import javax.swing.*; 

public class CustomComponent1 extends JFrame { 

    private static final long serialVersionUID = 1L; 

    public CustomComponent1() { 
     setTitle("Custom Component Test/GridLayout"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    public void display() { 
     setLayout(new GridLayout(10, 10, 10, 10)); 
     for (int row = 0; row < 100; row++) { 
      add(new CustomComponents1()); 
     } 
     //pack(); 
     // enforces the minimum size of both frame and component 
     setMinimumSize(getMinimumSize()); 
     setPreferredSize(getPreferredSize()); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 

      @Override 
      public void run() { 
       CustomComponent1 main = new CustomComponent1(); 
       main.display(); 
      } 
     }; 
     javax.swing.SwingUtilities.invokeLater(r); 
    } 
} 

class CustomComponents1 extends JLabel { 

    private static final long serialVersionUID = 1L; 

    @Override 
    public Dimension getMinimumSize() { 
     return new Dimension(20, 20); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(20, 20); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     int margin = 10; 
     Dimension dim = getSize(); 
     super.paintComponent(g); 
     g.setColor(Color.red); 
     g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2); 
    } 
} 
+0

它的作品,但後來在我的代碼中,我把另一個JLabel放在JLabel []上,所以需要面板,謝謝。 – Russell 2012-01-02 09:02:21

相關問題