如何使用 JCheckBox 启用/禁用 JTextField?或者我的代码有什么问题?

我是 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();

    }

}


森栏
浏览 136回答 2
2回答

郎朗坤

这是一个使用 ItemListener 的快速解决方案。&nbsp;private void setupEvents() {&nbsp; &nbsp; &nbsp; &nbsp; setDefaultCloseOperation(EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i<10; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int finalI = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cb[i].addItemListener(new ItemListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void itemStateChanged(ItemEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text[finalI].setEnabled(e.getStateChange() == ItemEvent.SELECTED);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }您也可以使用 ActionListener 来完成,但解决方案有点麻烦,而且没有那么优雅。我发布这个是因为你也可以像这样解决你的问题:&nbsp;private void setupEvents() {&nbsp; &nbsp; &nbsp; &nbsp; setDefaultCloseOperation(EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i<10; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int finalI = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cb[i].addActionListener(new ActionListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text[finalI].setEnabled(!text[finalI].isEnabled() && e.getID() == ActionEvent.ACTION_PERFORMED);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

阿晨1998

问题:您永远不会调用setupEvents(),因此永远不会调用此方法中的代码final如果要在内部匿名类中使用它们,则需要创建本地字段。所以:private void setupEvents() {&nbsp; &nbsp; setDefaultCloseOperation(EXIT_ON_CLOSE);&nbsp; &nbsp; for (int i=0; i<10; i++) {&nbsp; &nbsp; &nbsp; &nbsp; final int finalIndex = i;&nbsp; &nbsp; &nbsp; &nbsp; cb[i].addActionListener(new ActionListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent event) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(cb[finalIndex].isSelected()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text[finalIndex].setEnabled(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text[finalIndex].setEnabled(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}我会做一些不同的事情,并让我的眼睛更清洁。例如,import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ItemEvent;import java.util.ArrayList;import java.util.List;import javax.swing.*;@SuppressWarnings("serial")public class CheckList2 extends JPanel {&nbsp; &nbsp; public static final int TEXT_FIELD_COUNT = 10;&nbsp; &nbsp; private List<JTextField> fields = new ArrayList<>();&nbsp; &nbsp; private JButton sendBtn = new JButton(new SendAction("Send Checklist"));&nbsp; &nbsp; public CheckList2() {&nbsp; &nbsp; &nbsp; &nbsp; JPanel checkPanel = new JPanel();&nbsp; &nbsp; &nbsp; &nbsp; checkPanel.setLayout(new GridLayout(0, 1, 1, 1));&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < TEXT_FIELD_COUNT; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JCheckBox checkBox = new JCheckBox("", true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // final so can use within item listener&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final JTextField textField = new JTextField(20);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textField.setEnabled(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fields.add(textField);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkBox.addItemListener(itemEvent -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // set textfield enabled based on checkbox state&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textField.setEnabled(itemEvent.getStateChange() == ItemEvent.DESELECTED);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JPanel rowPanel = new JPanel();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowPanel.add(checkBox);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowPanel.add(Box.createHorizontalStrut(3));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowPanel.add(textField);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkPanel.add(rowPanel);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; setLayout(new BorderLayout());&nbsp; &nbsp; &nbsp; &nbsp; add(checkPanel, BorderLayout.CENTER);&nbsp; &nbsp; &nbsp; &nbsp; add(sendBtn, BorderLayout.PAGE_END);&nbsp; &nbsp; }&nbsp; &nbsp; private class SendAction extends AbstractAction {&nbsp; &nbsp; &nbsp; &nbsp; public SendAction(String name) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int mnemonic = name.charAt(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; putValue(MNEMONIC_KEY, mnemonic);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (JTextField jTextField : fields) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(jTextField.getText());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static void createAndShowGui() {&nbsp; &nbsp; &nbsp; &nbsp; CheckList2 mainPanel = new CheckList2();&nbsp; &nbsp; &nbsp; &nbsp; JFrame frame = new JFrame("CheckList2");&nbsp; &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; frame.getContentPane().add(mainPanel);&nbsp; &nbsp; &nbsp; &nbsp; frame.pack();&nbsp; &nbsp; &nbsp; &nbsp; frame.setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; SwingUtilities.invokeLater(() -> createAndShowGui());&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java