【发布时间】:2016-04-01 16:53:21
【问题描述】:
我正在尝试创建一个为用户提供按钮的 GUI 程序。单击此按钮时,它会显示一个文本字段并提示用户输入正则表达式。
以下是迄今为止我为实现这一点所采取的步骤:
创建了一个 JFrame 并添加了一个按钮、标签和文本字段。
最初将文本字段的可见性设置为“false”。标签和按钮 可见性设置为“true”
实现了 ActionListener 接口并重写了 ActionPerformed 方法,以在单击按钮时将 textField 可见性更改为“true”。
将 ActionListener 实例注册到按钮。
当我以这种方式运行它时,单击按钮后文本字段不可见(编译正常,但 GUI 中没有任何反应)
但是,如果我最初将标签可见性更改为“false”,然后在 ActionListener 中添加一个设置为“true”的操作,它会起作用,并且在单击按钮时标签和文本字段都变得可见。
我想知道的是为什么文本字段只有在 ActionListener 中包含标签时才可见?
换句话说,为什么这段代码不起作用?
import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class RegularExpressionGui extends JFrame {
private final JLabel label1; //label
private final JTextField textField1;
private JTextField textField2; //text field
private final JButton button1; //button
private final JTextArea textArea1; //textArea
public RegularExpressionGui() {
super("Regular Expression Lookup");
setLayout(new FlowLayout()); //GUI Layout
//add the components
label1 = new JLabel("Regular Expression");
label1.setVisible(true);
add(label1);
textField1 = new JTextField("Enter Regular Expression here");
textField1.setVisible(false);
add(textField1);
button1 = new JButton("Lookup Expression");
add(button1);
textArea1 = new JTextArea("Results");
add(new JScrollPane(textArea1)); //makes the output area scrollable
LookupHandler lookup = new LookupHandler();
button1.addActionListener(lookup);
textField1.addActionListener(lookup);
} //end constructor
//inner class containing ActionListner
private class LookupHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button1) {
textField1.setVisible(true);
}
}
}
}
为什么这个会起作用?
import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class RegularExpressionGui extends JFrame {
private final JLabel label1; //label
private final JTextField textField1;
private JTextField textField2; //text field
private final JButton button1; //button
private final JTextArea textArea1; //textArea
public RegularExpressionGui() {
super("Regular Expression Lookup");
setLayout(new FlowLayout()); //GUI Layout
//add the components
label1 = new JLabel("Regular Expression");
label1.setVisible(false);
add(label1);
textField1 = new JTextField("Enter Regular Expression here");
textField1.setVisible(false);
add(textField1);
button1 = new JButton("Lookup Expression");
add(button1);
textArea1 = new JTextArea("Results");
add(new JScrollPane(textArea1)); //makes the output area scrollable
LookupHandler lookup = new LookupHandler();
button1.addActionListener(lookup);
textField1.addActionListener(lookup);
} //end constructor
//inner class containing ActionListner
private class LookupHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button1) {
textField1.setVisible(true);
label1.setVisible(true);
}
}
}
}
【问题讨论】:
标签: java swing user-interface jtextfield visible