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

时间:2015-07-14 11:57:30

标签: java swing layout-manager null-layout-manager

我有一个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.setBounds(10, 10, 100, 30);的大小(已在代码中设置为titlebar)?

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的大小。

1 个答案:

答案 0 :(得分:4)

不要使用setPreferredSize,您刚刚删除了该标签用于计算其所需尺寸的所有计算。

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

使用适当的布局。重要的是,没有任何一个布局可以做你想要的一切。您将需要学习利用每种布局的优势(和弱点)并将它们用于您的优势。这就是通常所说的"复合布局"。有关更多详细信息和想法,请查看Laying Out Components Within a Container

Resizable label

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非常精通,更改字体(文本/图标)会自动导致布局无效并请求重新绘制所有内容......