摆动全尺寸JPanel

时间:2013-10-17 07:56:56

标签: java swing jpanel

如何达到outerPanel的全宽?

    private JPanel createLabelPanel() {

        final JToolTip tt = new JToolTip();
        //tt.setSize(new Dimension(100,200));
        final CompoundBorder cb = new CompoundBorder(tt.getBorder(), BorderFactory.createEmptyBorder(2, 2, 2, 2));
        final JPanel innerPanel = new JPanel();
        innerPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        innerPanel.setBorder(cb);
        innerPanel.setBackground(tt.getBackground());
        innerPanel.add(displayLabel);
        innerPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN));

        final JScrollPane tooltipscrool = new JScrollPane(innerPanel);
        //tooltipscrool.setMinimumSize(new Dimension(200,100));
        tooltipscrool.setPreferredSize(new Dimension(100,120));
        tooltipscrool.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        final JPanel outerPanel = new JPanel();
        outerPanel.add(tooltipscrool);
        outerPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN));
        //outerPanel.set
        return outerPanel;
    }

让我:

enter image description here

我的目标是拥有一个全宽的innerPanel(绿色)。

1 个答案:

答案 0 :(得分:1)

JPanel默认使用FlowLayout,这倾向于使用每个组件的首选大小来定义其大小。

而是使用BorderLayout,例如......

final JPanel outerPanel = new JPanel(new BorderLayout());

请查看How to layout components within a container了解详情

您还应避免在可能的情况下使用setPreferredSize,请查看Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?了解详情