gpt4 book ai didi

java - 将值/字符串返回到 JTextField,IndexoutofboundsException

转载 作者:行者123 更新时间:2023-12-01 15:03:18 26 4
gpt4 key购买 nike

根据另一个 Headfirst 练习,我在用车辆数据填充 GUI 时遇到了麻烦。我使用 Controller 类来管理我的车辆对象类。由于某种原因,我遇到了索引超出范围异常。

GUI类

public class ShowroomDriver{
public static Showroom Cars = new Showroom("Cars");
public static void main(String[] args) {
Showroom Cars = new Showroom("Cars");
Vehicle vechicle1 = new Vehicle("Ford");

Cars.addVehicle(vechicle1);
GuiInterface gui = new GuiInterface("Car Showroom");
}

private static class GuiInterface extends JFrame {
private JButton saleButton, previousButton, nextButton;
private static JTextField textField1;
private JLabel label1;
private JPanel[] p = new JPanel[5];
public GuiInterface(String sTitle) {
super(sTitle);
setLayout(new FlowLayout());
previousButton = new JButton("Previous Car");
nextButton = new JButton("Next Car");
saleButton = new JButton("Sale");

for(int i = 0; i < 5; i++){
p[i] = new JPanel();
}

Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel formPanel = new JPanel(new GridLayout(1, 2));


textField1 = new JTextField(10);
label1 = new JLabel("Manufacture");

p[0].add(label1);
p[1].add(textField1);


for(int i = 0; i < 2; i++){
formPanel.add(p[i]);
}

contentPane.add(formPanel, BorderLayout.CENTER);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,300);
this.setResizable(false);
this.setLocationRelativeTo(null);
getField();

this.setVisible(true);
}

private void getField(){
textField1.setText(Cars.currentVehicle().getManufacutre());
}
}
}

Controller 类

public class Showroom{
private ArrayList<Vehicle> vehiclesSold = new ArrayList();
private ArrayList<Vehicle> theVehicles;
private String vechicleType;
private int arrayPosition = 0;

public Showroom(String type){
vechicleType = type;
theVehicles = new ArrayList<Vehicle>();
}

public boolean addVehicle(Vehicle newVehicle){
theVehicles.add(newVehicle);
return true;
}

public Vehicle currentVehicle(){
return theVehicles.get(arrayPosition);
}

public void getVehicles(){
System.out.println("---Vehicle Type: " + vechicleType +"---");
for(Vehicle nextVehicle : theVehicles){
System.out.println(nextVehicle.toString());
}
}
}

车辆类别

public class Vehicle{
private String Manufacture
Vehicle(String Manufacture){ //There are more
this.Manufacture = Manufacture;
}
}

@Override
public String toString(){
String s = "Maufacture: " + getManufacutre()
"\n";
return s;
}

public String getManufacutre() { return this.Manufacture; }
}

最佳答案

如果没有更多代码,就无法判断错误来自何处。但从这段代码来看,唯一可能出现 IndexOutOfBoundsException 的地方是

return theVehicles.get(arrayPosition);

你的问题是,arrayPosition是错误的。
尝试调试您的代码以找出到底出了什么问题,或发布更多代码

编辑:您似乎对 static 关键字的作用有误解。
静态对象或方法是只在运行时实例化一次的东西。
例如,您在 class ShowhroomDriver 中声明 Cars 属性意味着该类 ShowroomDriver 有一个single 类属性名为Cars(顺便说一句 - 不要让属性以大写字符开头。这非常令人困惑)。

您想要的是将 ShowRoom 实例(您的 Cars 属性)传递给您的类 GuiInterface (同时删除 >static 关键字)通过其构造函数,如下所示:

// ...
private Showroom cars;
public GuiInterface(String sTitle, Showroom cars) {
// ...
this.cars = cars;
// ...
}

然后,而不是

private void getField(){
textField1.setText(Cars.currentVehicle().getManufacutre());
}

你写

private void getField(){
textField1.setText(this.cars.currentVehicle().getManufacutre());
}

同时删除除 main 方法中的关键字之外的所有 static 关键字。

关于java - 将值/字符串返回到 JTextField,IndexoutofboundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13324866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com