java swing button 焦点,Swing JButton出现JCheckBox焦点状态故障

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:

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

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:

Ki26q.png

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.

解决方案

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 messy code:

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值