Java JPanel未更新
我有一个非常奇怪的问题,我正在制作的JPanel并不是每次都更新。这是我用来更新包含游戏地图的JPanel的方法。调用MapView会创建一个JPanel,其中包含地图。但是,有时地图会刷新,其中的所有分幅都会正确显示,有时它只在右上角显示分幅Java JPanel未更新,java,arrays,swing,graphics,jpanel,Java,Arrays,Swing,Graphics,Jpanel,我有一个非常奇怪的问题,我正在制作的JPanel并不是每次都更新。这是我用来更新包含游戏地图的JPanel的方法。调用MapView会创建一个JPanel,其中包含地图。但是,有时地图会刷新,其中的所有分幅都会正确显示,有时它只在右上角显示分幅 public void updateMap(char[][] map){ remove(mapView); revalidate(); repaint(); mapView = new MapView(lo
public void updateMap(char[][] map){
remove(mapView);
revalidate();
repaint();
mapView = new MapView(logic, oCreator, 240, 240, map);
mapView.setBounds(40, 80, 240, 240);
add(mapView);
}
以及MapPanel类中绘制地图的方法:
public JPanel addMapPanel(char[][] map){
int mapLength = map.length;
int mapHeight = map[0].length;
int tileSize = Math.min((length / mapLength), (height / mapHeight));
absoluteMapHeight = tileSize * mapHeight;
absoluteMapLength = tileSize * mapLength;
JPanel mapFrame = new JPanel(new GridLayout(mapHeight, mapLength));
mapFrame.setBackground(oCreator.getColorMedium());
int counter = 0;
for(int yIndex = 0; yIndex < mapHeight; yIndex++){
for(int xIndex = 0; xIndex < mapLength; xIndex++){
char charAtPos = map[xIndex][yIndex];
JPanel tile;
//A tile is just a JPanel with the correct background.
switch(charAtPos){
//TODO Colours are placeholders.
case '.':
tile = createMapTile(Color.DARK_GRAY, tileSize);
break;
case '#':
tile = createMapTile(Color.black, tileSize);
break;
case 'G':
tile = createMapTile(Color.yellow, tileSize);
break;
case 'P':
tile = createMapTile(Color.MAGENTA, tileSize);
break;
case 'E':
tile = createMapTile(Color.RED, tileSize);
break;
default:
tile = createMapTile(Color.blue, tileSize);
}
mapFrame.add(tile);
counter++;
}
}
System.out.println(counter);
return mapFrame;
}
公共JPanel addMapPanel(char[]]map){
int maple长度=map.length;
int-mapHeight=map[0]。长度;
int tileSize=Math.min((长度/最大长度),(高度/贴图高度));
absoluteMapHeight=tileSize*mapHeight;
绝对最大长度=瓷砖尺寸*最大长度;
JPanel mapFrame=新JPanel(新网格布局(mapHeight,mapLength));
setBackground(oCreator.getColorMedium());
int计数器=0;
for(int-yIndex=0;yIndex我不知道为什么。尝试在JPanel甚至JFrame上使用
repaint()
或revalidate()
。使用setBounds表示您正在尝试使用布局管理器,这排除了诸如invalidate、validate和revalidate之类的方法,因为这些方法与更新布局管理器相关,试着使用一个合适的布局管理器,这与我每个窗格加载大约300个JFrames有关吗?