所以,从我之前的问题开始,我决定继续使用推荐的列表(谢谢你)。
我现在有一个使用列表的单个班级。此列表通过按钮更新。当用户按下按钮时,文本字段中的值将被解析为双倍并保存到列表中。我正在尝试创建一个方法,将其放入将打印出列表的按钮中。
import java.util.ArrayList;
public class foodDept extends javax.swing.JFrame {
ArrayList<MyShoppingCart> list = new ArrayList<MyShoppingCart>();
public foodDept() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
appleLabel = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
cartArea = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
appleField = new javax.swing.JTextField();
bananaField = new javax.swing.JTextField();
addTocart = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
appleLabel.setText("Apple ($1.99):");
cartArea.setColumns(20);
cartArea.setRows(5);
jScrollPane1.setViewportView(cartArea);
cartArea.setEditable(false);
jLabel1.setText("Banana ($0.99):");
addTocart.setText("Add to Cart");
addTocart.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addTocartActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(appleLabel)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(appleField)
.addGap(44, 44, 44))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(addTocart)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(bananaField, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(appleLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(appleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(21, 21, 21)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bananaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 96, Short.MAX_VALUE)
.addComponent(addTocart))
.addComponent(jScrollPane1))
.addContainerGap())
);
pack();
}// </editor-fold>
**// HEEEEEEERRRRRRRRRRRREEEEEEEEEEEEEEEEEEEEEE
void printList() {
cartArea.setText("");
for (int i = 0; i < list.length; i++){
cartArea.append(list[i].toString());
}//******** ^ THIS IS THE METHOD IM TALKING ABOUT (printList)^*******
}**
**private void addTocartActionPerformed(java.awt.event.ActionEvent evt) {
double a = Double.parseDouble(appleField.getText());
list.add(new MyShoppingCart(a));
double b = Double.parseDouble(bananaField.getText());
list.add(new MyShoppingCart(b));
appleField.setText("");
bananaField.setText("");
printList();
} //where the values are parsed**
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(foodDept.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(foodDept.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(foodDept.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(foodDept.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new foodDept().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton addTocart;
private javax.swing.JTextField appleField;
private javax.swing.JLabel appleLabel;
private javax.swing.JTextField bananaField;
private javax.swing.JTextArea cartArea;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}
答案 0 :(得分:0)
您尝试将List
用作Array
,但这是错误的。
使用下一个:
for (int i = 0; i < list.size(); i++){
cartArea.append(list.get(i).toString());
}
list.size()
就像array.length
和list.get(i)
为array[i]
阅读List
的{{3}}。