为所有子组件设置属性
我以前从未使用过 Java AWT,现在我有一段代码可以显示 JFrame
并将所有子组件的字体属性设置为相同的值。 我只想在一个地方设置该属性。 我怎样才能做到这一点?
在 .NET/WinForms 中,子控件继承自其父控件,因此只需为 Form
实例设置字体即可将其传播到所有控件。 出乎意料的是,这对于AWT 来说似乎并不成立。
下面的小代码递归地设置所有组件的字体:
private void setFontForAll(JFrame f, java.awt.Font font) {
f.setFont(font);
setFontRecursive(f.getContentPane().getComponents(), font);
}
private static void setFontRecursive(Component[] components, java.awt.Font font) {
for (Component c : components) {
c.setFont(font);
if (c instanceof java.awt.Container)
setFontRecursive(((java.awt.Container)c).getComponents(), font);
}
}
但是,它有四个缺点:
- 额外的代码,对于具有嵌套布局面板的大型表单来说实际上可能效率很低。
- 代码是非通用的。 如果我将来需要对另一个属性执行相同的操作,我必须重写该方法(或将其重构为更通用,但会牺牲简洁性)。
- 用法是非声明性的,即必须在表单创建的最后调用(在所有子组件都已初始化和添加之后),而不是以声明方式的任何地方调用。
- 它不起作用。组件设置正确,但并非所有东西都是组件。 例如,
JPanel
的TitledBorder
未设置。
I've never used Java AWT before and now I've got a piece of code that displays a JFrame
and sets the font property for all child components to the same value. I'd like to set the property in one place only. How can I do this?
In .NET/WinForms, child controls inherit from their parent controls, thus it would be enough to set the font for the Form
instance to have it propagate to all controls. Unexpectedly, this doesn't seem to hold for AWT.
The following little code sets the font for all components recursively:
private void setFontForAll(JFrame f, java.awt.Font font) {
f.setFont(font);
setFontRecursive(f.getContentPane().getComponents(), font);
}
private static void setFontRecursive(Component[] components, java.awt.Font font) {
for (Component c : components) {
c.setFont(font);
if (c instanceof java.awt.Container)
setFontRecursive(((java.awt.Container)c).getComponents(), font);
}
}
However, it has four drawbacks:
- Extra code, which might actually be quite inefficient for large forms with nested layout panels.
- Code is non-generic. If I need to do the same for another property in future, I've got to rewrite the method (or refactor it to be more general at the expense of conciseness).
- Usage is non-declarative, i.e. has to be called at the very end of the form creation (after all child components have been initialized and added) instead of anywhere in a declarative manner.
- It doesn't work. Components are set correctly but not all things are components. For example, the
TitledBorder
s ofJPanel
s don't get set.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UIManager 类就是您需要的东西。 在构建用户界面之前,只需告诉它您想要什么字体。 但请注意; 定义了很多字体键,如果您想更改所有字体键,则必须全部设置。
您可以通过以编程方式检查返回哈希表的 UIManager.getDefaults() 来查看设置的键和值。
The UIManager class is the thing you need. Before you build your user interface simply tell it what fonts you want. Be warned though; there are a lot of font keys defined and if you want to change them all, you'll have to set them all.
You can see the keys and values that are set by programmatically inspecting UIManager.getDefaults() which returns a hashtable.
对于 Swing,您还可以使用命令行参数设置字体:
添加
-Dswing.aatext=true
用于抗锯齿,这使得整个 GUI 看起来更好。 :)For Swing you can also set the fonts with command-line arguments:
Add
-Dswing.aatext=true
for anti-aliasing which makes the whole GUI look a lot nicer. :)