BoxLayout как предотвратить растяжение вложенных

 
0
 
Java
ava
Mirkes | 23.09.2013, 16:41
День добрый.

Мне нужно создать узкое окно для легенды к рисункам. Легенда как правило состоит из нескольких подпанелей и создается и меняется динамически.
Я создаю ранель легенд и размещаю его в восточную часть панели (BorderLayout).

Подпанели легенды состоят из одного - двух столбцов и я делаю их в GridBagLayout.

Все бы ничего, но вот при изменении размеров вид становится малоприличный smile 

import javax.swing.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.*;

public class BoxLayoutTestGBL extends JFrame{

    public BoxLayoutTestGBL(){
        super("BoxLayout");
        final JPanel content = new JPanel();
        content.setBorder(BorderFactory.createLineBorder(Color.red));
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

    JPanel pan1 = new JPanel();
    pan1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Legend"));

    pan1.setLayout(new GridBagLayout());


        JLabel lbl = new JLabel("Label 1");
        lbl.setBorder(BorderFactory.createLineBorder(Color.blue));
        lbl.setFont(lbl.getFont().deriveFont(30f));
        pan1.add(lbl, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(5, 5, 5, 5), 0, 0));


        lbl = new JLabel("Label 2");
        lbl.setBorder(BorderFactory.createLineBorder(Color.blue));
        lbl.setFont(lbl.getFont().deriveFont(30f));
        pan1.add(lbl, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(5, 5, 5, 5), 0, 0));

    content.add(pan1);

    pan1 = new JPanel();
    pan1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Legend"));

    pan1.setLayout(new GridBagLayout());


        lbl = new JLabel("Label 3");
        lbl.setBorder(BorderFactory.createLineBorder(Color.blue));
        lbl.setFont(lbl.getFont().deriveFont(30f));
        pan1.add(lbl, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(5, 5, 5, 5), 0, 0));

        lbl = new JLabel("Label 4");
        lbl.setBorder(BorderFactory.createLineBorder(Color.blue));
        lbl.setFont(lbl.getFont().deriveFont(30f));
        pan1.add(lbl, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(5, 5, 5, 5), 0, 0));

    content.add(pan1);

        getContentPane().add(content, BorderLayout.EAST);
        setSize(410,220);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args){
        JFrame.setDefaultLookAndFeelDecorated(true);
        new BoxLayoutTestGBL().setVisible(true);
    }
}


А хотелось бы вот такого поведения:

import javax.swing.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.*;

public class BoxLayoutTest extends JFrame{

    public BoxLayoutTest(){
        super("BoxLayout");
        final JPanel content = new JPanel();
        content.setBorder(BorderFactory.createLineBorder(Color.red));
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

    JPanel pan1 = new JPanel();
    pan1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Legend"));

    pan1.setLayout(new BoxLayout(pan1, BoxLayout.Y_AXIS));


        JLabel lbl = new JLabel("Label 1");
        lbl.setBorder(BorderFactory.createLineBorder(Color.blue));
        lbl.setFont(lbl.getFont().deriveFont(30f));
    pan1.add(lbl);

        lbl = new JLabel("Label 2");
        lbl.setBorder(BorderFactory.createLineBorder(Color.blue));
        lbl.setFont(lbl.getFont().deriveFont(30f));
    pan1.add(lbl);
    content.add(pan1);

    pan1 = new JPanel();
    pan1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Legend"));

    pan1.setLayout(new BoxLayout(pan1, BoxLayout.Y_AXIS));


        lbl = new JLabel("Label 3");
        lbl.setBorder(BorderFactory.createLineBorder(Color.blue));
        lbl.setFont(lbl.getFont().deriveFont(30f));
    pan1.add(lbl);

        lbl = new JLabel("Label 4");
        lbl.setBorder(BorderFactory.createLineBorder(Color.blue));
        lbl.setFont(lbl.getFont().deriveFont(30f));
    pan1.add(lbl);

    content.add(pan1);

        getContentPane().add(content, BorderLayout.CENTER);
        setSize(410,220);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args){
        JFrame.setDefaultLookAndFeelDecorated(true);
        new BoxLayoutTest().setVisible(true);
    }
}


Заранее благодарен за совет smile
Comments (1)
ava
Mirkes | 24.09.2013, 20:25 #
Нашел некоторое решение, хотя возможно и не оптимальное. Но вроде работает. Вместо JPanel использую свой класс-обертку

    private class MyPanel extends JPanel{
      public Dimension getMaximumSize() {
        return new Dimension(Integer.MAX_VALUE,getPreferredSize().height);
      }
    }    


Вот полностью работающий образец.


import javax.swing.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.*;

public class BoxLayoutTestGBL extends JFrame{

    public BoxLayoutTestGBL(){
        super("BoxLayout");
        final JPanel content = new JPanel();
        content.setBorder(BorderFactory.createLineBorder(Color.red));
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

    MyPanel pan1 = new MyPanel();
    pan1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Legend"));

    pan1.setLayout(new GridBagLayout());


        JLabel lbl = new JLabel("Label 1");
        lbl.setBorder(BorderFactory.createLineBorder(Color.blue));
        lbl.setFont(lbl.getFont().deriveFont(30f));
//    pan1.add(lbl);
        pan1.add(lbl, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(5, 5, 5, 5), 0, 0));


        lbl = new JLabel("Label 2");
        lbl.setBorder(BorderFactory.createLineBorder(Color.blue));
        lbl.setFont(lbl.getFont().deriveFont(30f));
//    pan1.add(lbl);
        pan1.add(lbl, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(5, 5, 5, 5), 0, 0));

    content.add(pan1);

    pan1 = new MyPanel();
    pan1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Legend"));

    pan1.setLayout(new GridBagLayout());


        lbl = new JLabel("Label 3");
        lbl.setBorder(BorderFactory.createLineBorder(Color.blue));
        lbl.setFont(lbl.getFont().deriveFont(25f));
//    pan1.add(lbl);
        pan1.add(lbl, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(5, 5, 5, 5), 0, 0));

        lbl = new JLabel("Label 4");
        lbl.setBorder(BorderFactory.createLineBorder(Color.blue));
        lbl.setFont(lbl.getFont().deriveFont(25f));
//    pan1.add(lbl);
        pan1.add(lbl, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(5, 5, 5, 5), 0, 0));

    content.add(pan1);


//        content.add(Box.createHorizontalStrut(5));
//        content.add(Box.createHorizontalStrut(5));
//        content.add(Box.createHorizontalGlue());

        getContentPane().add(content, BorderLayout.EAST);
        setSize(410,220);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args){
        JFrame.setDefaultLookAndFeelDecorated(true);
        new BoxLayoutTestGBL().setVisible(true);
    }

    private class MyPanel extends JPanel{
      public Dimension getMaximumSize() {
        return new Dimension(Integer.MAX_VALUE,getPreferredSize().height);
      }
    }    
}

Please register or login to write.
Firm of day
Вы также можете добавить свою фирму в каталог IT-фирм, и публиковать статьи, новости, вакансии и другую информацию от имени фирмы.
Подробнее
Contributors
ava  Mirkes
advanced
Submit