调整组件大小

您是否曾经需要过较小版本的组件放置在工具选项板或工具栏上或状态栏中?您可以通过在组件上设置 Client 端属性来调整组件的大小。除了“常规”尺寸外,还支持三种尺寸:迷你,小和大。

不支持 size variants 属性的一个组件是JLabel。但是,您可以通过更改标签的字体大小来更改标签的大小。

Note:

其他外观实现方式(例如 Apple 的 Aqua)也可能会尊重 Client 端 Client 使用的尺寸变体。目前,Nimbus 是唯一支持尺寸变化的 Sun 外观。

在显示组件之前,可以用一行代码设置组件的大小。以下代码段显示了如何使用每种尺寸:

// mini
myButton.putClientProperty("JComponent.sizeVariant", "mini");
// small
mySlider.putClientProperty("JComponent.sizeVariant", "small");
// large
myTextField.putClientProperty("JComponent.sizeVariant", "large");

如果正确设置了大小变量属性,但是组件以“常规”大小显示,则可能需要强制对 UI 进行更新。您可以通过在显示窗口之前调用SwingUtilities.updateComponentTreeUI(Component)方法来实现。以下代码片段更新了窗口及其包含的所有组件:

JFrame frame = ...;

SwingUtilities.updateComponentTreeUI(frame);

frame.pack();
frame.setVisible(true);