标题中提到了这个问题。我寻找解决方案,但没有任何效果。
之前的repaint(),revalidate() - 只是没有效果。有谁知道这是什么问题?我不会覆盖paint()方法或类似的东西。
这是我的代码:
public class Ui extends JFrame{
public Ui() {
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().removeAll();
pHeader = new Header();
pHeader.setBackground(Color.DARK_GRAY);
pMap = new Map();
pMap.setBackground(Color.LIGHT_GRAY);
pMap.setLayout(new java.awt.GridLayout(20, 20));
pBase = new Base();
pBase.setBackground(Color.DARK_GRAY);
JButton button = new JButton();
button.setText("Hallo");
button.add(pHeader);
button.setVisible(true);
add(pHeader, BorderLayout.NORTH);
add(pBase, BorderLayout.SOUTH);
add(pMap, BorderLayout.CENTER);
pack();
setVisible(true);
}
}
地图级:
public class Map extends JPanel {
private Folk folk = new Folk();
private Field[][] field = new Field[20][20];
public static Random rnd = new Random();
public Map() {
for(int row = 0; row < field.length; row++) {
for(int column = 0; column < field[row].length; column++) {
Field f = new Field(row,column,setFieldType(row, column)) {
@Override
public Dimension getPreferredSize() {
return new Dimension(30, 30);
}
};
field[row][column]=f;
if(f.getType() == 0) { f.setBackground(new Color(153,204,255)); } //light blue
else if(f.getType() == 6) { f.setBackground(new Color(128,255,0)); } //light green
else if(f.getType() == 11) { f.setBackground(new Color(0,102,51)); } //dark green
else if(f.getType() == 12) { f.setBackground(new Color(0,102,51)); } //dark green
else if(f.getType() == 13) { f.setBackground(new Color(102,204,51)); } //green
else if(f.getType() == 20) { f.setBackground(Color.GRAY); } //grey
else { f.setBackground(new Color(128,255,0)); } //light green
add(f);
f.setVisible(true);
}
}
}
}