简体   繁体   English

Java组件未显示在JFrame上

[英]Java components not showing on JFrame

I'm trying to make a program which gives an overview of equipment. 我正在尝试制作一个程序,概述设备。 My problem is that the frame isn't displaying anything at all. 我的问题是该框架根本不显示任何内容。 Here is my code: 这是我的代码:

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

public class Start {

    protected static JButton exit;
    protected static JFrame main;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                System.out.println("running");
                Start mf = new Start();
                mf.init();
            }
        });
    }

    public void init() {
        JPanel panel = new JPanel();
        main = new JFrame("Main menu");
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.getContentPane();
        main.setSize(750, 500);
        Dimension d = new Dimension(100, 50);
        exit = new JButton("Exit");
        exit.setPreferredSize(d);
        JLabel text1 = new JLabel("thingie");
        panel.add(exit);
        panel.add(text1);
        main.setVisible(true);
    }
}

I have been googling the issue, but most these issues are caused by people using setVisible(true) before they add their components, which I'm not doing. 我一直在搜索该问题,但是大多数这些问题是由人们在添加组件之前使用setVisible(true)引起的,而我没有这样做。 Hope you can tell me what I'm doing wrong. 希望你能告诉我我在做什么错。

You have to add the Panel to your JFrame . 您必须将Panel添加到您的JFrame You can do so by: main.getContentPane().add(panel); 您可以通过以下方法实现: main.getContentPane().add(panel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM