如何在Java中单击按钮时使TextField可见?

时间:2016-04-01 16:53:22

标签: java swing user-interface jtextfield visible

我正在尝试创建一个为用户提供按钮的GUI程序。单击此按钮时,它将显示textField并提示用户输入正则表达式。

以下是我迄今为止采取的步骤:

  1. 创建了一个JFrame并添加了一个按钮,标签和文本字段。

  2. 将文本字段可见性设置为" false"原来。标签和按钮     可见性设置为" true"

  3. 实现ActionListener接口并覆盖ActionPerformed方法,将textField可见性更改为" true"单击按钮时。

  4. 将ActionListener实例注册到按钮。

  5. 当我以这种方式运行时,单击按钮后文本字段不可见(它编译得很好,但GUI中没有任何反应)

    但是,如果我将标签可见性更改为" false"最初,然后添加一个动作设置为" true"在ActionListener中,它可以工作,并且标签和文本字段在单击按钮时变得可见。

    我想知道的是,当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);
             }
           } 
      }
    
    }
    

1 个答案:

答案 0 :(得分:0)

我不是百分百肯定,但我认为你不需要内心阶层。

对于您的问题,如果删除了内部类,而是在ActionListener类上实现RegularExpressionGui,那么在pack()内添加actionPerformed它应该有效,因为pack()应该调用repaint方法。如果您调用第一个代码并调整窗口大小,您现在应该看到它,因为在调整大小时,布局管理器正在调用重绘方法。

所以,你的课程现在应该像这样:

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();
  }
}