Swing的相对和绝对定位问题

时间:2015-11-26 20:37:30

标签: java swing

我正在创建一个需要相对和绝对定位的GUI。我需要绝对定位(我相信),因为显示的元素动态变化,我不希望GUI一直调整大小。

注意:我正在尝试创建一个包含4行5个复选框的网格。

这是代码。

    final int MAXNUMBEROFLABELSPERROW = 5;
    final int defaultCheckBoxWidth = 60;
    final int defaultCheckBoxHeigth = 60;

    JLabel bigBlindRemoveLabel = new JLabel("Remove big blind numbers", SwingConstants.CENTER);

    JPanel buttonsPanel = new JPanel();
    buttonsPanel.setLayout(new BorderLayout());

    JPanel buttonsSubPanel = new JPanel();
    buttonsSubPanel.setLayout(new BorderLayout());

    removeButton = new JButton("Remove");
    removeButton.addActionListener(this);

    removeAllButton = new JButton("Remove All");
    removeAllButton.addActionListener(this);

    buttonsSubPanel.add(removeButton, BorderLayout.NORTH);
    buttonsSubPanel.add(removeAllButton, BorderLayout.SOUTH);

    buttonsPanel.add(buttonsSubPanel, BorderLayout.CENTER);
    buttonsPanel.setBorder(new EmptyBorder(50, 20, 50, 20));

    JPanel checkboxesSubPanel = new JPanel();
    checkboxesSubPanel.setLayout(null);

    // Create 20 check boxes and fill the list field with big blind numbers
    for (int index = 0; index <= TOTALNUMBEROFCHECKBOXES - 1; index++)
    {
        numberCheckBoxes[index] = new JCheckBox();
        numberCheckBoxes[index].setSize(defaultCheckBoxWidth, defaultCheckBoxHeigth);
        numberCheckBoxes[index].setSelected(false);
        numberCheckBoxes[index].setLocation(
                defaultCheckBoxWidth * (index % MAXNUMBEROFLABELSPERROW) + 10,
                (index / MAXNUMBEROFLABELSPERROW) * defaultCheckBoxHeigth + defaultCheckBoxHeigth);
        numberCheckBoxes[index].setVisible(false);

        checkboxesSubPanel.add(numberCheckBoxes[index]);
    }

    checkboxesSubPanel.setSize(
            numberCheckBoxes[TOTALNUMBEROFCHECKBOXES - 1].getX()
                    + numberCheckBoxes[TOTALNUMBEROFCHECKBOXES - 1].getWidth() + 20,
            numberCheckBoxes[TOTALNUMBEROFCHECKBOXES - 1].getY()
                    + numberCheckBoxes[TOTALNUMBEROFCHECKBOXES - 1].getHeight() + 20);

    resultsPanel.add(bigBlindRemoveLabel, BorderLayout.NORTH);
    resultsPanel.add(checkboxesSubPanel, BorderLayout.WEST);
    resultsPanel.add(buttonsPanel, BorderLayout.EAST);

    JFrame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(resultsPanel);
    frame.setVisible(true);
    frame.pack();

我在寻找什么(以绝对定位完成所有这些,我正在努力改进)

enter image description here

我看到的是上面发布的代码

enter image description here

我的问题是显示该部分,但如果我更改复选框文本(我试图避免),其大小会动态更改。另外,我不知道为什么,所有的复选框都显示在一行上(我之前使用了相同的代码,绝对定位没有问题)。提前谢谢!

2 个答案:

答案 0 :(得分:2)

  

我需要绝对定位(我相信),因为显示的元素会动态变化,我不希望GUI一直调整大小。

我想你看不到森林里的树木了。请记住,您不会只使用单个布局管理器,您可以使用复合容器/布局来实现一些非常复杂的布局。

您还可以使用布局管理器function fetchRule() { static $index = 0; $ret = null; if(isset($this->rules[$index])) { $ret = $this->rules[$index]; } $index = $ret == null ? 0 : $index++; return $ret; } 的功能,如果是按钮,则可以使用边距来调整控件和间距的大小。

Layout

EmptyBorder

答案 1 :(得分:0)

感谢有用的反馈,能够通过使用网格布局找到我的答案,并使用numberCheckBoxes [index] .setMinimumSize(new Dimension(2,0))设置复选框最小尺寸;

再次感谢您的帮助!

相关问题