简体   繁体   中英

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. Hope you can tell me what I'm doing wrong.

You have to add the Panel to your JFrame . You can do so by: main.getContentPane().add(panel);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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