java - 读取文本字段数组

标签 java arrays swing jtextfield

所以我需要制作一个可以读取文本字段中的用户输入的 Java 程序。我能够设置文本字段数组,但读取输入并将数据存储在新数组中让我非常困扰。我为按钮创建了一个监听器,我只需要弄清楚如何将 textField 数组中输入的信息存储到成绩数组中,以便我可以对成绩进行计算。我是这个网站的新手,感谢您的帮助

//an Array for test scores and one to hold the input grades
  JTextField[] testScores;
  double[] grade;

/**
    Constructor
*/

public StatisticsCalculator()
{
    //Display a Title
    setTitle("JP Stearns");

    //Specify the action for the Close button
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create a Border Layout
    setLayout(new BorderLayout());

    //Create the Custom Panels
    buildScoresPanel();
    buildStatisticsPanel();

    //Build the Button Panel
    buildButtonPanel();

    //Add the Components to the content pane
    add(scoresPanel, BorderLayout.NORTH);
    add(statisticsPanel, BorderLayout.CENTER);
    add(buttonPanel, BorderLayout.SOUTH);

    //Pack the contents of the Window to display it.
    pack();
    setVisible(true);
}

    //Create a GridLayout manger
    //with 1 row 4 columns.
    scoresPanel.setLayout(new GridLayout(1,4));

    //Create 4 text fields using an array
    testScores = new JTextField[4];

    for (int index = 0; index < testScores.length; index++)
        {
            testScores[index] = new JTextField(4);
            scoresPanel.add(testScores[index]);
        }

    //Border the panel
    scoresPanel.setBorder(BorderFactory.createTitledBorder("Test Scores"));

private void buildScoresPanel()
{
    //Create a panel for the test scores
    scoresPanel = new JPanel();

    //Create a GridLayout manger
    //with 1 row 4 columns.
    scoresPanel.setLayout(new GridLayout(1,4));

    //Create 4 text fields using an array
    testScores = new JTextField[4];

    for (int index = 0; index < testScores.length; index++)
        {
            testScores[index] = new JTextField(4);
            scoresPanel.add(testScores[index]);
        }

    //Border the panel.v
    scoresPanel.setBorder(BorderFactory.createTitledBorder("Test Scores"));
}

最佳答案

我认为需要更多信息,但看起来你可以做到 像这样的(伪代码):

 JButton button = new JButton("This is L");
  button.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) { 

          for(int i=0; i<testScores.length(); i++)
                grade[i]=Double.parseDouble(testScores[i].getText());
                 //then do calculations like grade[i] * 100%
             }

关于java - 读取文本字段数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25251107/

相关文章:

java - 从 4 个 TextView 中选择一个 TextView 并同时更改其颜色

java - 空白敏感属性 key 需要 Nifi 迁移

java - 如何计算并仅打印重复项?

java - 无法在eclipse中设置共享库路径

python - 查询保存为 npz 的 NumPy 数组的 NumPy 数组很慢

c++ - 如何在 vector C++ 中打印元素

java - 图像未出现在小程序中

c - 将字符串拆分为C中的整数数组

java - 动态添加图像到 JTable 单元格

java - sleep 一个java线程但首先更新jFrame