Java Swing-如何在JPanel的TitledBorder上更改字体大小? [英] Java Swing - How to change the font size on a JPanel's TitledBorder?

查看:427
本文介绍了Java Swing-如何在JPanel的TitledBorder上更改字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够以编程方式更改Swing应用程序中所有组件的字体大小.我无法以通常的方式(通过UIManager或putClientProperty)执行此操作,因为我使用的是Nimbus外观,因此我正在使用以下方法单独增加应用程序中每个组件的字体大小...

I need to be able to programmatically change the font size of all the components in my Swing app. I cannot do this in the usual ways (with UIManager or putClientProperty) as I am using the Nimbus look and feel, so am using the following method to increase the font size of each component in my app individually...

private void enlargeFont(java.awt.Component c, float factor) {
    c.setFont(c.getFont().deriveFont(c.getFont().getSize() * factor));
}

我遇到的问题是我在JPanel上使用了TitledBorder,并且(可以预测)将JPanel传递到上述方法中并不能调整JPanel边框标题的大小.

The problem I'm having is that I am using a TitledBorder on my JPanel and (predictably) passing my JPanel into the above method doesn't resize the JPanel's border title.

那么我有什么办法可以更改边框上的字体大小? (如果可以获取边框的文本,则可以创建一个新的TitledBorder(使用更大的字体),然后使用JPanel的setBorder()方法对其进行应用...但是似乎无法获取边框的文本(?).

So is there any way I can change the font size on the border? (If I could get the text of the border, I could then create a new TitledBorder (using a bigger font) and then apply it with the JPanel's setBorder() method... but it doesn't seem possible to get the border text(?).

有人对如何解决有任何建议吗?

Does anyone have any suggestions on how to solve?

推荐答案

以下内容对我有用:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    ((javax.swing.border.TitledBorder) jPanel1.getBorder()).
        setTitleFont(new Font("Arial", Font.ITALIC, 14));

    jPanel1.repaint();
}

我已经在NetBeans 6.9.1中对此进行了测试.

I've tested this in NetBeans 6.9.1

这篇关于Java Swing-如何在JPanel的TitledBorder上更改字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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