【问题标题】:Swing JButton is glitching with JCheckBox focus stateSwing JButton 在 JCheckBox 焦点状态下出现故障
【发布时间】:2015-07-31 22:19:18
【问题描述】:

我有一个带有 Windows 外观和感觉的 JPanel。我有 3 个 JCheckBoxes,包括 2 个不影响这个问题的禁用的。但是,未禁用的 JButton 与我稍后在此 JPanel 中的 JButton 出现故障:

JCheckBox 代码:

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 代码:

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

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

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

【问题讨论】:

  • 这不是故障。这就是使用绝对定位时发生的情况。使用布局管理器,您的问题将迎刃而解。
  • @BinkanSalaryman 不,getWidth()和getHeight()方法是JPanel的宽高。
  • @Momo,当提出问题时,请发布适当的SSCCE 来证明问题。我们无法说出上述代码是如何使用的上下文。

标签: java swing jbutton jcheckbox


【解决方案1】:

这里有一个可运行的小示例,向您展示如何使用LayoutManagers,因为 LayoutManagers 将解决您在绝对定位方面遇到的问题。 (请注意,这可能不是最好的解决方案,LineBorders 仅用于可视化)

有点乱的代码:

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);

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 2014-06-27
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多