Elements not showing in GridBagLayout

六眼飞鱼酱① 提交于 2019-11-27 08:40:29

问题


I am using GridBagLayout as JFrame layout. My elements are not showing no matter what i write. Please don't give answers which use anything but GridBagLayout(sorry if it sound rude)

JPanel Panel;
    JButton insertButton = new JButton("Insert");
    GridBagConstraints gbc;

    public MainFrame() {

        this.setTitle("JAVA & MySQL");
        this.setVisible(true);
        this.setBounds(500, 100, 600, 600);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        Panel = new JPanel(new GridBagLayout());
        Panel.setOpaque(true);
        Panel.setBackground(Color.BLUE);
        gbc = new GridBagConstraints();

        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(0, 10, 0, 0);
        gbc.fill = GridBagConstraints.BOTH;

        Panel.add(insertButton, gbc);




    }

回答1:


The problem is you didn't add the panel to the top-level container's content pane. Call this.add(panel) to make it happen.

Besides, as @MaddProgrammer said in his comment, you should call pack() and setVisible() methods after adding all the components, otherwise you have to call revalidate() and repaint() in order to validate the components hierarchy.

As per Container#add(Component comp) documentation:

This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to display the added component.

In addition you shouldn't mix absolute layout calls such as setBounds(), setLocation() or setSize() (just avoid them) and layout managers (these ones are highly recommended)




回答2:


Your code is not clear whats where (is this code inside your frame class) and you have chosen names poorly. By convention field names should start witha lower case letter (to distinguish them from class names).

It appears you never add your panel to the frame, and also you never pack() the frame.

Alter the code like this:

public MainFrame() {
    this.setTitle("JAVA & MySQL");
    // setting visible should come last!
    //this.setVisible(true);
    this.setBounds(500, 100, 600, 600);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    Panel = new JPanel(new GridBagLayout());
    Panel.setOpaque(true);
    Panel.setBackground(Color.BLUE);
    gbc = new GridBagConstraints();

    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.insets = new Insets(0, 10, 0, 0);
    gbc.fill = GridBagConstraints.BOTH;

    Panel.add(insertButton, gbc);

    // put the panel into the frame!
    setLayout(new BorderLayout());
    add(Panel, BorderLayout.CENTER);
    pack();
    setVisible(true);
}


来源:https://stackoverflow.com/questions/35187039/elements-not-showing-in-gridbaglayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!