有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将自定义JPanel添加到我的JFrame时,不会显示任何内容

我正在做我的第一个Java Swing应用程序,我正在使用Windows Builder来实现这一点

我有一个扩展JFrame的大型机

public class MainFrame extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainFrame frame = new MainFrame();
                    frame.setVisible(true);


                    PanelTest panelTest = new PanelTest();
                    frame.getContentPane().add(panelTest);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MainFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
    }

    }

然后我在另一节课上有我的JPanel

package gui;

public class PanelTest extends JPanel {

    /**
     * Create the panel.
     */
    public PanelTest() {

        setBounds(100, 100, 1225, 835);
        //getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        //getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);
        {
            JLabel lblCreacion = new JLabel("Hello");
            lblCreacion.setBounds(180, 16, 427, 20);
            contentPanel.add(lblCreacion);
        }
    }

我不明白,这个面板在Windows Builder中运行得很好,但一旦我添加它,什么都不会出现,JFrame中已经有了一个布局,所以我不知道缺少了什么

谢谢


共 (0) 个答案