java - 布局使 JFrame 最小化并且没有内容

标签 java swing jframe jpanel layout-manager

由于 Layout,我想制作一个包含两个 JPanelJFrame。左边是结果(尽管有 setExtendedState(JFrame.MAXIMIZED_BOTH);),右边是调整框架大小时的结果:

Screen of the result

这是最小化的代码:

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * The Frame which will contain one or two Panels.
 *
 */
class Frame extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel panAction;

    public void JFrame() {
        setLayout(new GridBagLayout());

        GridBagConstraints info = new GridBagConstraints();
        info.gridx = info.gridy = 0;
        //info.gridwidth = 1;
        //info.gridheight = 2;
        info.fill = GridBagConstraints.BOTH;
        //info.weightx = 1;
        //info.weighty = 1;
        JPanel buttonPanel = new ButtonPanel(this);
        add(buttonPanel, info);

        setExtendedState(JFrame.MAXIMIZED_BOTH);
        pack();
    }

    public void changeSecondPanel(JPanel panel) {
        if(this.panAction != null) {
            remove(this.panAction);
        }

        GridBagConstraints info = new GridBagConstraints();
        info.gridx = 0;
        info.gridy = 1;
        //info.gridwidth = 1;
        //info.gridheight = 2;
        //info.weightx = 1;
        //info.weighty = 1;
        info.fill = GridBagConstraints.BOTH;

        add(panAction, info);
        this.panAction = panel;
    }
}

/**
 * The upper Panel.
 *
 */
class ButtonPanel extends JPanel {

    private static final long serialVersionUID = 1L;

    public ButtonPanel(final Frame frame) {
        setBackground(Color.BLUE);
        JButton button = new JButton("CREATE");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                frame.changeSecondPanel(
                        /**
                         * The bottom Panel.
                         */
                        new JPanel() {

                    private static final long serialVersionUID = 1L;

                    {
                        setBackground(Color.GREEN);
                    }
                });
            }
        });
    }
}


public class PanelProblem {

    public static void main(String[] args) {
        new Frame().setVisible(true);
    }

}

我尝试了 GridLayoutBorderLayout 但它没有解决我的问题。我已经检查过Can components of a gridbaglayout fill parent frame upon resize? , GridBagLayout doesn't fill all the space以及许多其他来源。

最佳答案

名为 JFramevoid 方法看起来很可疑。

public void JFrame() { ... }

我认为你打算编写一个像这样的构造函数

public Frame() { ... }

您编写的布局代码没有被调用。此修复将解决该问题。

希望这有帮助。

关于java - 布局使 JFrame 最小化并且没有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32440313/

相关文章:

java - Java 的最佳缓存框架

java - JFrame 的半透明加载覆盖

java - JFrame 中的 NullPointerException

java - INetAddress 直接实例化

java - 有效获取 SOAPBody 长度

java - 为什么我无法在运行 JAR 之前加载所需的库?

java - 在Java中,如何更改按钮的颜色,等待几秒钟,然后再次更改?

java - 对 unicode 数字进行数学运算

java - 在 Windows LAF 下为每个组件的 JTextField 设置非 Activity 背景颜色

java - 重新加载 JFrame 或 JLabel