Как вернуть XxxSize из JComponent(ов), добавленных в JLabel

как правильно вернуть XxxSize из JComponent(ов), добавленных в JLabel

1-й. рисунок >> lets LayoutManager работает как для JPanel, JLabel возвращает Size(0, 0)

enter image description here

2-й. рисунок >> добавил некоторые PreferredSize в JLabel

enter image description here

3-й. рисунок >> рассчитал PreferredSize из JComponent(ов), добавленных в JLabel

enter image description here

4-й. рисунок >> позволяет работать LayoutManager изменил JLabel на JPanel, теперь LayoutManager правильно вычислил Dimension без использования какого-либо XxxSize

enter image description here

обратите внимание, что поскольку здесь используется Nimbus L&F, тот же вывод есть для всех доступных L&F

import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.util.Queue;
import javax.swing.*;

public class NimbusBorderPainterDemo extends JFrame {

    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame();
    private JPanel fatherPanel = new JPanel(), titlePanel = new JPanel();
    private JLabel buttonPanel = new JLabel();


    //figure  ---> 4th. switch JLabel with JPanel
    //private JPanel buttonPanel = new JPanel();
    private Queue<Icon> iconQueue = new LinkedList<Icon>();

    public NimbusBorderPainterDemo() {
        iconQueue.add(UIManager.getIcon("OptionPane.errorIcon"));
        iconQueue.add(UIManager.getIcon("OptionPane.informationIcon"));
        iconQueue.add(UIManager.getIcon("OptionPane.warningIcon"));
        JButton button0 = createButton();
        JButton button1 = createButton();
        JButton button2 = createButton();
        button2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(1);
            }
        });
        int gap = 5;
        buttonPanel.setLayout(new GridLayout(0, 3, gap, 0));
        buttonPanel.add(button0);
        buttonPanel.add(button1);
        buttonPanel.add(button2);

        // figure 1st. --->  without PreferredSize

        // figure 2nd. --->
        //buttonPanel.setPreferredSize(new Dimension(160, 30));

        // figure 3rd. --->
        /*Dimension dim = button0.getPreferredSize();
        int w = dim.width;
        int h = dim.height;
        w = (w + 5) * 3;
        h += 4;
        dim = new Dimension(w, h);
        buttonPanel.setPreferredSize(dim);*/

        titlePanel.setLayout(new BorderLayout());
        titlePanel.add(new JLabel(nextIcon()), BorderLayout.WEST);
        titlePanel.add(new JLabel("My Frame"), BorderLayout.CENTER);
        titlePanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        titlePanel.add(buttonPanel, BorderLayout.EAST);
        fatherPanel.setLayout(new BorderLayout());
        fatherPanel.add(titlePanel, BorderLayout.CENTER);
        frame.setUndecorated(true);
        frame.add(fatherPanel);
        frame.setLocation(50, 50);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.setVisible(true);
    }

    private JButton createButton() {
        JButton button = new JButton();
        button.setBorderPainted(false);
        button.setBorder(null);
        button.setFocusable(false);
        button.setMargin(new Insets(0, 0, 0, 0));
        button.setContentAreaFilled(false);
        button.setIcon(nextIcon());
        //button.setRolloverIcon(nextIcon());
        //button.setPressedIcon(nextIcon());
        //button.setDisabledIcon(nextIcon());
        nextIcon();
        return button;
    }

    private Icon nextIcon() {
        Icon icon = iconQueue.peek();
        iconQueue.add(iconQueue.remove());
        return icon;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (Exception fail) {
                }
                UIManager.getLookAndFeelDefaults().put("nimbusFocus", Color.RED);
                NimbusBorderPainterDemo nimbusBorderPainterDemo = new NimbusBorderPainterDemo();
            }
        });
    }
}
7
задан mKorbel 20 December 2011 в 12:44
поделиться