-1

每次通过循环时,我将如何替换名为 hangmanImg 的图像?

public void postImg() {
  String imgName = Integer.toString(numError);
  String hangmanImg = imgName.concat(".jpg");
  try{
    BufferedImage myImg = ImageIO.read(new File(hangmanImg));
    JLabel hangman = new JLabel(new ImageIcon(myImg));
    hangman.setSize(600,600);
    hangman.setLocation(300, 50);
    add(hangman);
    repaint();
  }catch(IOException ex){
    System.out.println("BEGIN CATCH: " + ex.getMessage());
    JLabel error = new JLabel(hangmanImg);
    add(error);
    error.setSize(200,100);
    error.setLocation(300, 20);
  }
}
4

1 回答 1

1

使用hangman.setIcon(new ImageIcon...),请始终查看 javadoc JLabel.setIcon

于 2013-05-15T13:46:52.930 回答