Düğmeli bir kullanıcı sağlayan bir GUI programı oluşturmaya çalışıyorum. Bu düğmeye tıklandığında, bir textField görüntüler ve kullanıcıdan normal bir ifade girmesini ister.Java'da bir düğmeye tıklandığında bir TextField nasıl görünür?
İşte bu uygulamaya şu ana kadar yaptığınız adımlar şunlardır:
- Bir JFrame düzenlendi ve bir düğme, etiket ve metin alanı eklendi.
Metin alanı görünürlüğünü başlangıçta "false" olarak ayarlayın. Etiket ve düğme görünürlük ActionListener arayüzü Uygulanan
"true" olarak ayarlanmış ve butonuna tıklandığında textField görünürlüğünü "true" olarak değiştirmek için actionPerformed yöntemini overrode edilir.
ActionListener örneğini tuşa kaydettirin.
Ben bu şekilde çalıştırdığınızda, metin alanı butonuna tıkladıktan sonra görünür hale DEĞİLDİR (ince derler, ama hiçbir şey GUI olur) Ben etiket görünürlüğü değiştirirseniz,
Ancak önce "false", sonra ActionListener'da "true" olarak ayarlamak için bir eylem ekleyin, işe yaramaz ve etiketin BOTH düğmesine basılır ve metin alanı düğmesine tıklandığında görünür hale gelir.
Bilmek istediğim, ActionListener'da bir etiketin bulunması durumunda metin alanının neden görünür olduğudur. Başka bir deyişle, neden bu kod çalışmıyor?
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);
}
}
}
}
Neden bunun yerine çalışıyor?
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);
}
}
}
}