我在运行时使用复选框禁用/启用一些 JComponent 时遇到了一些困难。我尝试过执行 if(checkbox.isSelected(){}
但没有成功。当我尝试添加 addActionListener(this)
时,出现错误“方法” AbstractButton 类中的 addActionListiner 无法应用于给定类型:必需的操作监听器:找到 JudgeMain(其类名) - 在构造函数中泄漏“this”
public class JudgeMain extends JFrame {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
LogInJ id = new LogInJ();
public String IdNumber;
public JudgeMain(LogInJ id)
{
initComponents();
ButtonGroup();
this.id = id;
initDetails();
yesCB.addActionListener(this);
if(yesCB.isSelected())
{
timeF.setEnabled(true);
catF.setEnabled(true);
yearsCB.setEnabled(true);
monthsCB.setEnabled(true);
}
}
感谢帮助,谢谢
最佳答案
类JudgeMain
不代表类型ActionListener
。
您需要实现此接口(interface)才能调用
yesCB.addActionListener(this);
或者只使用匿名监听器(注意,无需检查来源):
yesCB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timeF.setEnabled(yesCB.isSelected());
catF.setEnabled(yesCB.isSelected());
yearsCB.setEnabled(yesCB.isSelected());
monthsCB.setEnabled(yesCB.isSelected());
}});
<小时/>
旁注:首选方法是创建 JFrame
的实例并直接使用,而不是对该类进行子类化。
关于java - 在 Java 中使用复选框启用/禁用 JComponent 存在困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14383800/