我遇到的问题是我的 JCheckBox 需要单击 2 次(而不是双击)进行检查,然后单击 1 次取消选中。其余代码完美运行。
JCheckBox check = new JCheckBox(new ImageIcon("images/check.png"));
check.setOpaque(false);
check.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent evt){
for(int i = 0; i < tempPanel.length; i++)
if(check.getParent().equals(tempPanel[i])){
if(!check.isSelected()){
JLabel tempLabel = new JLabel("Command "+(i+1));
tempLabel.setFont(new Font("Franchise",Font.BOLD,122));
tempLabel.setForeground(Color.BLACK);
tempPanel[i].setBorder(lineBorder);
tempPanel2[i].add(tempLabel);
secondScreenPanel.add(tempPanel2[i]);
secondScreenPanel.revalidate();
secondScreenPanel.repaint();
break;
}
if(check.isSelected()){
tempPanel[i].setBorder(null);
secondScreenPanel.remove(tempPanel2[i]);
tempPanel2[i].removeAll();
secondScreenPanel.revalidate();
secondScreenPanel.repaint();
}
}
}
});
最佳答案
不要使用 MouseListener。
JCheckBox
设计为与 ItemListener
或 ActionListener
一起使用。阅读 Swing 教程中关于 How to Use Check Boxes 的部分了解更多信息和示例
关于JAVA JCheckBox 需要点击 2 次才能选中,点击 1 次才能取消选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30467446/