Swing缩放组件的文本字体 [英] Swing Scale a Text Font of Component

查看:42
本文介绍了Swing缩放组件的文本字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在componentResized事件上设置按钮的字体大小.我得到了屏幕尺寸和按钮的尺寸.但是无法计算出要设置的首选字体大小.如果按钮的宽度增加/减小,则字体大小也应相应地增加/减小.我也无法获得Graphics对象.

I want to set the font size of button on componentResized event. I get the screensize and the size of the button. But can't get a way to calculate the preferred font size to set. If the Width of button is increased/decreased the font size also should increase/decrease accordingly. I can't get an object of Graphics also.

解决此问题的解决方案是什么?

What could be a solution to work out for this problem ?

推荐答案

这更像是蛮力解决方案.

This is more of brute force solution.

1)您可以尝试使用以下方法获取字体指标:

1) You can try getting the font metrics using doing something like this:

// get metrics from the graphics
FontMetrics metrics = graphics.getFontMetrics(font);
// get the height of a line of text in this font and render context
int hgt = metrics.getHeight();
// get the advance of my text in this font and render context
int adv = metrics.stringWidth(text);
// calculate the size of a box to hold the text with some padding.
Dimension size = new Dimension(adv+2, hgt+2);

2)然后,您可以搜索适合您组件的字体大小.

2) Then you can search through the fonts sizes that fit your component.

        Font savedFont = oldFont;
    for (int i = 0; i < 100; i++) {
        Font newFont = new Font(oldFont.getFontName(), oldFont.getStyle(), i);
        Dimension d = getFontSize(g,newFont,text);
        if(componentSize.height < d.height || componentSize.width < d.width){
            return savedFont;
        }
        savedFont = newFont;
    }

将它们放在一起(请注意,这未经测试)

Putting it all together (note this is not tested)

public Dimension getFontSize(Graphics graphics, Font font, String text){
    // get metrics from the graphics
    FontMetrics metrics = graphics.getFontMetrics(font);
    // get the height of a line of text in this font and render context
    int hgt = metrics.getHeight();
    // get the advance of my text in this font and render context
    int adv = metrics.stringWidth(text);
    // calculate the size of a box to hold the text with some padding.
    Dimension size = new Dimension(adv+2, hgt+2);
    return size;
}

public Font findFont(Dimension componentSize, Font oldFont, String text, Graphics g){
    //search up to 100
    Font savedFont = oldFont;
    for (int i = 0; i < 100; i++) {
        Font newFont = new Font(oldFont.getFontName(), oldFont.getStyle(), i);
        Dimension d = getFontSize(g,newFont,text);
        if(componentSize.height < d.height || componentSize.width < d.width){
            return savedFont;
        }
        savedFont = newFont;
    }
    return oldFont;
}

使用组件进行编辑以获取FontMetrics

EDIT using component to get FontMetrics

    public Dimension getFontSize(FontMetrics metrics ,Font font, String text){
    // get the height of a line of text in this font and render context
    int hgt = metrics.getHeight();
    // get the advance of my text in this font and render context
    int adv = metrics.stringWidth(text);
    // calculate the size of a box to hold the text with some padding.
    Dimension size = new Dimension(adv+2, hgt+2);
    return size;
}

public Font findFont(Component component, Dimension componentSize, Font oldFont, String text){
    //search up to 100
    Font savedFont = oldFont;
    for (int i = 0; i < 100; i++) {
        Font newFont = new Font(oldFont.getFontName(), oldFont.getStyle(), i);
        Dimension d = getFontSize(component.getFontMetrics(newFont),newFont,text);
        if(componentSize.height < d.height || componentSize.width < d.width){
            return savedFont;
        }
        savedFont = newFont;
    }
    return oldFont;
}

测量文本

这篇关于Swing缩放组件的文本字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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