我在从 Java 中的“二维数组”中检索值时遇到问题。我知道在 Java 中并没有像二维数组这样的东西,但请耐心等待。我对 Java 很陌生,正在为我的班级做一个项目。我应该将信息从 jTextField 提交到数组,然后将存储在数组中的信息拉回以获取摘要报告。这是我到目前为止所拥有的:
public class KETTask1UI extends javax.swing.JFrame {
//Creates the "2d array" to store minutes worked and payment values
int[][] paymentArray = new int [20][2];
/**
* Creates new form KETTask1UI
*/
public KETTask1UI() {
initComponents();
}
private void runreportActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
for (int i=0; i<paymentArray.length; i++) {
for (int j=0; j<paymentArray[i].length; j++) {
//Blanks out the displayTextArea
displayTextArea.setText(" ");
//Displays the contents of the array in the displayTextArea
displayTextArea.append("History of minutes and payment entered:\n");
displayTextArea.append(String.valueOf(paymentArray[i][j]));
}
}
}
private void minutesActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void quitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int min = Integer.parseInt(minutes.getText());
int pay = Integer.parseInt(payment.getText());
for (int i=0; i<paymentArray.length; i++) {
for (int j=0; j<paymentArray[i].length; j++) {
//Sets the amount of minutes worked and payment to the array
paymentArray[i][0] = min;
paymentArray[i][1] = pay;
//display a message to the user to let them know the values that they've entered.
displayTextArea.append("******************\nRaw Tutoring Earnings Data\n\nMinutes Earnings\n");
displayTextArea.append(String.valueOf(paymentArray[i][j]));
displayTextArea.append("\n\n******************");
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new KETTask1UI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextArea displayTextArea;
private javax.swing.JButton enterButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField minutes;
private javax.swing.JTextField payment;
private javax.swing.JLabel paymentLabel;
private javax.swing.JButton quit;
private javax.swing.JButton runreport;
private javax.swing.JLabel timeLabel;
// End of variables declaration
}
现在,目前当我点击进入按钮时,它给我的只是以下内容(30 是最后一个分钟数,20 是最后一个付款数字):
******************
Raw Tutoring Earnings Data
Minutes Earnings
30
************************************
Raw Tutoring Earnings Data
Minutes Earnings
20
此消息在 textArea 中重复 20 次。如果我输入更多数据,它只会对新数字做同样的事情。
我需要它看起来像这样:
******************
Raw Tutoring Earnings Data
Minutes Earnings
20 30
******************
当我输入新值并再次按下回车按钮时,我需要它显示一个更新的列表,如下所示:
******************
Raw Tutoring Earnings Data
Minutes Earnings
20 30
30 15
******************