现在有一个文本框和一个复选框,需要实现的是:当选中复选框的时候,文本框变成不可用,当不选复选框的时候,文本框才可以输入文字,请教!

解决方案 »

  1.   

    复选框上加一个onclick事件,判断复选框是否被选中,然后动态设置文本框的readOnly属性。
      

  2.   

    我做了个实验,你看符合你要求不~
    /*
     * Created on 2006-7-18
     *
     * To change the template for this generated file go to
     * Window>Preferences>Java>Code Generation>Code and Comments
     */
    package radio_test;import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;/**
     * @author xiaoxianggua
     *
     * To change the template for this generated type comment go to
     * Window>Preferences>Java>Code Generation>Code and Comments
     */
    public class RadioTest extends JFrame implements ActionListener{ public JCheckBox checkBox = new JCheckBox();
    public JTextArea textArea = new JTextArea("hello word!");
    public static void main(String[] args) {
    RadioTest myFrame = new RadioTest();
    myFrame.setSize(400,300);
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.setVisible(true);

    }
    public RadioTest(){
    Container container = getContentPane();
    container.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
    container.add(checkBox);
    container.add(textArea);

    textArea.setEnabled(false);
    textArea.setVisible(true);

    checkBox.addActionListener(this);
    } /* (non-Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent e) {
    if(e.getSource() == checkBox){
    if(textArea.isEnabled())
    textArea.setEnabled(false);
    else
    textArea.setEnabled(true);
    }

    }
    }
      

  3.   

    如果是在jsp里面,实现起来更简单。用javascript,几句代码就可以搞定了~
      

  4.   

    <script language=javascript>
    function check(obj){
     document.getElementById("userId").disabled=obj.checked;
    }
    </script>
    <input type="checkbox" onclick="check(obj);">choose
    <input type="text" id="userId" disabled>默认为不选中
      

  5.   

    不好意思,写错了,应该是onclick="check(this);"