java - 仅更改某些组件的字体
问题描述
我有必要更改整个(swing)应用程序的字体样式和大小,但仅限于某些组件(主要是 JLabel、JTextArea、JTextField、JComboBox、JTable)。
我有这段代码
public void updateComponent(final Component component, final Font font){
if(component instanceof JComponent){
((JComponent)component).updateUI();
}
if(component instanceof Container){
Component[] children = ((Container)component).getComponents();
for(Component child : children){
updateComponent(child, font)
}
}
if(component instanceof JLabel || component instanceof JTextArea || component instanceof JComboBox || component instanceof JTextField || component instanceof JTable){
component.setFont(font);
}
}
但是这段代码改变了所有JLabel、JTextArea、JTextField、JComboBox、JTable 的字体,而我只希望这些类的某些实例使用它。
我知道我可以定义和创建n个扩展原始类并检查它们的类,但我真的不喜欢为此创建n 个基本上是空的类。
public class JLabel2 extends JLabel{}
public class JTextArea2 extends JTextArea{}
...
和
if(component instanceof JLabel2 || component instanceof JTextArea2 || ...){
有没有更简单的解决方案?谢谢
解决方案
推荐阅读
- node.js - 如何存储多个用户名并在聊天屏幕上显示?Socket.io 和 Node.js
- corrupt-data - .xlxs 文件在使用带有脚本任务的 SSIS 从谷歌驱动器下载时损坏
- tensorflow - 如何添加个人数据增强?
- css - React Boostrap 4 css模块未应用于导航栏
- javascript - 如何使用基于其他数组的嵌套对象数组过滤对象数组
- python - 将值从列表列表映射到字典的简单方法
- sql - 如何添加具有某种类型的数字序列的列
- c# - OleDbCommand 创建带括号的 excel 列名
- java - 在没有 POJO 的情况下将 JsonObject 反序列化(转换)为 DateTime
- javascript - 箭头翻转功能半工作,几乎完整的 JS