我找到了解决方案..
调用方法repaint();在创建2jpanels之后。 解决它。
现在......我怎么能关闭这个问题,或者将其标记为已解决? 很多人都读过这个。
这是班级。
主要班级
public class Main {
public static void main(String[] args) {
new GameFrame();}
}
GameFrame类
public class GameFrame extends JFrame {
private static final long serialVersionUID = 1L;
private static final int baseWidth=192,baseHeight=352;
private JPanel panel;
private GamePanel gamePanel;
public GameFrame() {
panel = (JPanel) this.getContentPane();
Dimension d = new Dimension(baseWidth, baseHeight);
panel.setPreferredSize(new Dimension(d));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setTitle("test game..");
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
init_panelGame();//this will content the real game panel.
}
private void init_panelGame() {
gamePanel = new GamePanel(this,scale);
gamePanel.setVisible(true);
panel.add(gamePanel);
this.revalidate();}
}
GamePanel类
public class GamePanel extends JPanel {
private static final long serialVersionUID = 1L;
private int imgSize = 32;
public String gameState ="create_row";
private ImagePanel[][] mat_pos = new ImagePanel[6][11];
private Timer tiempo;//changed to a swing timer..
public GamePanel(GameFrame gp,int scale) {
this.setLayout(null);
createRow();//works fine if i call this method from here.
tiempo=new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
gameFlow();
}});
timerStart();
}
public void gameFlow() {//this is the method call by my timer
if (gameState.equals("create_row"))
{createRow(); //it doesnt render the created row!! this is my problem!!
gameState = "move_squares";
return;}
if (gameState.equals("move_squares"))
{moveSquares();return;}
}
private void moveSquares() {
int runningSquares = 0;
for (int col=0;col<6;col++)
for(int ren=10;ren>=0;ren--)
{if (mat_pos[col][ren]!=null)
if (mat_pos[col][ren].state.equals("running"))
{runningSquares++;
mat_pos[col][ren+1]=mat_pos[col][ren];
mat_pos[col][ren]=null;
if (ren+1==10 || mat_pos[col][ren+2]!=null)
mat_pos[col][ren+1].state = "stopped";
int newX,newY;
newX = imgSize * col;
newY = imgSize * (ren+1);
mat_pos[col][ren+1].setBounds(newX,newY, imgSize,imgSize);
}
}
if (runningSquares==0)
{gameState="create_row";
createRow();}
}
public void createRow() {//create 2 panels and add them to this jpanel.
gameState = "move_squares";
int colorRandom1 = (int) (Math.random()*5);
int colorRandom2 = (int) (Math.random()*5);
ImagePanel t1 = new ImagePanel(colorRandom1);
ImagePanel t2 = new ImagePanel(colorRandom2);
int columna = (int) (Math.random()*5);
mat_pos[columna][0] = t1;
mat_pos[columna+1][0] = t2;
t1.setBounds(columna*imgSize,0,32, 32);
t2.setBounds((columna+1)*imgSize,0,32, 32);
this.add(t1);
this.add(t2);
this.revalidate();
}
public void timerStart(){
tiempo.start();
}
public void timerStop(){
tiempo.stop();
}
}
//完全是图像面板类,它只创建一个简单的面板,其中img为背景
public class ImagePanel extends JPanel {
private static final long serialVersionUID = 1L;
public String state = "running";
private Image img;
private size = 1;
public ImagePanel(int color) {
String cad=null;
this.setVisible(true);
cad = "assets/blue.png";
this.img = new ImageIcon(cad).getImage();
crearFondo();
}
public void crearFondo() {
Dimension size = new Dimension(32,32);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0,0, null);
}
}
答案 0 :(得分:0)
首先,如果容器已添加到组件层次结构且可见,则通常需要在向容器添加内容后重新验证。
尝试在createRow方法中调用this.add()之后调用this.revalidate()。
其次,您似乎正在使用java.util.Timer。这不会在EDT上给你打电话。请改用javax.swing.Timer,或确保createRow切换回EDT。这可以使用SwingUtilities.invokeLater(new Runnable(){...})完成。如果我的第一个建议有效,你应该解决这个问题!
如果这不起作用,请提供一个SSCCE,理想情况下我可以复制粘贴并尝试使用。{/ p>