简体   繁体   中英

Swing full size JPanel

How to reach a full width of 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;
    }

makes me:

在此处输入图片说明

and my aim is to have a full width of innerPanel (the green one).

JPanel uses a FlowLayout by default, which tends to what to use the preferred size of each component to define their size.

Instead use a BorderLayout , for example...

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

Take a look at How to layout components within a container for more details

You should also avoid using setPreferredSize where possible, check out Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM