所以我需要创建一个可以在文本字段中读入用户输入的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"));
}
答案 0 :(得分:1)
我认为需要更多信息,但看起来你可以做到 像这样的东西(伪代码):
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%
}