我有8个背景颜色的jpanels,我试图在它上面绘制一个矩形,但它显示在它下面,我不能把它放在顶部
以下是初始化JPanel
的代码tempPanel = new JPanel();
tempPanel.setBackground(Color.black);
tempPanel.setOpaque(true);
tempPanel.setLayout(null);
panel.add(tempPanel);
tempPanel.setBounds(195 + insets.left, 155 + insets.top,
main.getLanes().get(5).getDistance()*5, 20);
if (!added)
lanes.add(tempPanel);
和绘制矩形的代码;它位于一个扩展Jpanel和inisde paintComponents的类中:
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawLanes();
int i, j;
ArrayList<Lane> lanes2= new ArrayList<Lane>();
ArrayList<Car> cars= new ArrayList<Car>();
for(int counter = 0; counter < 10; counter ++) {
lanes2 = main.getLanes();
for( i = 0; i< lanes.size(); i++) {
lanes.get(i).removeAll();
cars = lanes2.get(i).getCars();
for ( j = 0; j < cars.size(); j++) {
Insets insets = lanes.get(i).getInsets();
g2d.setColor(Color.red);
g2d.draw(new Rectangle(lanes.get(i).getLocation().x + 5*(lanes2.get(i).getDistance() - cars.get(j).getCurrent_pos()), lanes.get(i).getLocation().y,
cars.get(j).getCar_size()*5, lanes.get(i).getHeight()));
JLabel temp = new JLabel("Car");
temp.setForeground(Color.red);
temp.setBounds(insets.left + 5*(lanes2.get(i).getDistance() - cars.get(j).getCurrent_pos()) , insets.top,
cars.get(j).getCar_size()*5, lanes.get(i).getHeight());
lanes.get(i).add(temp);
}
}
}
感谢adavnce