简体   繁体   English

我无法将jpanel添加到jframe

[英]I cannot add a jpanel to a jframe

I have the following code, but my JPanel just doesn't show. 我有以下代码,但我的JPanel却不显示。 I can't figure out why. 我不知道为什么。 Do you see why? 你明白为什么吗? All I see is the JFrame with black background 我只看到黑色背景的JFrame

public class ShapeFrame extends JFrame 
{
    private JPanel outlinePanel;

    public ShapeFrame(LinkedList<Coordinate> list)
    {
        super("Outline / Abstract Image");
        setSize(950, 500);
        setLayout(null);
        setBackground(Color.BLACK);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel outlinePanel = new JPanel();
        outlinePanel.setBackground(Color.WHITE);
        outlinePanel.setBorder(null);
        outlinePanel.setBounds(50, 50, 400, 400);
        add(outlinePanel);


//      abstractPanel = new JPanel();
//      abstractPanel.setBackground(Color.WHITE);
//      abstractPanel.setBounds(500, 50, 400, 400);
//      add(abstractPanel);
    }

All I get is a frame with a white square in it... 我所得到的只是一个带有白色正方形的框架...

You should use getContentPane().setBackground() to set the back ground of the frame 您应该使用getContentPane().setBackground()设置框架的背景

Frames are made up of layers. 框架由图层组成。 Typically, the content that you see is added (automatically in most cases) to the content pane, which covers the frame. 通常,您看到的内容会(在大多数情况下自动)添加到内容窗格中,该窗格将覆盖框架。

在此处输入图片说明

(Picture borrowed from the Java Trails) (图片来自Java Trails)

So setting the background of the frame "appears" to have no effect. 因此,设置框架的背景“似乎”无效。

Using your code... 使用您的代码...

在此处输入图片说明

Using getContent().setBackground(...) 使用getContent().setBackground(...)

在此处输入图片说明

This is the code I used to test your code with... 这是我用来测试您的代码的代码...

public class BadLayout01 {

    public static void main(String[] args) {
        new BadLayout01();
    }

    public BadLayout01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                ShapeFrame shapeFrame = new ShapeFrame();
                shapeFrame.setSize(525, 525);
                shapeFrame.setVisible(true);

            }
        });
    }

    public class ShapeFrame extends JFrame {

        private JPanel outlinePanel;

        public ShapeFrame() {
            super("Outline / Abstract Image");
            setSize(950, 500);
            setLayout(null);
            getContentPane().setBackground(Color.BLACK);
//            setBackground(Color.BLACK);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JPanel outlinePanel = new JPanel();
            outlinePanel.setBackground(Color.WHITE);
            outlinePanel.setBorder(null);
            outlinePanel.setBounds(50, 50, 400, 400);
            add(outlinePanel);


//      abstractPanel = new JPanel();
//      abstractPanel.setBackground(Color.WHITE);
//      abstractPanel.setBounds(500, 50, 400, 400);
//      add(abstractPanel);
        }
    }
}

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

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