Java 杰帕内尔元';布局为空时不显示组件

Java 杰帕内尔元';布局为空时不显示组件,java,swing,layout-manager,null-layout-manager,Java,Swing,Layout Manager,Null Layout Manager,我正在尝试创建一个GUI,我想在某些地方放置元素。我把面板的布局设为空,所以我可以这样做。但是,当面板为空时,将不会显示任何内容以下是代码: public class OverView extends JFrame { //height and width of screen Toolkit tk = Toolkit.getDefaultToolkit(); int x = ((int) tk.getScreenSize().getWidth());//length o

我正在尝试创建一个GUI,我想在某些地方放置元素。我把面板的布局设为空,所以我可以这样做。但是,当面板为空时,将不会显示任何内容
以下是代码:

public class OverView extends JFrame {

    //height and width of screen
    Toolkit tk = Toolkit.getDefaultToolkit();
    int x = ((int) tk.getScreenSize().getWidth());//length of screen
    int y = ((int) tk.getScreenSize().getHeight());//height

    //components
    private JLabel title;
    private JLabel description;
    private JPanel panel;
    private ArrayList<JButton> farms;

    //farm variables
    public ArrayList<Farm> owned;


    public OverView(ArrayList<Farm> owned) {
        super("The Lolipop Farm - Overview");
        setSize(700, 700);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);

        //initialize variables
        this.owned = owned;
        panel = new JPanel();
        panel.setLayout(null);
        title = new JLabel("<html>Your Farms - The Lolipop Farm"
                + "<br> <font size=1000> <i> An Eph Production </i> </font></html>");

        //set background color, color, and font of JComponents
        title.setFont(new Font("serif", Font.BOLD, 25));
        title.setBackground(Color.GRAY);
        title.setOpaque(true);

        //set size and location of the components
        title.setSize(350, 120);
        title.setLocation(x / 2, 600);

        //add to panel
        panel.add(title);

        //add panel to the screen
        add(panel);         

    }

}
public类概述扩展了JFrame{
//屏幕的高度和宽度
Toolkit tk=Toolkit.getDefaultToolkit();
int x=((int)tk.getScreenSize().getWidth());//屏幕长度
int y=((int)tk.getScreenSize().getHeight());//高度
//组成部分
私人产权;
私有JLabel描述;
私人JPanel小组;
私营ArrayList农场;
//农场变量
公有上市公司;
公共概览(ArrayList拥有){
超级(“Lolipop农场-概述”);
设置大小(700700);
setExtendedState(JFrame.MAXIMIZED_两者);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(空);
//初始化变量
这个。拥有的=拥有的;
panel=新的JPanel();
panel.setLayout(空);
title=新的JLabel(“你的农场-Lolipop农场”
+“
生产Eph”); //设置JComponents的背景色、颜色和字体 title.setFont(新字体(“衬线”,Font.BOLD,25)); 标题.挫折背景(颜色.灰色); 标题.设置不透明(真); //设置组件的大小和位置 标题.设置大小(350、120); 标题.设置位置(x/2600); //添加到面板 小组.添加(标题); //将面板添加到屏幕 添加(面板); } }

当布局为空时,为什么面板没有显示任何内容?

由于概览是一个框架,我认为您必须调用该方法

setVisible(true);
根据以使其可见。
现在,如果这不起作用,我想知道您是否在代码的其他地方或在Main方法中创建了Overview类的实例。如果没有,则没有对象可以显示类内部的面板,因此程序不会显示任何内容

您的问题在于代码

setLayout(null);
这将把JFrame的布局设置为null,因为您正在扩展(继承它)。您必须拥有JFrame的布局,尽管您可以不使用JPanel的布局。只要去掉那条线就好了

编辑:
当然,你需要像另一个人说的那样调用setVisible(true)。

“我想在某些地方放置元素”Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。相反,使用布局管理器,或与布局填充和边框一起使用。以最小尺寸提供ASCII艺术或GUI预期布局的简单绘图,如果可调整大小,则具有更大的宽度和高度。“现在,如果不起作用…”。“如果没有,那么就没有对象可以显示类内部的面板,这样程序就不会显示任何内容。”即使框架设置为可见,也没有面板,原因很简单,我觉得最好不要显示。我已将框架设置为可见,并且在另一个类中创建了OverView实例。“我已将框架设置为可见…”最好在a/中作为旁白显示,上面的代码不会编译,即使有相关的导入,也包括运行它并在屏幕上显示框架所需的
main(String[])