簡體   English   中英

如何在數組中創建JPanels並向其中添加Jlabels?

[英]how do you create JPanels in an array and add Jlabels to it?

我看過很多網站。 沒有面板,標簽會正確顯示,帶有面板會顯示錯誤:

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 x 10的數組中打個招呼。 看起來是這樣的: 這是它的樣子

創建JPanel數組只會創建該數組。 它不會創建任何JPanel來填充數組。 因此,數組用null填充。 您必須為數組的每個元素創建一個JPanel:

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

而且,一個組件可能只有一個祖先。 該按鈕必須添加到框架或面板上,但不能同時添加到兩者上。 如果要在面板中顯示按鈕,則必須將其添加到面板中。

默認情況下,組件是可見的(除了頂級組件(如框架或對話框,必須使其可見))。 您不需要調用button.setVisible(true)

您絕對應該學會使用布局管理器,而不是明確設置組件的大小和界限。 這是擁有美觀,可移植的GUI應用程序的唯一方法。 閱讀http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

不要使用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);
    }
}

暫無
暫無

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

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