다른 Headfirst 연습을 기반으로 차량의 데이터로 GUI를 채우는 데 문제가 있습니다. Controller 클래스를 사용하여 Vehicle Object 클래스를 관리합니다. 어떤 이유로 나는 예외 범위를 벗어난 인덱스를 얻고있다.JTextField에 값/문자열 반환, IndexoutofboundsException
구이 클래스
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());
}
}
}
컨트롤러 클래스
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; }
}
죄송합니다. 더 나은 도움을 받으려면 곧 [SSCCE] (http://sscce.org/)에 게시하십시오. JTextField가 shortly, runnable, complilable 인 경우에만 시연합니다. – mKorbel
오류는 IndexOutOfBoundsException : Index : 0, Size입니다. : 0. 비록 배열 내에 4가 있다는 것을 알고 있습니다. 위와 같이 getVehicles 메서드를 사용하여이를 증명했습니다. – Melky
이 예외는 필자가 작성한 코드에 대해 흥미로울 수 있으며 내 화면에서보고 디버그합니다. – mKorbel