检索存储在数组中的值,以便在java swing jFrame上显示



我在Java swing jFrame上显示存储在数组中的值时遇到麻烦,我试图在jFrame内将值显示为jLabels。

我希望在表单加载时显示第一辆车的详细信息,当单击"下一步"(记录)按钮时显示数组中的其他汽车。

Car类:

package abc;
import java.io.*;
import java.util.*;
public class Car {
private String carReg, carModel, carMake;
private double carValue;
public void setCarReg(String carReg){
    this.carReg = carReg;
}
public String getCarReg(){
    return this.carReg;
}
public void setCarModel(String carModel){
    this.carModel = carModel;
}
public String getCarModel(){
    return this.carModel;
}
public void setCarMake(String carMake){
    this.carMake = carMake;
}
public String getCarMake(){
    return this.carMake;
}
public void setCarValue(double carValue){
    this.carValue = carValue;
}
public double getCarValue(){
    return this.carValue;
}
public static ArrayList<Car> CarArray = new ArrayList<Car>();
Iterator I = CarArray.iterator();
public static void exportCar(){
    String fileLoc = "C:\abc\exportCar.txt";
    int i = 0;
    try
    {
        FileOutputStream uh = new FileOutputStream(fileLoc);
        ObjectOutputStream st = new ObjectOutputStream(uh);
        for (i=0; i<CarArray.size(); i++)
        {
            st.writeObject(CarArray.get(i));
        }
    }
    catch(IOException e)
    {
          System.out.println(e);      
    }
}
public static void importCar(){
    String fileLoc = "C:\abc\exportCar.txt";
    int i = 0;
    try
    {
        FileInputStream uh = new FileInputStream(fileLoc);
        ObjectInputStream st = new ObjectInputStream(uh);
        try
        {
            while(uh!=null)
            {
                CarArray.add((Car)st.readObject());
            }
        }
        catch(Exception e){}
    }
    catch(IOException e)
    {
          System.out.println(e);      
    }
}
}

Login jForm:

package abc;
import java.util.*;
public class Login extends javax.swing.JFrame {
// Bidder bidder1 = new Bidder("cm", "w", 3, "FN", "LN", 1, "Street", "City", "BA99AE", 
//"1234567890", "EMail", "Card Make", "123456", "1122", 123);
public static int userLevelFound = 0;
/** Creates new form Login */
public Login() {
initComponents();
    Judge.JudgeArray.add(judge1);
    Representative.RepArray.add(rep1);
    //Bidder.BidderArray.add(bidder1);
    Bidder.importBidder();
    Car.importCar();
    LoginError.setVisible(false);
}
 private void LoginActionPerformed(java.awt.event.ActionEvent evt) {                                      
// Get the username entered
    String inputUN = UserName.getText();
// Get the password entered (plain text)
    String inputPassword = Password.getText();
    boolean loginFound = false;

    int a = 0;
    int b = Judge.JudgeArray.size();
    do
    {
        Judge tempJudge = new Judge();
        tempJudge = Judge.JudgeArray.get(a);
        if (inputUN.equals(tempJudge.getUserName()) && 
                inputPassword.equals(tempJudge.getPassword())){
            loginFound = true;
            userLevelFound = 1;
        }
        a++;
    } while ((a < b) && (loginFound == false));
    a = 0;
    b = Representative.RepArray.size();
    do
    {
        Representative tempRep = new Representative();
        tempRep = Representative.RepArray.get(a);
        if (inputUN.equals(tempRep.getUserName()) && 
                inputPassword.equals(tempRep.getPassword())){
            loginFound = true;
            userLevelFound = 2;
        }
        a++;
    } while ((a < b) && (loginFound == false));
    a = 0;
    b = Bidder.BidderArray.size();
    do
    {
        Bidder tempBidder = new Bidder();
        tempBidder = Bidder.BidderArray.get(a);
        if (inputUN.equals(tempBidder.getUserName()) && 
                inputPassword.equals(tempBidder.getPassword())){
            loginFound = true;
            userLevelFound = 3;
        }
        a++;
    } while ((a < b) && (loginFound == false));
    if (loginFound == true){
        Home home = new Home();
        home.setVisible(true);
        this.setVisible(false);
    } else{
        LoginError.setVisible(true);
    }
}                             

Home jForm:

package abc;
import java.util.*;
public class Home extends javax.swing.JFrame {
ArrayList<Car> carList = new ArrayList<Car>();
Iterator I;
/** Creates new form Home */
public Home() {
    initComponents();
    if (Login.userLevelFound == 3)
    {
        addCar.setVisible(false);
        editCar.setVisible(false);
    }
}
private void editCarActionPerformed(java.awt.event.ActionEvent evt) 
{                                        
    // TODO add your handling code here:
    Edit updateCar = new Edit(carList);
    updateCar.setVisible(true);
    this.dispose();
}  

Edit Cars jForm:

package abc;
import java.util.*;
public class Edit extends javax.swing.JFrame {
ArrayList<Car> carList;
Iterator I;
Car editCar = new Car();

/** Creates new form Edit */
public Edit(ArrayList eC) {
    initComponents();
    carList = eC;
    I = carList.iterator();
    if (Login.userLevelFound == 2)
    {
        carValue.setVisible(false);
        jLabel4.setVisible(false);
    }
}                                     
private void backActionPerformed(java.awt.event.ActionEvent evt)  
{                                     
    // TODO add your handling code here:
    Home home = new Home();
    home.setVisible(true);
    this.dispose();
}                                    
private void SaveActionPerformed(java.awt.event.ActionEvent evt)
{                                     
    // TODO add your handling code here:
}                                    
private void nextActionPerformed(java.awt.event.ActionEvent evt)
{                                     
    // TODO add your handling code here:
    if(I.hasNext()){
    editCar = (Car)I.next();
    carMake.setText(editCar.getCarMake());
    carModel.setText(editCar.getCarModel());
    carReg.setText(editCar.getCarReg());
    carValue.setText("£" + editCar.getCarValue());
}    

我很确定我还没有正确初始化数组,努力掌握如何做到这一点,在所有的诚实。

就是这样,ArrayList<Car> carList从未初始化。

代码:

public class Home extends javax.swing.JFrame {
    ArrayList<Car> carList;

正确的代码:

public class Home extends javax.swing.JFrame {
    ArrayList<Car> carList = new ArrayList<Car>();

相关内容

  • 没有找到相关文章

最新更新