如何在更改字体大小时更改 JLabel 的大小?

How to change JLabel's size when the font size is changed?

我有一个 JLabel。 JLabel的代码如下

panelmain = new JPanel();
panelmain.setLayout(null);
panelmain.setPreferredSize(new java.awt.Dimension(800, 600));
panelmain.addComponentListener(listen);
panelmain.setBorder(null);
titlebar = new JLabel("Hello World");
titlebar.setBounds(10, 10, 100, 30);
panelmain.add(titlebar);

我的问题是,如果我更改 titlebar 的字体(即 JLabel),那么如何更改 titlebar 的大小(已在代码中设置为 titlebar.setBounds(10, 10, 100, 30);)?

Girish 编辑

我的完整代码如下。

import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class IFrame extends JInternalFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 6526561589695424088L;

    private JScrollPane jsp;

    private IFListen listen;

    private JPanel panelmain;

    protected JPanel panel;

    private String title;

    private JLabel titlebar;

    public IFrame()
    {
        this.title="";
        init();
    }

    public IFrame(String title)
    {
        this.title=title;
        init();
    }

    private void init()
    {
        setLayout(null);

        listen=new IFListen();
        panelmain=new JPanel();
            panelmain.setLayout(null);
            panelmain.setPreferredSize(new java.awt.Dimension(800, 600));
            panelmain.addComponentListener(listen);
            panelmain.setBorder(null);

            titlebar=new JLabel("Hello World");
            titlebar.setFont(new java.awt.Font("Monotype Corsiva", 1, 48));
            panelmain.add(titlebar);

            panel=new JPanel();
            panel.setBorder(javax.swing.BorderFactory.createTitledBorder(title));
            panel.setMinimumSize(new java.awt.Dimension(400, 400));
            panel.setSize(400, 400);
            panelmain.add(panel);

        jsp=new JScrollPane(panelmain);
        jsp.setBorder(null);

        add(jsp);

        this.addComponentListener(listen);
    }

    //INFO Custom Methods
    public void setTitleFont(java.awt.Font font)
    {
        titlebar.setFont(font); //Here I want to change size of label.
    }

    //INFO Listener Class for IFrame
    private class IFListen implements ComponentListener
    {
        //INFO Overridden Methods
        @Override
        public void componentResized(ComponentEvent e) 
        {
            if(e.getSource() instanceof IFrame)
                jsp.setBounds(5, 5, getWidth()-20, getHeight()-20);
            else if(e.getSource()==panelmain)
            {
                panel.setLocation(Integer.parseInt(panelmain.getWidth()/2-panel.getWidth()/2+""), 0);
            }
        }

        //INFO Unimplemented Methods
        @Override
        public void componentShown(ComponentEvent arg0) {}

        @Override
        public void componentHidden(ComponentEvent arg0) {}

        @Override
        public void componentMoved(ComponentEvent arg0) {}
    }
}

我已经评论了更改字体大小的地方,我想更改 jlabel 的大小。

不要使用 setPreferredSize,您刚刚删除了标签用于计算所需大小的所有计算。

避免使用 null 布局,像素完美布局是现代 ui 设计中的一种错觉。影响组件个体大小的因素太多,none 是您可以控制的。 Swing 旨在与核心的布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正

使用适当的布局。这里重要的是,没有一种布局可以满足您的所有需求。您将需要学习利用每种布局的优势(和劣势)并将它们用于您的优势。这就是俗称的"compound layouts"。查看 Laying Out Components Within a Container 了解更多详细信息和想法

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label;
        private JSlider slider;

        public TestPane() {
            label = new JLabel("Look, no hands!");
            setLayout(new BorderLayout());
            JPanel panel = new JPanel(new GridBagLayout());
            panel.add(label);

            add(panel);

            slider = new JSlider(8, 96);
            add(slider, BorderLayout.SOUTH);

            slider.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    Font font = label.getFont();
                    font = font.deriveFont((float)slider.getValue());
                    label.setFont(font);
                }
            });
            slider.setValue(8);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

    }

}

JLabel出人意料的熟练,改字体(text/icon)会自动导致布局失效,自动请求重绘...