我正在尝试使用JRadioButtons和JCheckBox来响应鼠标右键单击以及鼠标左键单击。我发现他们没有正确回答鼠标右击,但我无法弄清楚原因。
private JRadioButton btn1 = new JRadioButton("1");
private JRadioButton btn2 = new JRadioButton("2");
private JRadioButton btn3 = new JRadioButton("3");
private JCheckBox btn4 = new JCheckBox("4");
private JCheckBox btn5 = new JCheckBox("5");
private ButtonGroup group = new ButtonGroup();
private void setupActionListeners() {
btn1.addMouseListener(createMouseAdapter(btn1));
btn2.addMouseListener(createMouseAdapter(btn2));
btn3.addMouseListener(createMouseAdapter(btn3));
btn4.addMouseListener(createMouseAdapter(btn4));
btn5.addMouseListener(createMouseAdapter(btn5));
group.add(btn1);
group.add(btn2);
group.add(btn3);
}
private MouseAdapter createMouseAdapter(final AbstractButton button) {
return new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("Button " + button.getText() + " selected = " + button.isSelected());
}
};
}
我使用上面的程序来查看完成鼠标左键或右键时按钮的状态。看起来在3个案例中,右键单击值是错误的。
这可能是Java中的错误,还是我做错了什么?
状态结果(抱歉看起来,无法找到向此条目添加表格的好方法)
按钮类型鼠标按钮点击时间的按钮状态 button.isSelected()in mouseClicked
JRadioButton 左未选中 TRUE
JRadioButton 正确未选中 FALSE < - 错误?
JRadioButton 左已选择 TRUE
JRadioButton 正确已选择 TRUE
JCheckBox 左未选中 TRUE
JCheckBox 正确未选中 FALSE < - 错误?
JCheckBox 左已选择 FALSE
JCheckBox 正确选择 TRUE < - 错误?