如何从 JComponent(s) 返回 XxxSize 添加到 JLabel [英] How returns XxxSize from JComponent(s) added to the JLabel

查看:21
本文介绍了如何从 JComponent(s) 返回 XxxSize 添加到 JLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从添加到 JLabel 的 JComponent(s) 中正确返回 XxxSize

how can I correctly returns XxxSize from JComponent(s) added to the JLabel

第一.图 >> 让 LayoutManager 像 JPanel 一样工作,JLabel 返回 Size(0, 0)

第二.图>> 在JLabel中添加了一些PreferredSize

第三.图 >> 从添加到 JLabel 的 JComponent(s) 计算出的 PreferredSize

第四.图>> 让LayoutManager的工作改变JLabel为JPanel,现在LayoutManager正确计算Dimension而不使用任何XxxSize

注意这里使用了 Nimbus L&F,所有可访问的 L&F 输出相同

notice sice there is used Nimbus L&F, same output is there for all accesible 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();
            }
        });
    }
}

推荐答案

默认的首选尺寸计算是使用布局管理器来确定组件的首选尺寸.这意味着布局管理器遍历所有子组件以确定每个子组件的首选大小.对于打算用作容器的 JPanel,使用此计算.

The default preferred size calculation is to use the layout manager to determine the preferred size of a component. This means the layout manager iterates through all the child components to determine the preferred size of each. For a JPanel, which is meant to be used as a Container this calculation is used.

但是,对于其他 Swing 组件,始终会覆盖 getPreferredSize() 方法以为给定组件提供合理的大小.

However, for other Swing components, the getPreferredSize() method is always overridden to provide a reasonable size for the given component.

对于 JLabel,首选大小计算会考虑使用的文本和图标.由于您没有提供任何首选大小为零.当然,如果您使用 setPreferredSize() 方法手动覆盖此计算,则组件将具有首选大小.

In the case of a JLabel, the preferred size calculation takes into account the text and the icon used. Since you didn't provide either the preferred size is zero. Of course if you manually override this calculation by using the setPreferredSize() method then the component will have a preferred size.

因此,即使 Swing 允许您向任何组件添加组件并使用布局管理器来布局子组件,但在首选大小计算中并未使用这些子组件.

So even though Swing allows you to add components to any component and use a layout manager to layout the child components, these child components are not used in the preferred size calculation.

这不仅仅是 Nimbus 的问题.

This is not just a Nimbus issue.

这篇关于如何从 JComponent(s) 返回 XxxSize 添加到 JLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆