Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取文本字段数组_Java_Arrays_Swing_Jtextfield - Fatal编程技术网

Java 读取文本字段数组

Java 读取文本字段数组,java,arrays,swing,jtextfield,Java,Arrays,Swing,Jtextfield,所以我需要制作一个Java程序,可以读取文本字段中的用户输入。我能够设置文本字段的数组,但是读取输入并将数据存储在新数组中让我非常烦恼。我为按钮做了一个监听器,我只需要弄清楚如何将文本字段数组中输入的信息存储到成绩数组中,这样我就可以对成绩进行计算。我是这个网站的新手,非常感谢您的帮助 //an Array for test scores and one to hold the input grades JTextField[] testScores; double[] grade;

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

//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"));
}
//一个用于测试分数的数组和一个用于保存输入分数的数组
JTextField[]测试分数;
双[]级;
/**
建造师
*/
公共统计计算器()
{
//显示标题
setTitle(“JP Stearns”);
//指定关闭按钮的操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建边框布局
setLayout(新的BorderLayout());
//创建自定义面板
buildScoresPanel();
buildStatisticsPanel();
//构建按钮面板
buildButtonPanel();
//将组件添加到内容窗格
添加(scoresPanel,BorderLayout.NORTH);
添加(统计面板,BorderLayout.CENTER);
添加(按钮面板,边界布局。南部);
//打包窗口的内容以显示它。
包装();
setVisible(真);
}
//创建GridLayout管理器
//有1行4列。
scoresPanel.setLayout(新网格布局(1,4));
//使用数组创建4个文本字段
testScores=新的JTextField[4];
for(int index=0;index
我认为需要更多的信息,但是看起来你可以做到 类似这样的代码(psuedo代码):

JButton按钮=新的JButton(“这是L”);
addActionListener(新建ActionListener()){
已执行的公共无效操作(操作事件e){

对于(int i=0;我看不到您发布了ActionListener及其actionPerformed方法,显示您刚刚试图解决此问题。请发布此内容,以便我们可以看到您可能做错了什么,更好地理解您的问题。另外,请注意您的代码格式,使用4个空格(无制表符)对于每个缩进,并对每个级别使用规则一致的缩进。这将极大地帮助我们理解您的代码。我下班回家后会尝试一下,谢谢。
 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%
             }