2014-01-14 28 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); 
     } 
    }); 
} 
相關問題