2014-01-14 38 views
0

我正在写一个程序来引用数据。它有一个JTextArea来编辑文本,但我想选择是将它保存为encripted还是纯文本。为此,我创建了一个在点击保存按钮时出现的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); 
     } 
    }); 
} 
} 

回答

5

我的问题是,如果有使文本字段没有可用的和半透明的简单方式,

您是否尝试过一个简单的

textField.setEditable(false); 

textField.setEnabled(false); 
+0

谢谢,我认为这很好。但是,你能帮我使用ChangeListener吗?它不工作。或者我应该问一个新的问题呢? – Junior

+0

是的,新的问题。似乎没有关系。 –

+1

同时查看'setEnabled(false)',它给出了*可视化指示*用户不应该输入它(该字段甚至不可聚焦)。 –

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