【问题标题】:Retrieve values stored in an array to display on a java swing jFrame检索存储在数组中的值以显示在 java swing jFrame 上
【发布时间】:2014-03-23 09:05:07
【问题描述】:

我无法在 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);
    }
}                             

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

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

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

【问题讨论】:

  • List eC 从何而来? Lists 对象是什么类型的?汽车?
  • 为了尽快获得更好的帮助,请发布MCVE(最小完整且可验证的示例)。注意:其中的第 30 个 LOC 是 this.dispose();,它不可能抛出 NPE。发布 MCVE。
  • 发布汽车类有帮助吗?列表对象是字符串
  • 你在问谁?添加@KlemensMorbe(或重要的@)以通知他们有新评论。如果你写那条评论问我,我已经建议你发什么了..
  • @AndrewThompson,抱歉,在看到您的回复之前,我正在写给 Klemens 的回复。我的编辑是否达到了您的要求?

标签: java arrays swing initialization jform


【解决方案1】:

就是这样,ArrayList&lt;Car&gt; carList 永远不会被初始化。

您的代码:

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

正确代码:

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

【讨论】:

  • 我已经编辑了我的代码以反映这些更改并添加了登录类。这现在显示了非常棒的 Edit 类,但最初或单击下一步时没有出现任何值。这些值存储在文件中,我在登录屏幕加载时导入。
  • 我不知道你真正想要什么。您的列表现在已初始化,但没有元素。您必须先添加一辆新车。 carList.add(new Car());carList.add(editCar));
  • 抱歉,好吧。非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 2012-11-21
  • 2016-09-19
  • 1970-01-01
  • 2021-05-20
相关资源
最近更新 更多