在Java中如何在面板中使用网格布局?

在Java中如何在面板中使用网格布局?,java,swing,jframe,jpanel,grid-layout,Java,Swing,Jframe,Jpanel,Grid Layout,我正在尝试创建一个带有两个面板的JFrame。顶部面板将包含一个文本字段,底部面板将包含一个按钮网格。我使用gridLayout来排列按钮,并将它们添加到面板中,然后将面板添加到JFrame中,但根据编辑器,panel的值为NULL。第二个面板也是如此。有人能告诉我这个问题吗 import java.awt.*; import javax.swing.*; public class frameClass extends JFrame{ private static final long

我正在尝试创建一个带有两个面板的JFrame。顶部面板将包含一个文本字段,底部面板将包含一个按钮网格。我使用gridLayout来排列按钮,并将它们添加到面板中,然后将面板添加到JFrame中,但根据编辑器,panel的值为NULL。第二个面板也是如此。有人能告诉我这个问题吗

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

public class frameClass extends JFrame{
    private static final long serialVersionUID = 1L;
private JFrame frame;

private JPanel panel;
private JPanel panel2;

private JButton button0;
private JButton button1;
private JButton button2;
private JButton button3;

public frameClass() {
    panel = new JPanel(new GridLayout(4,4,5,5));            

    panel.setBackground(Color.BLACK);

    Font font1 = new Font("SanSerif",Font.BOLD, 16);

    button0 = new JButton("0");
    button0.setFont(font1);
    button0.setBackground(Color.BLACK);
    button0.setForeground(Color.WHITE);
    panel.add(button0);

    button1 = new JButton("1");
    button1.setFont(font1);
    button1.setBackground(Color.WHITE);
    panel.add(button1);

    button2 = new JButton("2");
    button2.setFont(font1);
    button2.setBackground(Color.BLACK);
    button2.setForeground(Color.WHITE);
    panel.add(button2);

    button3 = new JButton("3");
    button3.setFont(font1);
    button3.setBackground(Color.WHITE);
    panel.add(button3);

            frame.add(panel);
            panel2 = new JPanel(new BorderLayout());
    panel2.add(new JTextField(21), BorderLayout.CENTER);

    frame.add(panel2);
    frame.setVisible(true);
}
public static void main(String[] args) {
    new calculator();       
}
}
“核心”问题不是面板的为空,而是
frame
为空

// You extend from JFrame, which isn't highly recommended
// but you seem to ignore...
public class FrameClass extends JFrame {

    private static final long serialVersionUID = 1L;
    // Instance variable of frame, but it's never initialised...
    private JFrame frame;

    private JPanel panel;
    private JPanel panel2;

    //...

    public FrameClass() {
        // Create panel...
        panel = new JPanel(new GridLayout(4, 4, 5, 5));
        panel.setBackground(Color.BLACK);

        //...

        // Add it to frame, but frame is null...
        frame.add(panel);

        //...
    }
相反,请尝试删除
扩展JFrame
并实例化
JFrame

public class FrameClass {

    private static final long serialVersionUID = 1L;
    private JFrame frame;

    private JPanel panel;
    private JPanel panel2;

    //...

    public FrameClass() {
        panel = new JPanel(new GridLayout(4, 4, 5, 5));

        //...

        frame = new JFrame("Testing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(panel);
        panel2 = new JPanel(new BorderLayout());
        panel2.add(new JTextField(21), BorderLayout.CENTER);

        frame.add(panel2);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
“核心”问题不是面板的为空,而是
frame
为空

// You extend from JFrame, which isn't highly recommended
// but you seem to ignore...
public class FrameClass extends JFrame {

    private static final long serialVersionUID = 1L;
    // Instance variable of frame, but it's never initialised...
    private JFrame frame;

    private JPanel panel;
    private JPanel panel2;

    //...

    public FrameClass() {
        // Create panel...
        panel = new JPanel(new GridLayout(4, 4, 5, 5));
        panel.setBackground(Color.BLACK);

        //...

        // Add it to frame, but frame is null...
        frame.add(panel);

        //...
    }
相反,请尝试删除
扩展JFrame
并实例化
JFrame

public class FrameClass {

    private static final long serialVersionUID = 1L;
    private JFrame frame;

    private JPanel panel;
    private JPanel panel2;

    //...

    public FrameClass() {
        panel = new JPanel(new GridLayout(4, 4, 5, 5));

        //...

        frame = new JFrame("Testing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(panel);
        panel2 = new JPanel(new BorderLayout());
        panel2.add(new JTextField(21), BorderLayout.CENTER);

        frame.add(panel2);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

为了更快地获得更好的帮助,请发布。您似乎试图使用从未声明过的
panel1
变量,而您声明了从未使用过的
panel2
变量。这段代码是否编译?引用的答案是:你确定这段代码是最新的吗?您有
panel1.add(新的JTextField(21),BorderLayout.CENTER)
,但看起来您确实声明了
私有JPanel panel2。这在您的应用程序中吗?最新编辑的代码仍然不能干净地编译。因此,不可能是运行时问题的SSCCE。为了更快地获得更好的帮助,请发布一个。您似乎试图使用从未声明过的
panel1
变量,并且声明了从未使用过的
panel2
变量。这段代码是否编译?引用的答案是:你确定这段代码是最新的吗?您有
panel1.add(新的JTextField(21),BorderLayout.CENTER)
,但看起来您确实声明了
私有JPanel panel2。这在您的应用程序中吗?最新编辑的代码仍然不能干净地编译。因此,不能成为运行时问题的SSCCE。谢谢!我现在明白了,在填充框架之前,我忘了实例化它。此外,我没有把两个面板都放在框架中,而是用按钮将包含JTextField的面板放在面板中,并将边框布局设置为北。谢谢!我现在明白了,在填充框架之前,我忘了实例化它。此外,我没有将两个面板都放在框架中,而是用按钮将包含JTextField的面板放在面板中,并将边界布局设置为north。