【问题标题】:Displaying issues with GridBagLayout显示 GridBagLayout 的问题
【发布时间】:2015-11-19 11:29:07
【问题描述】:

我正在使用 Swing 用 Ja​​va 编写一个简单的计算器,但在使用 GridBagLayout 时遇到了问题。

以下是我对组件进行切片的方式:

如您所见,cos sin tan % log sqrt sqr 和 pi 按钮不应远离其他按钮。

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

按钮以我希望它们显示的方式显示,但 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();
    }

谁能帮帮我?

提前感谢您的回答。

【问题讨论】:

标签: java swing jscrollpane layout-manager gridbaglayout


【解决方案1】:

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

默认布局管理器是框架的 BorderLayout,我会坚持使用它。

然后您可以使用 GridBagLayout 创建一个“按钮面板”并使用以下方法添加面板:

frame.add(buttonPanel, BorderLayout.LINE_END);

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

  1. 将顶部标签添加到 BorderLayout.PAGE_START
  2. 将表格添加到 BorderLayout.CENTER
  3. 将底部标签添加到 BorderLayout.PAGE_END

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

frame.add(leftPanel, BorderLayout.CENTER);

现在框架中所有多余的空间都将放到桌子上。

【讨论】:

    【解决方案2】:

    我认为可能有两个问题:

    1. 尝试将表格滚动的拉伸宽度和高度从0.1设置为1.0(大于零)并将拉伸策略设置为GridBagLayout.BOTH
    2. 设置minPrefferedHeight()minPrefferedWidth()JScrollPane 是表容器。

    【讨论】:

    • Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?(是的。)剩下的似乎真的更适合发表评论。
    • 选择权在你。一些 Swing 组件具有“奇怪”的行为,对于这些情况,java api 提供了许多方法,我们必须使用它们来避免讨厌它们;)
    • 您错过了该线程的主要观点。答案提到,在极少数情况下您需要更改这些值,根据需要@Override 相关方法通常是更好的设计。调用set..Size 方法,我们通常只是随机猜测所需的数字。
    猜你喜欢
    • 2014-02-23
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多