我正在尝试在 JFrame 内的 MainPanel 内的 JPanel 内绘制一个矩形。在主类中,我初始化了一个 RoomView:
roomView = new RoomView(newropc,"Default");
然后我执行:
private void init_panels() {
mainPanel.setLayout(new BorderLayout());
mainPanel.add(toolsPanel,BorderLayout.SOUTH);
mainPanel.add(roomView,BorderLayout.EAST);
mainPanel.add(libraryPanel,BorderLayout.WEST);
}
然后课堂 roomview 执行:
public class RoomView extends JPanel{
private RoomPController rpc;
private double w, h;
int qFactor;
Vector v;
String roomName;
DrawingPanelVector dpv;
public RoomView(RoomPController newRpc, String newRoomName) {
setPreferredSize(new Dimension(600,350));
rpc = newRpc;
w = 0;
h = 0;
qFactor = 1;
v = new Vector();
roomName = newRoomName;
v.add(new GraphicRectangle(0, 0, (int)(w*qFactor), (int)(h*qFactor), Color.black));
dpv = new DrawingPanelVector(v);
add(dpv);
setVisible(true);
getPreferredSize();
}
public void RepaintRoom() {
Scanner c = new Scanner(System.in);
v = new Vector();
w = rpc.getRoomW(roomName);
h = rpc.getRoomL(roomName);
System.out.println("Room w = " + w + " Room h = " + h);
System.out.println("Height = " + getHeight() + " Width = " + getWidth());
if ((double)getHeight()/h < (double)getWidth()/w) qFactor = (int)((double)getHeight()/h);
else qFactor = (int)((double)getWidth()/w);
System.out.println("qFactor = " + qFactor);
v.add(new GraphicRectangle(0, 0, (int)(w*qFactor), (int)(h*qFactor), Color.black));
dpv = new DrawingPanelVector(v);
add(dpv);
setVisible(true);
}
}
最后,DrawingPanelVector 是:
public class DrawingPanelVector extends JPanel {
// variable miembro
private Vector v;
// constructor
public DrawingPanelVector(Vector va) {
super(new FlowLayout());
setPreferredSize(new Dimension(600,350));
v = va;
getPreferredSize();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Drawable dib;
Iterator it;
it = v.iterator();
while (it.hasNext()) {
dib = (Drawable) it.next();
dib.draw(g);
}
}
}
它什么也不做,MainPanel 上也没有出现任何内容。
这让我发疯,我必须在 7 小时内将此代码提供给我的老师。