我有一个带有Windows外观的JPanel设置。我有3个JCheckBox,包括2个禁用的JCheckBox,它们不会干扰这个问题。然而,非残疾人正在使用我后来在这个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);
并没有帮助。
答案 0 :(得分:0)
这是一个可运行的小例子,向您展示如何使用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);
}
}