首页 文章

显示GridBagLayout的问题

提问于
浏览
-1

我正在使用Swing在Java中编写一个简单的计算器,我遇到了GridBagLayout的问题 .

这是我如何切割我的组件:

my window

enter image description here

你可以看到,cos sin tan%log sqrt sqr和pi按钮不应该远离其他按钮 .

此外,当我在程序启动时缩小窗口大小时会发生这种情况:

enter image description here

按钮以我希望它们显示的方式显示,但JTable显示奇怪 .

这是关于主窗口的一些信息:

首选尺寸:600 * 600 Jtable:未设置尺寸信息

并且每个组件在GridBagConstraints对象中都将其权重属性设置为0(我认为它可能来自此

编辑:一些代码!这是我用来初始化按钮的枚举,所以你可以看到GridBagConstraints .

//new GridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, 
//int anchor, int fill, Insets insets, int ipadx, int ipady);
ZERO("0", 96, new GridBagConstraints(5,5,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        ONE("1", 97, new GridBagConstraints(4,4,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        TWO("2", 98, new GridBagConstraints(5,4,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        THREE("3", 99, new GridBagConstraints(6,4,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        FOUR("4", 100, new GridBagConstraints(4,3,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        FIVE("5", 101, new GridBagConstraints(5,3,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        SIX("6", 102, new GridBagConstraints(6,3,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        SEVEN("7", 103, new GridBagConstraints(4,2,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        EIGHT("8", 104, new GridBagConstraints(5,2,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        NINE("9", 105, new GridBagConstraints(6,2,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        DOT(".", 110, new GridBagConstraints(6,5,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        PLUS("+", 107, new GridBagConstraints(7,2,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        MINUS("-", 109, new GridBagConstraints(7,3,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        DIVIDE("/", 111, new GridBagConstraints(7,5,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        MULTIPLY("*", 106, new GridBagConstraints(7,4,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        MODULO("%", 0, new GridBagConstraints(7,6,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        EQUAL("=", 10, new GridBagConstraints(4,5,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        CLEAR("C", 67, new GridBagConstraints(7,1,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        BACKSPACE("Bck", 8, new GridBagConstraints(7,0,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        CLEARHISTORY("Clear history", 72, new GridBagConstraints(0,7,4,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        COSINUS("cos", 72, new GridBagConstraints(4,6,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        SINUS("sin", 72, new GridBagConstraints(5,6,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        TANGENT("tan", 72, new GridBagConstraints(6,6,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        SQUAREROOT("sqrt", 72, new GridBagConstraints(5,7,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        SQUARE("sqr", 72, new GridBagConstraints(6,7,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        LOGARITHM("log", 72, new GridBagConstraints(4,7,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        PLUSMINUS("±", 72, new GridBagConstraints(7,7,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));

这是我初始化视图的代码:

CALCULATIONSHISTORYLABEL(new GridBagConstraints(0,0,4,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)),
        CALCULATIONSHISTORYTABLE(new GridBagConstraints(0,1,4,6,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)),
        CURRENTCALCULATIONLABEL(new GridBagConstraints(4,0,3,1,0,0,GridBagConstraints.LINE_END,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)),
        CURRENTRESULTLABEL(new GridBagConstraints(4,1,3,1,0,0,GridBagConstraints.LINE_END,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));

/*...*/

    public View() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("simpleCount");
        setPreferredSize(new Dimension(800, 800));
        setLayout(new GridBagLayout());
        calculationsHistoryView = new JTable();

        GridBagConstraints c = new GridBagConstraints();
        add(new JLabel("Calculations History"), ComponentPlacement.CALCULATIONSHISTORYLABEL.gbc);
        add(new JScrollPane(calculationsHistoryView), ComponentPlacement.CALCULATIONSHISTORYTABLE.gbc);

        add(currentCalculationView, ComponentPlacement.CURRENTCALCULATIONLABEL.gbc);
        add(currentResultView, ComponentPlacement.CURRENTRESULTLABEL.gbc);
        for (int i = 0; i < Button.values().length; i++) {
            add(buttonsView.getButtons()[i], Button.values()[i].gbc);
        }
        pack();
        setVisible(true);
        requestFocus();
    }

谁能帮助我?

提前感谢您的回答 .

2 回答

  • 3

    不要对整个框架使用单个布局管理器 .

    默认的布局管理器是框架的BorderLayout,我会坚持这一点 .

    然后,您可以使用GridBagLayout创建“按钮面板”,并使用以下命令添加面板:

    frame.add(buttonPanel, BorderLayout.LINE_END);
    

    然后你可以创建另一个“左面板”,它可能再次使用BorderLayout . 那你可以

    • 将顶部标签添加到BorderLayout.PAGE_START

    • 将表添加到BorderLayout.CENTER

    • 将底部标签添加到BorderLayout.PAGE_END

    最后将左侧面板添加到框架中:

    frame.add(leftPanel, BorderLayout.CENTER);
    

    现在框架中的所有额外空间都将转到表格中 .

  • -1

    我认为可能有两个问题:

    • 尝试将表格滚动的拉伸宽度和高度设置为0.1到1.0(大于零),并将拉伸策略设置为 GridBagLayout.BOTH

    • 设置 minPrefferedHeight()minPrefferedWidth() of JScrollPane 这是表容器 .

相关问题