- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想缩放一个 JCheckBox
例如与其设置的文本的字体大小相同。例如,当我增加 Font
大小时,复选框本身保持很小,但它应该随着文本增长,或者我想自己设置框的大小:
JCheckBox chckbxTest = new JCheckBox("Test");
chckbxTest.setFont("Arial", Font.BOLD, 27));
如果可能,我需要为 JRadioButton
提供相同的功能。不幸的是,我还没有找到关于此功能的任何文档。
解决方案:
标记的答案帮助我创建了一个完全可扩展的自己样式的 JCheckbox。下面的示例绘制了一个简单的矩形,选中时用另一个矩形填充:
[...]
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
然后您需要为文本的字体大小提供自定义图标。请参阅以下方法:
setIcon(....)
setSelectedIcon(...)
您需要对 JRadioButton 执行相同的操作。此外,您需要为每个 LAF 使用不同的图标。
关于java - 缩放 JCheckBox 框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22691353/
我正在开发一个需要确定选择了哪个 JCheckBox 的程序。我正在使用三个不同的按钮组,其中两个有重叠的文本。我需要能够确定是哪个触发了事件,以便将适当的费用(COSTPERROOM 与 COSTP
我正在为我的类(class)做一个项目,我有以下情况:它是一个 JDialog,它在构造函数上接收一个 Object 以使用它。对象“Album”有两个属性,根据 MySQL boolean 值可以是
我正在开发一个 Java 应用程序。主类框架名称为“a”。在框架“a”中,有一个组件 - jCheckBox。因此,当我检查(勾选)这个 jCheckBox 时,它会打开另一个框架“b”。我想在关闭框
有时我需要复选框的标签位于复选框的左侧而不是右侧,因此不使用 JCheckBox checkbox = new JCheckBox("label",false); 我愿意: JCheckBox che
如何更改 JCheckbox 中文本的位置。 我希望文本位于复选框的左侧,而不是右侧的默认文本。 我曾多次使用 jcheckbox、jtooglebutton 和 jabstractButton ap
我已经检查了所有地方是否有修复,但没有任何方法可以使我的复选框出现。我将它添加到面板并将面板添加到窗口。该按钮出现,因此肯定是复选框有问题。这是我的代码: import java.awt.Border
这个问题已经有答案了: Java ActionListener error: incompatible types (4 个回答) 已关闭 5 年前。 我最近刚刚开始学习 Java 代码。我在添加监听
我已经实现了一个返回 JCheckBox 的 TreeCellRenderer(渲染器扩展 JCheckBox 并根据项目中的标志设置其选择状态的简单代码)并且运行良好。但是,当列表中有多个项目时,它
当表模型最初未定义时如何添加jcheckbox。 我已经用数据库中的数据填充了 jtable,但现在我需要添加一个用于选择所需记录的 jcheckbox。 如果数据表行是用 boolean 类预定义的
我想在java中创建一个具有可变数量复选框的框架,这将取决于数据库中存在的行数。我面临的问题是:-1. 如何统计数据库的行数?2. 如何以及在哪里存储数据库列中存在的数据?3. 如何使用数据库列中存在
这个问题已经有答案了: How to add checkboxes to JTABLE swing [closed] (1 个回答) 已关闭 9 年前。 我创建了一个 jTable 类,它与另一个类一
我有一个 List,其中包含某种类型的 JPA Entity 对象。它们的reference字符串值显示在JList中供用户查看。 我希望我的用户能够在 UI 中选择过滤器作为 JCheckBox,例
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
我希望我的程序在我启用一个名为 doos 的复选框时开始在鼠标下打印 RGB 值,并且我希望它在取消选择时停止。 private void doosActionPerformed(java.a
当我选择JCheckBox时,它会自动取消选择... import javax.swing.*; import java.awt.*; import java.awt.event.*; public
我正在使用 JCheckBox 查找客户想要购买的商品,如果选择了 JCheckBox(hat),则总计 += hatprice,但它不会更新总计。我的代码: import javax.swing.*
我有一个 JCheckbox,当单击该复选框时,我想在标签内设置文本。 到目前为止,我已经声明了变量以及复选框和标签框架内的设置: JCheckBox chckbxIncludeExt;
我正在尝试创建一个 java GUI,但在显示复选框时遇到一些问题。我查看了 oracle 教程并包含了他们拥有的所有代码,但我不确定我缺少什么。有什么想法吗? public class HPAPro
我有一个帮助 Pane ,它出现在程序开始时,但可以关闭。如果用户希望它返回,菜单栏中有一个选项可以重新激活它。但是,当他们选择从帮助菜单中显示它时,它会自动重新选中“不再显示”框。如何使该框保持与用
我正在尝试 JCheckBox。我想要的是,当我选中其中任何一个(复选框)时,复选框中的文本应该显示在 jtextarea 中。 我现在拥有的是: import java.awt.*; import
我是一名优秀的程序员,十分优秀!