【问题标题】:How to hide a Text Field when a radio button is selected/not selected?选择/未选择单选按钮时如何隐藏文本字段?
【发布时间】:2014-01-14 19:01:00
【问题描述】:

我正在编写一个程序来加密数据。它有一个 JTextArea 来编辑文本,但我想选择是将其保存为加密文本还是纯文本。为此,我创建了一个在单击保存按钮时出现的 JDialog。它包含两个单选按钮:一个用于保存加密数据,另一个用于以纯文本格式保存。在它们中间有一个 JPasswordField 请求加密的密钥。

我的问题是,如果没有选择保存加密的选项,是否有一种简单的方法可以使 TextField 不可用且半透明。或者,如果没有简单的方法,可以隐藏 TextArea。我尝试在单选按钮上使用 ChangeListener,但它不起作用。这是我的代码:

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class StackOverflowVersion extends JFrame {

public static JFrame frame;

public StackOverflowVersion() {
    dialogoCriptografar();
    System.exit(EXIT_ON_CLOSE);
}

public void dialogoCriptografar(){
    final ButtonGroup bGroup = new ButtonGroup();
    JRadioButton[] buttons = new JRadioButton[2];
    final JPasswordField passwordField = new JPasswordField(20);

    // create the raio bunttons
    buttons[0] = new JRadioButton("Encript document before saving");
    buttons[1] = new JRadioButton("Just save it");
    //ad them to the ButtonGroup
    bGroup.add(buttons[0]);
    bGroup.add(buttons[1]);
    // select the option to encript
    buttons[0].setSelected(true);

    //creates a panel with the radio buttons and a JPasswordField
    final JPanel box = new JPanel();
    JLabel descricao = new JLabel("Choose an option to save:");
    box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
    box.add(descricao);
    box.add(buttons[0]);
    box.add(passwordField);
    box.add(buttons[1]);

    // creates the dialog
    final JDialog dialogo = new JDialog(frame, "Storage options", true);
    dialogo.setSize(275,125);
    dialogo.setLocationRelativeTo(frame);
    dialogo.setResizable(false);
    dialogo.add(box);
    dialogo.setVisible(true);

    /* Doesn't work:
    buttons[0].addChangeListener(new ChangeListener(){
        boolean visivel = true;//ele começa visivel
        public void stateChanged(ChangeEvent event){
            if(visivel){
                box.remove(password);
                SwingUtilities.updateComponentTreeUI(dialogo);
                dialogo.revalidate();
                dialogo.repaint();
                visivel = false;
            }
            else{
                box.add(password);
                SwingUtilities.updateComponentTreeUI(dialogo);
                dialogo.revalidate();
                dialogo.repaint();
                SwingUtilities.updateComponentTreeUI(dialogo);
                visivel = true;
            }
        }
    });
    */
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
           frame = new StackOverflowVersion();
           frame.setVisible(true);
        }
    });
}
}

【问题讨论】:

    标签: java swing jtextfield jradiobutton changelistener


    【解决方案1】:

    我的问题是,是否有一种简单的方法可以使 TextField 不可用且半透明,

    你试过简单的

    textField.setEditable(false);
    

    或者

    textField.setEnabled(false);
    

    【讨论】:

    • 谢谢,我认为这很好。但是你能帮我使用 ChangeListener 吗?它不工作。还是我应该为此提出一个新问题?
    • 是的,新问题。似乎无关。
    • 还可以查看setEnabled(false),它会提供视觉指示用户不应该输入它(该字段甚至无法聚焦)。
    【解决方案2】:

    观察你的代码。

    1) 设置好所有 UI 组件的属性、事件等后,在末尾添加setVisible

    2) 在您的情况下,您需要为每个 RadioButton 设置一个侦听器。

    3) 我更喜欢使用 ItemListener 而不是 ChangeListener(即使鼠标移到上面也会触发)。

    请检查下面的代码。

    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    import javax.swing.BoxLayout;
    import javax.swing.ButtonGroup;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JRadioButton;
    import javax.swing.SwingUtilities;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    public class Main extends JFrame {
    
    public static JFrame frame;
    
    public Main() {
        dialogoCriptografar();
        System.exit(EXIT_ON_CLOSE);
    }
    
    public void dialogoCriptografar(){
        final ButtonGroup bGroup = new ButtonGroup();
        final JRadioButton[] buttons = new JRadioButton[2];
        final JPasswordField passwordField = new JPasswordField(20);
    
        // create the raio bunttons
        buttons[0] = new JRadioButton("Encript document before saving");
        buttons[1] = new JRadioButton("Just save it");
        //ad them to the ButtonGroup
        bGroup.add(buttons[0]);
        bGroup.add(buttons[1]);
        // select the option to encript
        buttons[0].setSelected(true);
    
        //creates a panel with the radio buttons and a JPasswordField
        final JPanel box = new JPanel();
        JLabel descricao = new JLabel("Choose an option to save:");
        box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
        box.add(descricao);
        box.add(buttons[0]);
        box.add(passwordField);
        box.add(buttons[1]);
    
        // creates the dialog
        final JDialog dialogo = new JDialog(frame, "Storage options", true);
        dialogo.setSize(275,125);
        dialogo.setLocationRelativeTo(frame);
        dialogo.setResizable(false);
        dialogo.add(box);
    
    
        // Doesn't work:
        buttons[0].addChangeListener(new ChangeListener() {
    
            @Override
            public void stateChanged(ChangeEvent arg0) {
                // TODO Auto-generated method stub
                if(buttons[0].isSelected()) {
                    passwordField.setVisible(true);;
                    //SwingUtilities.updateComponentTreeUI(dialogo);
                //  System.out.println("asdasd");
                    box.revalidate();
                    box.repaint(); 
                }
            }
    
        });
        buttons[1].addChangeListener(new ChangeListener() {
    
            @Override
            public void stateChanged(ChangeEvent arg0) {
                // TODO Auto-generated method stub
                //System.out.println("a");
                if(buttons[1].isSelected()) {
                    passwordField.setVisible(false);;
                    //System.out.println("asdasd");
                    //SwingUtilities.updateComponentTreeUI(dialogo);
                    box.revalidate();
                    box.repaint(); 
                }
            }
        });   //
    
        dialogo.setVisible(true);
    }
    
    public static void main(String[] args) {
    
        SwingUtilities.invokeLater(new Runnable() {
    
            public void run() {
               frame = new Main();
               frame.setVisible(true);
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      • 2010-11-07
      • 2021-10-01
      • 2017-12-24
      相关资源
      最近更新 更多