在Java的公共类中生成私有类的实例
我想在一个名为VehiclePanel的构造函数中创建一个Car实例,但我似乎不知道怎么做,所以我来了。有人能帮忙吗在Java的公共类中生成私有类的实例,java,user-interface,Java,User Interface,我想在一个名为VehiclePanel的构造函数中创建一个Car实例,但我似乎不知道怎么做,所以我来了。有人能帮忙吗 public class VehiclePanel extends JPanel { //variables here public VehiclePanel() { // somehow need to create a new instance of Car class and add it to the VehiclePanel } private class C
public class VehiclePanel extends JPanel {
//variables here
public VehiclePanel() {
// somehow need to create a new instance of Car class and add it to the VehiclePanel
}
private class Car extends JPanel {
// Car code here, not important
}
}
请注意,因为汽车是在VehiclePanel内声明的,所以它是私人的这一事实并不重要 为什么不干脆把车=新车;-然后用你的汽车变量?你的问题把我弄糊涂了,因为我不知道你遇到了什么问题;如果汽车不需要进入车辆面板的状态,就考虑把它做成一个。
public VehiclePanel() {
Car car = new Car();
add(car);
}