0

我正在为学校做一个期末项目,但我的代码正在做一些非常奇怪的事情。如果我在课堂上调用一个函数,则会显示 jpanel,但是当我从另一个课堂调用这个函数时,它不会。

我找到了解决方案..

调用方法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();}
     }

游戏面板类

    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);
    }
    }
4

1 回答 1

0

首先,如果容器已经附加到组件层次结构并且可见,则通常需要在将内容添加到容器后重新验证。

尝试在您的 createRow 方法中调用 this.add() 之后调用 this.revalidate()。

其次,您似乎正在使用 java.util.Timer。这不会在 EDT 上给您打电话。请改用 javax.swing.Timer 或确保 createRow 切换回 EDT。这可以通过 SwingUtilities.invokeLater(new Runnable() {...}) 来完成。无论我的第一个建议是否有效,您都应该解决这个问题!

如果这不起作用,请提供一个SSCCE,理想情况下我可以复制粘贴并在我的最后尝试。

于 2012-12-03T00:30:04.703 回答