Java Swing - 如何如果未選中,請禁用JCheckBox
我們想知道如何如果未選中,請禁用JCheckBox。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
JCheckBox checkBox = new JCheckBox("Enabled", true);
checkBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (checkBox.isEnabled())
checkBox.setEnabled(false);
else
checkBox.setEnabled(true);
}
});
JOptionPane.showMessageDialog(null, checkBox);
}
}