java - 将 JTextField 中的内容保存到数组中

标签 java arrays swing

我正在尝试从 JTextFields 读取内容,然后将内容保存到并行数组中,这将进入 readFields() 方法,但我不知道从哪里开始,我对数组也很陌生

我的代码如下

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

     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
     //---------------------------------------
     public void readFields()
     {
     }

     public void fieldsExist()
     {
     }

     public void fieldsEmpty()
     {
     }

     public void actionPerformed(ActionEvent e)
     {
     }


}

以及我设置一些对象的另一个类

import javax.swing.*;

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

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

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


}  

// -----------------------------------
// Problem Description:
//   Inheritance from JTextField Class
//
// Demonstrates:
//   Inheritance in Java
//   (1) super method
//   (2) private member function
//   (3) private static data members
// -----------------------------------

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

public class FormJTextField extends JTextField
{
  private final static int FONT_SIZE   = 14;
  private final static int RED_LEVEL   = 220;  // 0 -> 255 allowed
  private final static int GREEN_LEVEL = 220;  // 0 -> 255 allowed
  private final static int BLUE_LEVEL  = 100;  // 0 -> 255 allowed
  private final static int WIDTH = 20;


  // -------------------------------------------------------------
  // Default constructor creates a JTextField with a default width
  // -------------------------------------------------------------
  public FormJTextField()
  {
    super("", WIDTH);

  }

  // ----------------------------------------------------
  // Create a JTextField with a width equal to the String
  // length and initially display the supplied String
  // ----------------------------------------------------
  public FormJTextField(String s)
  {
    super(s);

  }

  // ------------------------------------------------
  // Create a JTextField using the supplied width and
  // initially display the supplied String
  // ------------------------------------------------
  public FormJTextField(String s, int w)
  {
    super(s, w);

  }

  // -------------------------------------------
  // Create a JTextField with the supplied width
  // -------------------------------------------
  public FormJTextField(int w)
  {
    super(w);

  }



}

最佳答案

好吧,从概念上讲,这是非常基本的 Java 101。我鼓励您仔细查看 How to use arrays有关如何使用数组的更多详细信息。

基本上,您只需要某种可以从 0 计数到 FIELDS - 1 的循环,并从 tf 中的每个字段获取文本。 code> 数组并将值分配给相应的 strings 元素,例如

public void readFields()
{
    for (int index = 0; index < FIELDS; index++) {
        strings[index] = tf[index].getText();
    }
}

关于java - 将 JTextField 中的内容保存到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36607773/

相关文章:

JavaScript - 将事件监听器添加到对象数组

java - 从 vlcj 播放器数组中播放视频

java - java 被滥用的背景

c++ - 动态数组推送功能 - 它是如何工作的?

Javascript/Jquery JSON 对象到对象内的数组

java - 绘制椭圆形组件,但填充与椭圆形线重叠,如何解决此问题?

java - Jetty ALPN 未正确配置 - Hadoop/Google Bigtable

java - 如何在菜单项的右侧显示图标?

java - 在 JavaScript 中解析来自 Java Jackson JSON 的 JSON

java - 有没有关于可伸缩 Web 应用程序(Java/Ajax/REST)的好书?