选择/未选择单选按钮时如何隐藏文本字段?

时间:2014-01-14 19:01:00

标签: java swing jtextfield jradiobutton changelistener

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

我的问题是,当没有选择保存encripted的选项时,是否存在使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);
        }
    });
}
}

2 个答案:

答案 0 :(得分:5)

  

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

你试过一个简单的

吗?
textField.setEditable(false);

或者

textField.setEnabled(false);

答案 1 :(得分:1)

在您的代码中观察。

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);
        }
    });
}
相关问题