2016-04-01 17 views
0

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:

  1. Bir JFrame düzenlendi ve bir düğme, etiket ve metin alanı eklendi.

  2. 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

  3. "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.

  4. 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); 
     } 
     } 
    } 

} 

cevap

0

Eminim% 100 emin değilim ama iç sınıfa ihtiyacınız yok. Eğer iç sınıf kaldırmak ve onun yerine pack()repaint yönteme çağıran gerektiğini, bunun nedeni çalışması gerekir actionPerformed içindeki pack() eklemek, sonra RegularExpressionGui sınıfına ActionListener uygulamak eğer sorun için

. 1. kodunu çağırırsanız ve pencereyi yeniden boyutlandırırsanız, şimdi onu görmelisiniz, çünkü yeniden boyutlandırılırken Düzen Yöneticisi, yeniden doğrulama yöntemini çağırıyor.

Yani, sınıf şu anda bu gibi görünmelidir:

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 implements ActionListener { 

    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(this); 
     textField1.addActionListener(this); 
     pack(); 
     setVisible(true); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } //end constructor 

    //inner class containing ActionListner 
    @Override 
    public void actionPerformed(ActionEvent event) { 
     if (event.getSource() == button1) { 
     textField1.setVisible(true); 
     //label1.setVisible(true); 
     pack(); 
     } 
    } 
    public static void main (String args[]) { 
    new RegularExpressionGui(); 
    } 
} 
İlgili konular