Swing JButton出现JCheckBox焦点状态故障 [英] Swing JButton is glitching with JCheckBox focus state

查看:89
本文介绍了Swing JButton出现JCheckBox焦点状态故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Windows外观设置了一个JPanel.我有3个JCheckBoxes,包括2个禁用的JCheckBoxes,它们不会干扰此问题.但是,非禁用用户可能会在稍后在此JPanel中使用的JButton遇到问题:

I have a JPanel set with the Windows Look and Feel. I have 3 JCheckBoxes, including 2 disabled ones which don't interfere with this problem. However, the non-disabled one is glitching with the JButton I have later in this JPanel:

JCheckBox代码:

The JCheckBox code:

JCheckBox checkBox = new JCheckBox("TTxJIRA Bash");
checkBox.setSize(300, (checkBox.getFontMetrics(checkBox.getFont()).getHeight()));
checkBox.setLocation(10, 100);
checkBox.setVisible(true);
checkBox.setSelected(true);
checkBox.setBackground(new Color(0, 0, 0, 0));
checkBox.setFocusable(false);
add(checkBox);

还有JButton代码:

And the JButton code:

JButton button = new JButton("Install");
button.setSize(80, 25);
button.setLocation(getWidth() - 100, getHeight() - 60);
button.setFocusable(false);
button.setVisible(true);
add(button);

当我将鼠标悬停在按钮上,然后将鼠标悬停在复选框上时,会出现此故障:

When I hover on the button, then hover on the checkbox, this glitch occurs:

我的疯狂猜测使我认为这与同时关注两个组件有关,但是添加button.setFocusable(false);并没有帮助.

My wild guess makes me think this has to do with two components being focused at the same time, but adding the button.setFocusable(false); did not help.

推荐答案

下面是一个可运行的示例,向您展示了如何使用LayoutManagers,因为LayoutManagers可以解决您在绝对定位时遇到的问题. (请注意,这可能不是最佳解决方案,并且LineBorders仅用于可视化)

Here's a little runnable example that shows you how you could use LayoutManagers for that, because LayoutManagers will fix your problems you had with absolute positioning. (Please note that this is probably not the best solution and that the LineBorders are just for visualization)

Kinda乱码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class LayoutManagerExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame();

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());

        JButton button = new JButton("Install");

        JCheckBox cb1 = new JCheckBox("1");
        cb1.setEnabled(false);

        JCheckBox cb2 = new JCheckBox("2");
        cb2.setEnabled(false);

        JCheckBox cb3 = new JCheckBox("3");

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10,10,10,10);

        JPanel southPanel = new JPanel();
        southPanel.setLayout(new BorderLayout());
        southPanel.setBorder(new LineBorder(Color.BLACK));

        JPanel westPanel = new JPanel();
        westPanel.setLayout(new GridLayout(10,1));
        westPanel.setBorder(new LineBorder(Color.BLACK));

        JPanel southEastPanel = new JPanel();
        southEastPanel.setBorder(new LineBorder(Color.BLACK));

        mainPanel.add(southPanel,BorderLayout.SOUTH);
        mainPanel.add(westPanel,BorderLayout.WEST);
        southPanel.add(southEastPanel,BorderLayout.EAST);
        westPanel.add(cb1);
        westPanel.add(cb2);
        westPanel.add(cb3);
        southEastPanel.add(button, gbc);
        frame.add(mainPanel);

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(500,500);
        frame.setVisible(true);

    }
}

这篇关于Swing JButton出现JCheckBox焦点状态故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆