Java swing集边界导致jpanel消失

Java swing集边界导致jpanel消失,java,swing,layout-manager,null-layout-manager,Java,Swing,Layout Manager,Null Layout Manager,我有一个JPanel,其中包含两个组件。我的布局管理器为null。当我设置两个组件的边界(我确信它们在范围内)时,它们将不可见或消失。我希望工具栏稍微薄一点,沿着顶部,并且字段填充该区域的其余部分。这是我的密码: import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener

我有一个JPanel,其中包含两个组件。我的布局管理器为
null
。当我设置两个组件的边界(我确信它们在范围内)时,它们将不可见或消失。我希望
工具栏
稍微薄一点,沿着顶部,并且
字段
填充该区域的其余部分。这是我的密码:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public final class IDE extends JPanel {
    private static final long serialVersionUID = -8258005554657842392L;
    private final Toolbar toolbar;
    private final TextEdit field;

    public IDE() {
        this.setLayout(null);
        this.toolbar = new Toolbar();
        this.add(this.toolbar);
        this.field = new TextEdit();
        this.add(this.field);
    }

    private static final class Toolbar extends JPanel {
        private static final long serialVersionUID = -7947843173691641047L;

        public Toolbar() {
            try {
                this.setBackground(Color.BLACK);
                Image run = ImageIO.read(new File("buttons/run.png"));
                JButton runButton = new JButton(new ImageIcon(run));
                runButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Building");
                    }
                });
                runButton.setBorder(BorderFactory.createEmptyBorder());
                this.add(runButton);
                System.out.println("Toolbar init");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static final class TextEdit extends JPanel {
        private static final long serialVersionUID = -7089210936444479729L;

        public void paintComponent(Graphics g) {

        }
    }

    public void validate() {
        super.validate();
        this.toolbar.setBounds(0, 0, this.getWidth(), 50);
        this.field.setBounds(0, 50, this.getWidth(), this.getHeight() - 50);
    }

    public static void main(String... args) {
        JFrame frame = new JFrame("[NAME] IDE");
        frame.add(new IDE());
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

请帮助使这些组件可见。

为什么要使用
null
布局?我不认为我是唯一一个会建议像瘟疫一样避免它的人…使用布局。你不会有这种问题的。希望工具栏有点薄,沿顶部,字段填充区域的其余部分:对于工具栏位于北部,字段位于中心的BorderLayout来说,这是一个完美的用例。使用布局管理器,或者为无尽的痛苦做好准备,并为无尽的评论告诉您使用它们做好准备。@GIRISHRAMNANI,您不使用setSize(…)。这是布局管理器的工作。@GIRISHRAMNANI、setBounds()和setSize()在使用空布局时做同样的事情。然后,问题是这个类的整个结构,我在评论中指出了这一点。