[英]Scale a JCheckBox box
I want to scale a JCheckBox
eg equally to its set text's font size. 我想缩放JCheckBox
例如同样地缩放其设置文本的字体大小。 For example when i increase the Font
size the checkbox itself stays small but it should grow with the text or i want to set the size of the box myself: 例如,当我增加Font
大小时,复选框本身保持较小但它应该随文本增长,或者我想自己设置框的大小:
JCheckBox chckbxTest = new JCheckBox("Test");
chckbxTest.setFont("Arial", Font.BOLD, 27));
If possible, I need the same functionality for JRadioButton
. 如果可能,我需要JRadioButton
的相同功能。 Unfortunately i haven't found any documentation regarding this functionality. 不幸的是,我没有找到任何有关此功能的文档。
Solution: 解:
The marked answer helped me to create a fully scalable own styled JCheckbox. 明确的答案帮助我创建了一个完全可扩展的自己风格的JCheckbox。 The following example draws a simple rectangle filled with another rectangle when checked: 以下示例在选中时绘制一个填充了另一个矩形的简单矩形:
[...]
JCheckBox myCheckBox = new JCheckBox("Test");
myCheckBox.setIcon(new SimpleCheckboxStyle(20));
[...]
class SimpleCheckboxStyle implements Icon {
int dim = 10;
public SimpleCheckboxStyle (int dimension){
this.dim = dimension;
}
protected int getDimension() {
return dim;
}
public void paintIcon(Component component, Graphics g, int x, int y) {
ButtonModel buttonModel = ((AbstractButton) component).getModel();
int y_offset = (int) (component.getSize().getHeight() / 2) - (int) (getDimension() / 2);
int x_offset = 2;
if (buttonModel.isRollover()) {
g.setColor(new Color(0, 60, 120));
} else if (buttonModel.isRollover()) {
g.setColor(Color.BLACK);
} else {
g.setColor(Color.DARK_GRAY);
}
g.fillRect(x_offset, y_offset, fontsize, fontsize);
if (buttonModel.isPressed()) {
g.setColor(Color.GRAY);
} else if (buttonModel.isRollover()) {
g.setColor(new Color(240, 240, 250));
} else {
g.setColor(Color.WHITE);
}
g.fillRect(1 + x_offset, y_offset + 1, fontsize - 2, fontsize - 2);
if (buttonModel.isSelected()) {
int r_x = 1;
g.setColor(Color.GRAY);
g.fillRect(x_offset + r_x + 3, y_offset + 3 + r_x, fontsize - (7 + r_x), fontsize - (7 + r_x));
}
}
public int getIconWidth() {
return getDimension();
}
public int getIconHeight() {
return getDimension();
}
}
For example when i increase the Font size the checkbox itself stays small but it should grow with the text or i want to set the size of the box myself 例如,当我增加字体大小时,复选框本身保持较小但它应该随文本增长,或者我想自己设置框的大小
Then you need to provide custom icons for the font size of your text. 然后,您需要为文本的字体大小提供自定义图标。 See methods like: 请参阅以下方法:
setIcon(....)
setSelectedIcon(...)
You would need to do the same for a JRadioButton. 你需要为JRadioButton做同样的事情。 Also, you would need different icons for each LAF. 此外,每个LAF都需要不同的图标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.