简体   繁体   English

如何使用JCheckBox使JTextField可编辑,反之亦然?

[英]How to use JCheckBox to make JTextField editable and vice versa?

I am writing an application in Java and am using Netbeans IDE. 我正在用Java编写应用程序,并且正在使用Netbeans IDE。 I have set two JCheckBox ( chk1 and chk2 ) and two JTextField ( jtextfield1 and jtextfield2 ). 我设置了两个JCheckBoxchk1chk2 )和两个JTextFieldjtextfield1jtextfield2 )。 I want that if I check chk1 , jtextfield2 will be set to uneditable and if I chk2 , jtextfield2 will be set to editable and vice versa. 我希望如果我检查chk1jtextfield2将被设置为不可编辑,如果我chk2jtextfield2将被设置为可编辑,反之亦然。

How to use JCheckBox to make JTextField editable and vice versa? 如何使用JCheckBox使JTextField可编辑,反之亦然?

With the code below, it works alright but if i check the chk2 all the text fields are set to uneditable. 使用下面的代码,它可以正常工作,但是如果我检查chk2所有文本字段都将设置为不可编辑。

private void ckDepoActionPerformed(java.awt.event.ActionEvent evt) {

   if(ckDepo.isSelected()){
   txtDeposit.setEditable(false);
   }
   else{

   txtWithdraw.setEditable(true);
   }

}                                      

private void ckWithdrawActionPerformed(java.awt.event.ActionEvent evt) {                                           
    transact="withdraw";
    if(ckWithdraw.isSelected()){
   txtWithdraw.setEditable(false);
   }
    else{
   txtDeposit.setEditable(true);
   }
}                                          

Suggestions: 建议:

  • I would use JRadioButtons all added to the same ButtonGroup. 我将使用全部添加到同一ButtonGroup的JRadioButtons。 This way selecting one JRadioButton will unselect all the others. 这样,选择一个JRadioButton将取消选择所有其他按钮。
  • I would give each JRadioButton an ItemListener that inside it enabled or disabled its adjacent JTextField. 我会给每个JRadioButton一个ItemListener,它在其中启用或禁用其相邻的JTextField。

For example: 例如:

import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class RadioBtnMayhem {
   private static final int COLUMNS = 10;

   public static void main(String[] args) {
      JPanel mainPanel = new JPanel(new GridLayout(0, 1));
      ButtonGroup btnGroup = new ButtonGroup();
      int fieldCount = 5;
      for (int i = 0; i < fieldCount; i++) {
         JRadioButton radioBtn = new JRadioButton();
         btnGroup.add(radioBtn);
         final JTextField textField = new JTextField(COLUMNS);
         textField.setEnabled(false);
         radioBtn.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
               textField.setEnabled(e.getStateChange() == ItemEvent.SELECTED);
            }
         });

         JPanel radioFieldPanel = new JPanel();
         radioFieldPanel.add(radioBtn);
         radioFieldPanel.add(textField);

         mainPanel.add(radioFieldPanel);
      }

      JOptionPane.showMessageDialog(null, mainPanel);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM