gpt4 book ai didi

java - JPanel 在 For 循环完成之前不会更新或重新绘制

转载 作者:太空宇宙 更新时间:2023-11-04 12:38:50 24 4
gpt4 key购买 nike

我知道这可能是一个非常简单的问题,但由于我只使用了 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 的好方法

关于java - JPanel 在 For 循环完成之前不会更新或重新绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37038200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com