java显示arraylist到textfield

时间:2015-01-15 12:31:43

标签: java arraylist

在文本字段中显示arraylist值不起作用;此类包含savebutton,它将foodquan ++保存到列表中,

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JButton;    
/**
 *
 * @author sirak elec
 */
public class User extends javax.swing.JFrame {
 int Food_num;
 int Food_quan;
 int Drink_num;
 int Drink_quan;
 int Tab_num;
 ArrayList saveArray1=new ArrayList(); 


   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   Tab_num =Integer.parseInt(Text3.getText());
   Food_num =Integer.parseInt(Text6.getText());
   Food_quan =Integer.parseInt(Text7.getText());
   Drink_num =Integer.parseInt(Text10.getText());
   Drink_quan =Integer.parseInt(Text11.getText());
   if((Tab_num!=0)&&(Food_quan<=10)&&(Drink_num<=10)&&(Drink_quan<=10)&&(Food_num<=10)){
   String ord ="Order Succesfull";  
   Text14.setText(ord);
    }
  else{
    String tryagain ="Invalid Entry";  
   Text14.setText(tryagain);  
  }
    }                                       

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
 show();
    } 
public void show(){
     User cquantity=new User();
     int c=cquantity.getquancakesold();
     saveArray1.add(c);

    }                                       
public int getquancakesold(){
return Food_quan++;
}
}

在这个类中按下show按钮时它会将arraylist中的值显示到textfield,这就是目标

import java.awt.event.ActionEvent;
import java.util.List;
public class Staff extends javax.swing.JFrame{

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                             
     User val=new User();
     list t=val.saveArray1;
     if(val.Food_num==1){
         for (int j=0;j<t.size();j++) {
             jTextField5.setText((t.get(i));
         }
     }

}

1 个答案:

答案 0 :(得分:1)

每次拨打JTextField时,您都会覆盖setText()值。您需要在循环中构建String值,然后使用创建的setText()调用String