簡體   English   中英

JPanel不會更新或重繪,直到For循環結束

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

我知道這可能是一個非常簡單的問題,但由於我只在JPanel和JFrame工作了一個周末,所以請不要太過於依賴我。 我的問題是我有這個代碼:

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

}

我所擁有的面板,一個基本的200x200藍色立方體,在循環完成運行之前似乎沒有改變位置,所以它更像是一個跳躍而不是任何東西。 我知道重繪功能只能盡快運行,因為這可能是問題所在,我想知道你們中的任何一個人是否有一個關於刷新它的替代方法的建議所以它不一定看起來很順利,但是至少表現出一些進展。 任何建議都表示贊賞,因為我對JPanel,JFrame或JAnything都不太了解。 謝謝!

令人耳目一新

this.revalidate(); 

是一個“刷新”JComponent的好方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM