私はボタンをユーザーに提供するGUIプログラムを作成しようとしています。このボタンをクリックすると、textFieldが表示され、正規表現を入力するように求められます。JavaでボタンをクリックしたときにTextFieldを表示させる方法は?
は、ここで私はこれを実装するために、これまでに撮影した手順は次のとおりです。
- は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);
}
}
}
}