[英]How to hide a Text Field when a radio button is selected/not selected?
I'm writting a program to encript data. 我正在编写一个程序来加密数据。 It has a JTextArea to edit text, but I want to choose if I save it encripted or in plain text.
它具有一个JTextArea来编辑文本,但是我想选择是否将其保存为抄写文本或纯文本格式。 For this I created a JDialog that appears when the save button is clicked.
为此,我创建了一个JDialog,单击“保存”按钮时将出现该对话框。 It contains two radio buttons: one to save the data encripted and the other to save in plain text.
它包含两个单选按钮:一个用于保存加密的数据,另一个用于以纯文本格式保存。 In the middle of them there is a JPasswordField requesting the key of the encription.
在它们中间,有一个JPasswordField请求加密密钥。
My question is if there is a simple way of making the TextField not useable and half transparent, when the option to save encripted is not selected. 我的问题是,当未选择保存已加密的选项时,是否存在一种使TextField不可用且半透明的简单方法。 Or, if there isn't a simple way of doing it, a way to hide the TextArea.
或者,如果没有简单的方法可以隐藏TextArea。 I tryed using a ChangeListener on the radio button but it isn't working.
我尝试在单选按钮上使用ChangeListener,但是它不起作用。 Here is my code:
这是我的代码:
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);
}
});
}
}
My question is if there is a simple way of making the TextField not useable and half transparent,
我的问题是,是否有一种简单的方法可以使TextField不可用且半透明,
Have you tried a simple 你尝试过一个简单的
textField.setEditable(false);
Or 要么
textField.setEnabled(false);
Observation in your code. 在您的代码中观察。
1) Add setVisible
at the end after all the UI components' properties, events etc are set. 1)在设置所有UI组件的属性,事件等之后,最后添加
setVisible
。
2) In your case you need a listener for each of the RadioButton. 2)在您的情况下,您需要每个RadioButton的侦听器。
3) Also I prefer using an ItemListener to a ChangeListener(fires even when mouse is moved over it). 3)我也更喜欢使用ItemListener而不是ChangeListener(即使将鼠标移到它上面也会触发)。
Please check the code below. 请检查下面的代码。
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);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.