简体   繁体   English

在JFrame上看不到JPanel

[英]Cannot see JPanel on JFrame

I am trying to add a JPanel onto the JFrame but I cannot see the panel. 我试图将JPanel添加到JFrame上,但是看不到面板。 I also am trying to add a JTable to the panel but after I can get this working. 我也试图将JTable添加到面板中,但是在我可以使它工作之后。 Here is my code: 这是我的代码:

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

public class table
{

    public table(){

        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.setSize(500,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        frame.add(panel, BorderLayout.CENTER);
        frame.setVisible(true);

    }
}

In another class, I have the code: 在另一堂课中,我有代码:

public class Main {

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

}

Here is a screenshot of the JFrame with no JPanel (I think). 这是没有JPanel的JFrame的屏幕截图(我认为)。 这是JFrame的屏幕截图。

If you take a look at the docs for Window.setVisible() , you might notice a note about the input parameter that says 如果您查看Window.setVisible()的文档,则可能会注意到有关输入参数的注释,内容为

The Window will be validated prior to being made visible. 在使窗口可见之前,将对其进行验证。

Validation is the key to making new components show up when you add them to a window. 验证是将新组件添加到窗口中时使其显示出来的关键。 You have two options: 您有两种选择:

  1. Move the call to setVisible to the end of your code, once everything is added to the window: 将所有内容添加到窗口后,将对setVisible的调用移至代码末尾:

    \nJFrame frame = new JFrame(); JFrame框架=新的JFrame();\nframe.setLayout(new BorderLayout()); frame.setLayout(new BorderLayout());\nframe.setSize(500,300); frame.setSize(500,300);\nframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n\nJPanel panel = new JPanel(); JPanel面板= new JPanel();\nframe.add(panel, BorderLayout.CENTER); frame.add(panel,BorderLayout.CENTER);\nframe.setVisible(true); frame.setVisible(true);\n
  2. Re-validate the frame yourself after adding the panel: 添加面板后,请自己重新验证框架:

    \nJFrame frame = new JFrame(); JFrame框架=新的JFrame();\nframe.setLayout(new BorderLayout()); frame.setLayout(new BorderLayout());\nframe.setSize(500,300); frame.setSize(500,300);\nframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\nframe.setVisible(true); frame.setVisible(true);\nJPanel panel = new JPanel(); JPanel面板= new JPanel();\nframe.add(panel, BorderLayout.CENTER); frame.add(panel,BorderLayout.CENTER);\nframe.validate(); frame.validate();\n

A note from the docs for Container.validate() : 文档中关于Container.validate()注释:

Validating the container may be a quite time-consuming operation. 验证容器可能是一个非常耗时的操作。 For performance reasons a developer may postpone the validation of the hierarchy till a set of layout-related operations completes, eg after adding all the children to the container. 出于性能原因,例如在将所有子项添加到容器之后,开发人员可能会推迟层次结构的验证,直到完成一组与布局相关的操作为止。

Using the first option is probably better. 使用第一个选项可能更好。

Clarification 澄清度

Keep in mind that the background color of a JPanel is the same as the background color of the JFrame 's content pane by default. 请记住,默认情况下, JPanel的背景色与JFrame内容窗格的背景色相同。 You will not be able to determine that it was added correctly visually. 您将无法确定它是在视觉上正确添加的。 At @WKS's suggestion, you can change the panel's color so you can see it clearly in the frame: 根据@WKS的建议,您可以更改面板的颜色,以便可以在框架中清楚地看到它:

JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setSize(500,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBackground(Color.RED);
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);

Don't forget to import java.awt.Color; 不要忘记import java.awt.Color; in this case. 在这种情况下。

尝试为面板提供背景颜色:

panel.setBackground(Color.BLUE);

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

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