简体   繁体   English

JPanel不会更新或重绘,直到For循环结束

[英]JPanel Doesn't Update or Repaint until For Loop Finishes

I understand that this is probably a very simple problem, but as I've only been working with JPanel and JFrame for one weekend please don't bag on me too much. 我知道这可能是一个非常简单的问题,但由于我只在JPanel和JFrame工作了一个周末,所以请不要太过于依赖我。 My issue is that I have this code: 我的问题是我有这个代码:

package Game_Elements;
import java.awt.Color;
import java.awt.Desktop.Action;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.RenderingHints.Key;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game implements KeyListener
{
private JFrame frame = null;
private JPanel panel = null;
int y = 0;
int x = 0;
public boolean W_TRUE;
public boolean S_TRUE;
public boolean A_TRUE;
public boolean D_TRUE;
public void keyPressed(KeyEvent e)
{

    if(e.getKeyCode()== KeyEvent.VK_W){
            y-= 15;
            panel.setLocation(x, y);
            W_TRUE = true;

    }
    if(e.getKeyCode()== KeyEvent.VK_S){
            y+= 15;
            panel.setLocation(x, y);
            S_TRUE = true;
    }
    if(e.getKeyCode()== KeyEvent.VK_A){
            x-= 15;
            panel.setLocation(x, y);
            A_TRUE = true;
    }
    if(e.getKeyCode()== KeyEvent.VK_D){
            x+= 15;
            panel.setLocation(x, y);
            D_TRUE = true;
    }
    if(D_TRUE == true  && S_TRUE == true ){
        y+= 15;
        x+= 15;
        panel.setLocation(x, y);
    }
    if(A_TRUE == true  && W_TRUE == true ){
        y-= 15;
        x-= 15;
        panel.setLocation(x, y);
    }
    if(A_TRUE == true  && S_TRUE == true ){
        y+= 15;
        x-= 15;
        panel.setLocation(x, y);
    }
    if(D_TRUE == true && W_TRUE == true ){
        y-= 15;
        x+= 15;
        panel.setLocation(x, y);
    }
}
public void keyReleased(KeyEvent e)
{
    if(e.getKeyCode()== KeyEvent.VK_W){
        W_TRUE = false;
}
if(e.getKeyCode()== KeyEvent.VK_S){
        S_TRUE = false;
}
if(e.getKeyCode()== KeyEvent.VK_A){
        A_TRUE = false;
}
if(e.getKeyCode()== KeyEvent.VK_D){
        D_TRUE = false;
}
if(e.getKeyCode()== KeyEvent.VK_SPACE){
    System.out.println("Running JUMP");
    for(int j = y - 200; j < y; y-= 15){
        try {
            Thread.sleep(10);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        panel.setLocation(x, y);
        panel.revalidate();
    }   
    System.out.println("Complete");

}
}

public void keyTyped(KeyEvent e) {

}

public void frameStart()
{

    frame = new JFrame();
    frame.setVisible(true);
    frame.setTitle("Desperation");
    frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)); // otherwise nice exceptions java.awt.AWTError:
    frame.setPreferredSize(new Dimension(1920, 1050));
    frame.setSize(new Dimension(1920, 1050));
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    System.out.println("Frame Created");

    panel = new JPanel();
    panel.setBackground(Color.BLUE);
    panel.setMaximumSize(new Dimension(200, 200));
    panel.setLocation(1080, 500);
    panel.revalidate();
    frame.add(panel);
    System.out.println("Panel Created");

    frame.addKeyListener(this);
    System.out.println("Listener Created");
}

public static void main(String[] args){

    Game obj = new Game();

    obj.frameStart();
}

} }

The panel I have, a basic 200x200 blue cube, does not seem to change location until the loop is done running, so it's more of a jump than anything. 我所拥有的面板,一个基本的200x200蓝色立方体,在循环完成运行之前似乎没有改变位置,所以它更像是一个跳跃而不是任何东西。 I understand that the repaint function only runs as soon as possible, and as this is likely the problem, I was wondering if any of you good folks have advice on an alternative method for refreshing it so it doesn't necessarily look smooth, but at least show some progression. 我知道重绘功能只能尽快运行,因为这可能是问题所在,我想知道你们中的任何一个人是否有一个关于刷新它的替代方法的建议所以它不一定看起来很顺利,但是至少表现出一些进展。 Any advice is appreciated as I don't know much at all about JPanel, JFrame, or really JAnything. 任何建议都表示赞赏,因为我对JPanel,JFrame或JAnything都不太了解。 Thank you! 谢谢!

For refreshing 令人耳目一新

this.revalidate(); 

is a good way to "refresh" your JComponent 是一个“刷新”JComponent的好方法

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

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