【问题标题】:How to change JLabel font size to fill JPanel free space while resizing?如何在调整大小时更改 JLabel 字体大小以填充 JPanel 可用空间?
【发布时间】:2012-03-07 21:03:00
【问题描述】:

这里有一个类似的问题: How to change the size of the font of a JLabel to take the maximum size

但它不能向后工作。

所以如果你把JPanel 变大,字体会变大,但如果你把它变小JLabel,大小和字体会保持原样

Check this Image

如何使Jlabel字体在调整大小时根据JPanel大小增长?

【问题讨论】:

  • 您是否尝试修改代码以解决帧尺寸缩小时的问题?

标签: java swing jlabel


【解决方案1】:

通过使用FontMetricsTextLayout 可以得到这个输出(请阅读代码中的注释)

SwingUtilities 也可以做到这一点

我建议在两个方向上再添加几个像素

ComponentListener 添加到容器中,并在componentResized 事件中再次重新计算FontMetrics

import java.awt.*;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import javax.swing.*;

public class ShowFontMetrics extends JFrame {

    private static final long serialVersionUID = 1L;
    private JLabel lTime;

    public ShowFontMetrics() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel pane = new JPanel();
        pane.setLayout(new FlowLayout());
        lTime = new JLabel("88:88");
        lTime.setFont(new Font("Helvetica", Font.PLAIN, 88));
        FontMetrics fm = lTime.getFontMetrics(lTime.getFont());
        TextLayout layout = new TextLayout(lTime.getText(), lTime.getFont(), fm.getFontRenderContext());
        Rectangle2D bounds = layout.getBounds();
        Dimension d = lTime.getPreferredSize();
        d.height = (int) (bounds.getHeight() + 100);// add huge amount of pixels just for for fun
        d.width = (int) (bounds.getWidth() + 150);// add huge amount of pixels just for for fun
        lTime.setPreferredSize(d);
        lTime.setVerticalAlignment(SwingConstants.CENTER);
        lTime.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        pane.add(lTime);
        setContentPane(pane);
    }

    public static void main(String[] arguments) {
        ShowFontMetrics frame = new ShowFontMetrics();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

【讨论】:

  • 谢谢。但这并没有达到我的意思.. JFrame 调整大小后必须更改 JLable 的字体。因此,如果您使 JFrame 更大/更小 - JLabel 字体也必须更改..
  • 有一个相关的例子可以做到这一点here
  • 我为 CompomentListener 更新了我的答案(可能我没有阅读你的问题...),
  • @mKorbel 仍然无法正常工作。动态字体设置在哪里?你设置字体: lTime.setFont(new Font("Helvetica", Font.PLAIN, 88));所以它是静态的......
【解决方案2】:

像这样? http://java-sl.com/tip_adapt_label_font_size.html

更新:

请求的代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

public class ResizeLabelFont extends JLabel {
    public static final int MIN_FONT_SIZE=3;
    public static final int MAX_FONT_SIZE=240;
    Graphics g;

    public ResizeLabelFont(String text) {
        super(text);
        init();
    }

    protected void init() {
        addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                adaptLabelFont(ResizeLabelFont.this);
            }
        });
    }

    protected void adaptLabelFont(JLabel l) {
        if (g==null) {
            return;
        }
        Rectangle r=l.getBounds();
        int fontSize=MIN_FONT_SIZE;
        Font f=l.getFont();

        Rectangle r1=new Rectangle();
        Rectangle r2=new Rectangle();
        while (fontSize<MAX_FONT_SIZE) {
            r1.setSize(getTextSize(l, f.deriveFont(f.getStyle(), fontSize)));
            r2.setSize(getTextSize(l, f.deriveFont(f.getStyle(),fontSize+1)));
            if (r.contains(r1) && ! r.contains(r2)) {
                break;
            }
            fontSize++;
        }

        setFont(f.deriveFont(f.getStyle(),fontSize));
        repaint();
    }

    private Dimension getTextSize(JLabel l, Font f) {
        Dimension size=new Dimension();
        g.setFont(f);
        FontMetrics fm=g.getFontMetrics(f);
        size.width=fm.stringWidth(l.getText());
        size.height=fm.getHeight();

        return size;
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.g=g;
    }

    public static void main(String[] args) throws Exception {
        ResizeLabelFont label=new ResizeLabelFont("Some text");
        JFrame frame=new JFrame("Resize label font");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(label);

        frame.setSize(300,300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

【讨论】:

  • 我知道这个答案很旧,但它也是一个仅链接的答案。你能从链接中复制所需的信息吗?
【解决方案3】:

好的,这是答案。

让我们从这里获取代码:How to change the size of the font of a JLabel to take the maximum size,来自 coobird

的回答
int componentWidth = label.getWidth();

我们需要从JFrame 组件获取宽度,而不是从JLabel,因为代码不允许更改 JLabel 的大小。

这将解决它:

int componentWidth = this.getWidth()-20; // '20' - according width of Jlabel to JFrame

【讨论】:

  • 对于 Container.getSize/getBounds 需要添加 Swing Timer,避免造成闪烁,只调用重启直到 resize 没有结束,
  • 为什么20;这不应该基于FontMetrics吗?
猜你喜欢
  • 1970-01-01
  • 2020-11-30
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 2011-01-17
  • 2016-02-26
  • 2014-10-28
  • 2016-07-07
相关资源
最近更新 更多