让 JCheckbox 更大..?

发布于 2024-10-14 05:07:42 字数 135 浏览 4 评论 0原文

我想让 JTable 中的 JCheckbox 更大(用于触摸屏),但它不会改变大小。

尝试了

  • 我用setPreferedSize
  • setSize

我该怎么办?..

i want to make my JCheckboxes in a JTable bigger (for Touchscreen), but it doesn't change the size.

I tried it with

  • setPrefferedSize
  • setSize

What should I do?..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思念绕指尖 2024-10-21 05:07:42

我假设你的意思是你想要一个更大的复选框。如果是这样,那么您需要创建图像来表示复选框的未选中和选中的图标。然后您可以使用这些图标创建渲染器和编辑器。最后,您需要增加表中每一行的高度。代码可能类似于:

Icon normal = new ImageIcon(...);
Icon selected = new ImageIcon(...);
JTable table = new JTable(...);
table.setRowHeight(...);

TableCellRenderer renderer = table.getDefaultRenderer(Boolean.class);
JCheckBox checkBoxRenderer = (JCheckBox)renderer;
checkBoxRenderer.setIcon( normal );
checkBoxRenderer.setSelectedIcon( selected );

DefaultCellEditor editor = (DefaultCellEditor)table.getDefaultEditor(Boolean.class);
JCheckBox checkBoxEditor = (JCheckBox)editor.getComponent();
checkBoxEditor.setIcon( normal );
checkBoxEditor.setSelectedIcon( selected );

I assume you mean you want a bigger check box. If so then you need to create images to represent the unselected and selected icons of the check box. Then you can create a renderer and editor using these icons. Finally you would need to increase the height of each row in the table. The code might look something like:

Icon normal = new ImageIcon(...);
Icon selected = new ImageIcon(...);
JTable table = new JTable(...);
table.setRowHeight(...);

TableCellRenderer renderer = table.getDefaultRenderer(Boolean.class);
JCheckBox checkBoxRenderer = (JCheckBox)renderer;
checkBoxRenderer.setIcon( normal );
checkBoxRenderer.setSelectedIcon( selected );

DefaultCellEditor editor = (DefaultCellEditor)table.getDefaultEditor(Boolean.class);
JCheckBox checkBoxEditor = (JCheckBox)editor.getComponent();
checkBoxEditor.setIcon( normal );
checkBoxEditor.setSelectedIcon( selected );
最丧也最甜 2024-10-21 05:07:42

重要提示:这仅在默认的“金属”外观和感觉下进行了测试。我不保证这适用于任何其他外观和感觉。另外,我也不完全确定它是如何工作的,因为它确实有点像黑客。

我能够以稍微不同的方式解决这个问题。

我想使用现有的图像并对其应用比例。我已经使用 UI 默认值缩放应用程序的字体,因此我有一个相当大的字体。我想知道是否可以利用这一点并相应地缩放复选框。

在搜索互联网并尝试了很多方法之后,我想出了这个方法:

public static void scaleCheckBoxIcon(JCheckBox checkbox){
    boolean previousState = checkbox.isSelected();
    checkbox.setSelected(false);
    FontMetrics boxFontMetrics =  checkbox.getFontMetrics(checkbox.getFont());
    Icon boxIcon = UIManager.getIcon("CheckBox.icon");
    BufferedImage boxImage = new BufferedImage(
        boxIcon.getIconWidth(), boxIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB
    );
    Graphics graphics = boxImage.createGraphics();
    try{
        boxIcon.paintIcon(checkbox, graphics, 0, 0);
    }finally{
        graphics.dispose();
    }
    ImageIcon newBoxImage = new ImageIcon(boxImage);
    Image finalBoxImage = newBoxImage.getImage().getScaledInstance(
        boxFontMetrics.getHeight(), boxFontMetrics.getHeight(), Image.SCALE_SMOOTH
    );
    checkbox.setIcon(new ImageIcon(finalBoxImage));

    checkbox.setSelected(true);
    Icon checkedBoxIcon = UIManager.getIcon("CheckBox.icon");
    BufferedImage checkedBoxImage = new BufferedImage(
        checkedBoxIcon.getIconWidth(),  checkedBoxIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB
    );
    Graphics checkedGraphics = checkedBoxImage.createGraphics();
    try{
        checkedBoxIcon.paintIcon(checkbox, checkedGraphics, 0, 0);
    }finally{
        checkedGraphics.dispose();
    }
    ImageIcon newCheckedBoxImage = new ImageIcon(checkedBoxImage);
    Image finalCheckedBoxImage = newCheckedBoxImage.getImage().getScaledInstance(
        boxFontMetrics.getHeight(), boxFontMetrics.getHeight(), Image.SCALE_SMOOTH
    );
    checkbox.setSelectedIcon(new ImageIcon(finalCheckedBoxImage));
    checkbox.setSelected(false);
    checkbox.setSelected(previousState);
}

它的作用是从复选框的字体规格中获取字体的大小。然后使用它根据“外观和感觉”中找到的图标派生一个新图标。

我无法解释的一件奇怪的事情是,当我访问相同的属性来获取每个属性时,处于“未选中”或默认状态的复选框的图标如何更改为“选定”图标。

我首先保存控件的状态,以便最后可以恢复它。这样做是因为为了正确设置图标,当您第一次从 UIManager 请求图标时需要取消选中状态,然后当您第二次请求图标以获得“选定”时需要检查状态' 图标。

我不完全确定 UIManager 是如何工作的,也不完全确定当我们仅通过设置单个复选框的“选定”值来调用相同的属性时,复选框图标会发生变化,但这正是获得两个必要图标所必需的。

如果您不想将大小基于字体,则可以轻松地将高度和宽度作为参数传递,并在设置缓冲图像大小时使用它们而不是字体的高度。

我可能会提到,同样的方法也适用于单选按钮

IMPORTANT NOTE: This was only tested with the default 'Metal' look and feel. I do not guarantee that this will work for any other look and feel. Also I am not entirely sure how it works because it is admittedly a bit of a hack.

I was able to solve this in a slightly different way.

I wanted to use the existing images and just apply a scale to it. I am already scaling the font of my application using the UI defaults and so I have a rather large font. I wondered if I could leverage that and scale the check boxes accordingly.

After scouring the internet and trying a bunch of things I came up with this method:

public static void scaleCheckBoxIcon(JCheckBox checkbox){
    boolean previousState = checkbox.isSelected();
    checkbox.setSelected(false);
    FontMetrics boxFontMetrics =  checkbox.getFontMetrics(checkbox.getFont());
    Icon boxIcon = UIManager.getIcon("CheckBox.icon");
    BufferedImage boxImage = new BufferedImage(
        boxIcon.getIconWidth(), boxIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB
    );
    Graphics graphics = boxImage.createGraphics();
    try{
        boxIcon.paintIcon(checkbox, graphics, 0, 0);
    }finally{
        graphics.dispose();
    }
    ImageIcon newBoxImage = new ImageIcon(boxImage);
    Image finalBoxImage = newBoxImage.getImage().getScaledInstance(
        boxFontMetrics.getHeight(), boxFontMetrics.getHeight(), Image.SCALE_SMOOTH
    );
    checkbox.setIcon(new ImageIcon(finalBoxImage));

    checkbox.setSelected(true);
    Icon checkedBoxIcon = UIManager.getIcon("CheckBox.icon");
    BufferedImage checkedBoxImage = new BufferedImage(
        checkedBoxIcon.getIconWidth(),  checkedBoxIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB
    );
    Graphics checkedGraphics = checkedBoxImage.createGraphics();
    try{
        checkedBoxIcon.paintIcon(checkbox, checkedGraphics, 0, 0);
    }finally{
        checkedGraphics.dispose();
    }
    ImageIcon newCheckedBoxImage = new ImageIcon(checkedBoxImage);
    Image finalCheckedBoxImage = newCheckedBoxImage.getImage().getScaledInstance(
        boxFontMetrics.getHeight(), boxFontMetrics.getHeight(), Image.SCALE_SMOOTH
    );
    checkbox.setSelectedIcon(new ImageIcon(finalCheckedBoxImage));
    checkbox.setSelected(false);
    checkbox.setSelected(previousState);
}

What it does is get the size of the font from the checkbox's font metrics. Then using that it derives a new icon based on the icon found in the 'Look and Feel'.

One odd thing that I am not able to explain is how the icon for the checkbox in its 'un-selected' or default state, changes to the 'selected' icon, when I am accessing the same property to get each one.

I start by saving the state of the control so I can restore it at the end. This is done because in order for the icons to be set properly, the state needs to be unchecked when you first request the icon from the UIManager and then it will need to be checked when you request the icon the second time to get the 'selected' icon.

I am not entirely sure how the UIManager works or why the checkbox icon changes when we call the same property just by setting the 'selected' value of a single checkbox, but that is what is required in order to get both the necessary icons.

If you did not want to base the size on the font you could easily just pass in the height and width as parameters and use them instead of the font's height when setting the buffered image size.

I might mention that this same methodology works with radiobuttons

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文