简体   繁体   English

如何在数组中创建JPanels并向其中添加Jlabels?

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

I've looked at many websites. 我看过很多网站。 Without the panels, the labels appear correctly, with the panels it give the the error: 没有面板,标签会正确显示,带有面板会显示错误:

Exception in thread "main" java.lang.NullPointerException   

so what can I do to fix this? 那我该怎么解决呢?

here is the source code: 这是源代码:

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]);

    }

Whats wrong with this, how can I fix it? 这有什么问题,我该如何解决? just so you know, it should have 100 labels that say hi in a 10 by 10 array. 请注意,它应该有100个标签,在10 x 10的数组中打个招呼。 this is what it looks like: 看起来是这样的: 这是它的样子

Creating an array of JPanel only creates the array. 创建JPanel数组只会创建该数组。 It doesn't create any JPanel to fill the array. 它不会创建任何JPanel来填充数组。 The arrays is thus filled with null s. 因此,数组用null填充。 You must create a JPanel for each element of the array: 您必须为数组的每个元素创建一个JPanel:

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

Moreover, a component may only have one ancestor. 而且,一个组件可能只有一个祖先。 The button must be added to the frame or to the panel, but not both. 该按钮必须添加到框架或面板上,但不能同时添加到两者上。 If you want the button in the panel, it must be added to the panel. 如果要在面板中显示按钮,则必须将其添加到面板中。

Components are visible by default (except top-level ones like frames or dialogs which must be made visible). 默认情况下,组件是可见的(除了顶级组件(如框架或对话框,必须使其可见))。 You don't need to call button.setVisible(true) . 您不需要调用button.setVisible(true)

You should definitely learn to use layout managers rather than setting the size and bounds of your components explicitely. 您绝对应该学会使用布局管理器,而不是明确设置组件的大小和界限。 That's the only way to have good-looking, portable GUI apps. 这是拥有美观,可移植的GUI应用程序的唯一方法。 Read http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html 阅读http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

don't use frame.setLayout(null); 不要使用frame.setLayout(null); use frame.setLayout(new GridLayout(10,10,10,10)); 使用frame.setLayout(new GridLayout(10,10,10,10)); instead, for example 相反,例如

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