【问题标题】:The layout makes the JFrame minimal and there is no content布局使 JFrame 最小化并且没有内容
【发布时间】:2015-12-03 02:33:24
【问题描述】:

感谢Layout,我想创建一个包含两个JPanels 的JFrame。左边是结果(尽管有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 和许多其他来源。

【问题讨论】:

    标签: java swing jframe jpanel layout-manager


    【解决方案1】:

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

    public void JFrame() { ... }
    

    我想你打算写一个像这样的构造函数

    public Frame() { ... }
    

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

    希望这会有所帮助。

    【讨论】:

    • 对,我在最小化示例时犯了这个错误。这个问题现在更新了add(button)。现在更有意义了,因为它的行为与我更大的代码不同。
    • @Qu'est-cet'yont: Tanmay 已经正确简洁地回答了你原来的问题 (1+),现在你有一个新问题。正确的 StackOverflow 礼仪是对 Tanmay 的回答进行投票和接受,然后在本网站上就您的新问题提出 问题。请不要现在就完全改变问题,并使 Tanmay 的努力和帮助无效。
    • 对不起,我认为问题对其他人有用。由于这个问题有一些无趣的错误(当我简化代码时添加了 no 让其他人可以使用),我认为我必须纠正它以使其符合逻辑。不确定我收到的 -1 是否相关,因为我做出了真正的努力以使问题变得舒适。
    • @Qu'est-cet'yont:-1 用于使 Tanmay 的所有工作无效。明白他和我们所有人都是志愿者,有人试图帮助你并不是很好,只是让他的努力和帮助无效。
    • @Tanmay Patil Frame 是 awt.Frame 的保留词,与 swing.JFrame 相同,可能类似于 MyFrame ......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多