我是 Java 编程新手,我在 JTextfield 数组旁边有一个 JCheckBox 数组。
我需要在检查时制作一个 CheckBox Deactivate JTextField,但我没有成功
我怎样才能使它与动作监听器一起工作?
这是我的代码:
public class Checklist_Complete extends JFrame {
private JLabel description;
private JButton send;
private JTextField text[]=new JTextField[10];
private JCheckBox cb[]=new JCheckBox[10];
public Checklist_Complete() {
setTitle("Activities");
setSize(500,300);
setupWidgets();
setVisible(true);
}
private void setupWidgets() {
JPanel pn_center = new JPanel(new GridLayout(10,1));
JPanel pn_west = new JPanel(new GridLayout(10,1));
description = new JLabel("List your activities and uncheck the irrelevant ones");
send = new JButton("Send Checklist");
for (int i=0; i<10; i++) {
text[i] = new JTextField();
cb[i] = new JCheckBox("", true);
}
add(description, BorderLayout.NORTH);
add(pn_center, BorderLayout.CENTER);
add(pn_west, BorderLayout.WEST);
add(send, BorderLayout.SOUTH);
for (int i=0; i<10; i++){
pn_center.add(text[i]);
pn_west.add(cb[i]);
}
}
private void setupEvents() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
for (int i=0; i<10; i++) {
cb[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if(cb[i].isSelected()){
text[i].setEnabled(false);
} else{
text[i].setEnabled(true);
}
}
});
}
}
public static void main(String[] args) {
new Checklist_Complete();
}
}
郎朗坤
阿晨1998
相关分类