0

我无法在 Java swing jFrame 上显示存储在数组中的值,我试图将这些值显示为 jFrame 中的 jLabels。

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

汽车类:

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);      
    }
}
}

登录 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);
    }
}                             

主页 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();
}  

编辑汽车 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());
}    

我很确定我没有正确初始化数组,老实说很难掌握如何做到这一点。

4

1 回答 1

0

它在那里,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>();
于 2014-03-23T09:45:43.037 回答