Java Swing - 如何更改 JPanel 的 TitledBorder 上的字体大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15260484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Swing - How to change the font size on a JPanel's TitledBorder?
提问by ban-geoengineering
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...
我需要能够以编程方式更改 Swing 应用程序中所有组件的字体大小。我无法以通常的方式(使用 UIManager 或 putClientProperty)执行此操作,因为我使用的是 Nimbus 外观和感觉,因此我使用以下方法分别增加应用程序中每个组件的字体大小...
private void enlargeFont(java.awt.Component c, float factor) {
c.setFont(c.getFont().deriveFont(c.getFont().getSize() * factor));
}
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.
我遇到的问题是我在我的 JPanel 上使用 TitledBorder 并且(可预测地)将我的 JPanel 传递到上述方法不会调整 JPanel 的边框标题的大小。
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(?).
那么有什么方法可以更改边框上的字体大小吗?(如果我可以获得边框的文本,那么我可以创建一个新的 TitledBorder(使用更大的字体),然后使用 JPanel 的 setBorder() 方法应用它......但似乎不可能获得边框文本(?)。
Does anyone have any suggestions on how to solve?
有没有人对如何解决有任何建议?
回答by Albert Turri
The following worked for me:
以下对我有用:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
((javax.swing.border.TitledBorder) jPanel1.getBorder()).
setTitleFont(new Font("Arial", Font.ITALIC, 14));
jPanel1.repaint();
}
I've tested this in NetBeans 6.9.1
我已经在 NetBeans 6.9.1 中测试过了