java - 使用 JButtons 清除 JTextFields

标签 java arrays methods

我已将 JButton cl 指定为清除, 但是在我的程序中,并使用 (e.getSource() == cl)...它不会为每个文本字段 setText("") 我不确定是因为我为 JTextField 使用了数组还是什么......

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class EmployeesApplet extends JApplet implements ActionListener 
{
  public JButton              sd   = new JButton ("Salaried");
  public JButton              hr   = new JButton ("Hourly");
  public JButton              cm   = new JButton ("Commissioned");
  public JButton              cl   = new JButton ("Clear"); 

  private final int    FIELDS      =  8,   
                       FIELD_WIDTH = 20;   

  private String[]     strings     = new String[FIELDS];
  private TextFieldWithLabel[] tf  = new TextFieldWithLabel[FIELDS];
  private JTextArea    ta          = new JTextArea(5,25); 

  // Add arrays for readFields() method



     public void init()
     {
      String[]  s = {"First Name", "Last Name", "Employee ID", "(a) Salaried: Weekly Salary", "(b1) Hourly 1: Rate Per Hour", 
                   "(b2) Hourly 2: Hours Worked" , "(c1) Commissioned: Rate", "(c2) Commissioned: Gross Sales" };

       //----------------------
       //  Set up the Structure
       //----------------------

       Container c = getContentPane();
       JPanel f   = new JPanel(new FlowLayout());
       JPanel b   = new JPanel(new BorderLayout(2,0));

       JPanel glb = new JPanel(new GridLayout(8,1,0,2));
       JPanel gtf = new JPanel(new GridLayout(8,1,0,2));
       JPanel flb = new JPanel(new FlowLayout());


       // Add FlowLayout to the container
       c.add(f);
       // Add BorderLayout to the FlowLayout
       f.add(b);

       //---------------------------------------
       //Add JPanels to the BorderLayout regions
       //---------------------------------------

       // Add JLables to GridLayout in West
       b.add(glb, BorderLayout.WEST);
       for (int i = 0; i < tf.length; i++)
       {
        tf[i] = new TextFieldWithLabel(s[i], FIELD_WIDTH);
        glb.add(tf[i].getLabel());
       }

       // Add JTextFeilds to GridLayout in East
       b.add(gtf, BorderLayout.EAST);
       for (int i = 0; i < tf.length; i++)
       {
        tf[i] = new TextFieldWithLabel(s[i], FIELD_WIDTH);
        tf[i].getTextField();
        gtf.add(tf[i].getTextField());
       }

       // Add JButtons to FlowLayout in South
       b.add(flb, BorderLayout.SOUTH);

       flb.add(sd);
       flb.add(hr);
       flb.add(cm);
       flb.add(cl);

       sd.addActionListener(this);
       hr.addActionListener(this);
       cm.addActionListener(this);
       cl.addActionListener(this);

       // Add JTextArea and make it not editable   
       f.add(ta);
       ta.setEditable(false);

     }

     //---------------------------------------
     //  Read all the JTextFields and 
     //  save the contents in a parallel array
     //---------------------------------------
     private void readFields()
     {
       for (int i = 0; i < tf.length; i++)  // or FIELDS
         strings[i] = tf[i].getText();
     }

     //---------------------------------------------------------------------------
     //  Returns true if required JTextFields for selected employee are not empty
     //  Checks required JTextFields in top down order,
     //    displays error in stats are for first req that is empty and places focus
     //----------------------------------------------------------------------------
     private boolean fieldsExist(int i, int i2)
     {
       for (int index = 0; index < tf.length; index++)
        {

        }  
        showStatus("field is empty");  // Diplays error message in status area
        tf[i].requestFocus();  // places focus in JTextField

        return true;
      }

     //-----------------------------------------------------------------------------------------
     //  Returns true if all non-required JTextFields for the seleceted employee are empty
     //  Checks non-required JTextFields in top-down order ,
     //    displays error message in first non-req JTextField that is not empty and places focus
     //-----------------------------------------------------------------------------------------

     private boolean fieldsEmpty(int i, int i2)
     {



       showStatus("field should be empty");  //  Diplays error message in status area
       tf[i].requestFocus();  //  Places focus in JTextField

       return true;
     }

     public void actionPerformed(ActionEvent e)
     {

       if (e.getSource() == cl)
       {
         for (int i = 0; i < tf.length; i++)
         {
           tf[i].setText("");
           tf[1].requestFocus();
         }
        }  //  End clear if


     }


}

这是 TextFieldWithLabel 类

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class TextFieldWithLabel  extends JTextField
{
  private JTextField text_field;
  private JLabel         label;
  private final static int WIDTH = 20;

  public TextFieldWithLabel (String s, int w)
  {
    label      = new JLabel(s);
    text_field = new JTextField(w);
  }

  public JLabel         getLabel()     {return label;}
  public JTextField     getTextField() {return text_field;}
  public String         getText()      {return text_field.getText();}


}  

最佳答案

您想要设置 TextFieldWithLabeltext_field 字段的文本,而不是 TextFieldWithLabel 的文本。

tf[i].getTextField().setText("");
tf[1].getTextField().requestFocus();

请注意,您可以删除此 text_field 属性,并直接使用 TextFieldWithLabel 作为 JTextField

更好的是,让 TextFieldWithLabel 扩展 JTextField,因为它没有用,它只是两个组件的容器类。

关于java - 使用 JButtons 清除 JTextFields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36627958/

相关文章:

java - 使用 java 1.8、jack 和 lambda Android gradle 编译失败

Java 性能 - ArrayLists 与 Arrays 相比,可实现大量快速读取

javascript - 下划线从数组中删除某些属性上包含空值的对象

java - 将多个 Java 方法转换为非阻塞线程运行?

VBA - 是否可以将对象的属性作为方法中的参数传递?

java - 复制和排序数组中的错误

与 MySQL 连接的 Java 应用程序死机

java - Swing GUI 布局在 4K 面板中很奇怪

arrays - 使用下标和线性索引分配给数组的 Julia 语言问题

ios - 保存多个选定的 UITableViewCell 值的数组