繁体   English   中英

如何在JPanel中更新JLabel?

[英]How do I update a JLabel in a JPanel?

我在获取JLabel上的图像以在JPanel中更新时遇到麻烦。 我看不出问题是什么。 我曾尝试删除现有的JLabel并将一个新的JLabel添加到JPanel,但这是行不通的。 这是我要让动画开始工作的代码。 我的关键侦听器工作正常,因此我可以确定此类中存在一些问题:

import javax.swing.*;
import java.awt.event.*;
public class Character extends JPanel{
  //Constructs array that holds running frames
  static ImageIcon running[] = new ImageIcon[19];
  static int i = 0;
  //Contstructs label that holds the current image frame
  static JLabel characterLabel = new JLabel();
  //Creates imageicon to be returned to the character and stored in characterLabel
  static ImageIcon character = new ImageIcon();

  Timer runningTimer = new Timer(300, new RunningListener());



  public Character() {

    running[0] = new ImageIcon("running 1.png");
    running[1] = new ImageIcon("running 2.png");
    running[2] = new ImageIcon("running 3.png");
    running[3] = new ImageIcon("running 4.png");
    running[4] = new ImageIcon("running 5.png");
    running[5] = new ImageIcon("running 6.png");
    running[6] = new ImageIcon("running 7.png");
    running[7] = new ImageIcon("running 8.png");
    running[8] = new ImageIcon("running 9.png");
    running[9] = new ImageIcon("running 10.png");
    running[10] = new ImageIcon("running 11.png");
    running[11] = new ImageIcon("running 12.png");
    running[12] = new ImageIcon("running 13.png");
    running[13] = new ImageIcon("running 14.png");
    running[14] = new ImageIcon("running 15.png");
    running[15] = new ImageIcon("running 16.png");
    running[16] = new ImageIcon("running 17.png");
    running[17] = new ImageIcon("running 18.png");
    running[18] = new ImageIcon("running 19.png");
    characterLabel.setIcon(running[0]);
    this.add(characterLabel);
  }

  private void refreshCharacter(){
     this.remove(characterLabel);
    characterLabel.setIcon(running[i]);
    this.add(characterLabel);
    i++;
    if (i > 18){
      i = 0;
    }
  }

  private class RunningListener implements ActionListener{
    public void actionPerformed(ActionEvent e) {
      refreshCharacter();
    }
  }
}

我想这个JPanel也可能是问题,但我对此表示怀疑:

import javax.swing.*;

//Panel that manages the game
public class GamePanel extends JPanel{


  //Adds stuff to GamePanel to send to Frame
  public GamePanel(){

    this.addKeyListener(new KeyInput());
    this.add(new Character()); 
  }
}

这是关键的侦听器:

import java.awt.event.*;

//Listens for key actions
public class KeyInput implements KeyListener{

  //Constructs character
  Character c = new Character();

  public void keyPressed(KeyEvent evt) {
    int key = evt.getKeyCode();
    //When the right key is pressed, start the timer that starts the running animation
    if(key == KeyEvent.VK_RIGHT){
      c.runningTimer.start();
    }
  }

  public void keyReleased(KeyEvent evt){
    int key = evt.getKeyCode();
    //When the right key is released, the timer that starts the running animation is stopped
    if(key == KeyEvent.VK_RIGHT){
   c.runningTimer.stop();
    }
  }

  //There is no use for this... it just has to be there
  public void keyTyped(KeyEvent evt){
  }
}

这是框架:

import javax.swing.*;
public class Frame{

  //Creates window and calls to GamePanel
  public static void main (String[]args){
    GamePanel gamePanel = new GamePanel();
    JFrame window = new JFrame("Game");

    window.add(gamePanel);
    window.setLocation(50,50);
    window.setSize(1000,650);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    window.setVisible(true);
    gamePanel.requestFocus();
  }
}

如果要更改显示的图像,请避免删除JLabel或组件,而要交换JLabel的图标,如果可以避免的话。 如果您不是要更改图像,而是要做一些不同的事情,那么请给我们提供有关您想要的行为的更多详细信息,因为您告诉我们的只是您正在尝试“动画”,而且范围还很广。

例如,

private void refreshCharacter() {
   i++;
   i %= running.length;
   characterLabel.setIcon(running[i]);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM