【问题标题】:How to make a TextField visible when a button is clicked in Java?在 Java 中单击按钮时如何使 TextField 可见?
【发布时间】:2016-04-01 16:53:21
【问题描述】:

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

以下是迄今为止我为实现这一点所采取的步骤:

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

  2. 最初将文本字段的可见性设置为“false”。标签和按钮 可见性设置为“true”

  3. 实现了 ActionListener 接口并重写了 ActionPerformed 方法,以在单击按钮时将 textField 可见性更改为“true”。

  4. 将 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


    【解决方案1】:

    我不是 100% 确定,但我认为您不需要内部类。

    对于您的问题,如果您删除内部类,而是在您的 RegularExpressionGui 类上实现 ActionListener,然后在 actionPerformed 内添加 pack() 它应该可以工作,因为 pack() 应该调用repaint 方法。如果您调用第一个代码并调整窗口大小,您现在应该可以看到它,因为在调整大小时,布局管理器正在调用 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();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多