【发布时间】:2014-08-11 19:42:43
【问题描述】:
所以我需要制作一个可以在文本字段中读取用户输入的 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"));
}
【问题讨论】:
-
我没有看到您发布了 ActionListener 及其 actionPerformed 方法,显示您尝试解决此问题。请发布此内容,以便我们查看您可能做错了什么并更好地了解您的问题。另外请注意您的代码格式,每个缩进使用 4 个空格(无制表符),并为每个级别使用常规一致的缩进。这将极大地帮助我们理解您的代码。
标签: java arrays swing jtextfield