所以我需要制作一个可以读取文本字段中的用户输入的 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/