gpt4 book ai didi

java - 使用 keylistener 在 jpanel 周围移动圆圈

转载 作者:行者123 更新时间:2023-12-01 11:34:52 25 4
gpt4 key购买 nike

好吧,我正在尝试创建一个 Jpanel,它创建一个圆圈,然后可以在屏幕上移动该圆圈。我来来回回有一段时间了,似乎不知道如何让实际的东西为我移动。

package astroidlab;

import java.awt.BorderLayout;
import javax.swing.JFrame;

public class MainFrame {
public static void main(String[] args) {
//components
JFrame jf = new JFrame();
PaintObjects po = new PaintObjects();


jf.setLayout(new BorderLayout());
jf.add(po, BorderLayout.CENTER);

jf.setVisible(true);
jf.setSize(300, 300);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}

这是框架类。 ^^

package astroidlab;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.Timer;

public class PaintObjects extends JPanel implements ActionListener{
//global variable
Ship s = new Ship();

//constructor
public PaintObjects() {
super();

Timer t = new Timer(50, this);
t.start();
}

@Override
public void paintComponent(Graphics g) {
super.paintComponents(g);
g.setColor(Color.BLACK);

g.fillOval(s.getXpos(), s.getYpos(), s.getHeight(), s.getWidth());

}

@Override
public void actionPerformed(ActionEvent ae) {
int xpos = s.getXpos();
int ypos = s.getYpos();


repaint();
}
}

上面的类应该绘制对象并更新它们。它确实画出了椭圆形,但似乎我在融入下一个类时遇到了麻烦。

package astroidlab;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class MoveShip extends JFrame{
Ship s = new Ship();

public MoveShip() {

ballMover bm = new ballMover();
this.addKeyListener(bm);

}

private class ballMover implements KeyListener {

@Override
public void keyPressed(KeyEvent ke) {
if(ke.getKeyCode() == 37) {
s.goLeft();
}
if(ke.getKeyCode() == 39) {
s.goRight();
}
if(ke.getKeyCode() == 38) {
s.goUp();
}
if(ke.getKeyCode() == 40) {
s.goDown();
}
}

@Override
public void keyReleased(KeyEvent ke) {
}

@Override
public void keyTyped(KeyEvent ke) {
}

}

}

上面的类(class)似乎是我出错的地方。我对图形没有经验,所以我感觉我只是错过了一些简单的东西。

package astroidlab;

public class Ship {
//fields
int xpos = 0;
int ypos = 0;
int height = 20;
int width = 20;

public Ship() {
super();
}

//move methods
public void goLeft() {
xpos -= 2;
System.out.println("xpos: " + xpos + " ypos: " + ypos);
}
public void goRight() {
xpos += 2;
System.out.println("xpos: " + xpos + " ypos: " + ypos);
}

public void goUp() {
ypos -= 2;
System.out.println("xpos: " + xpos + " ypos: " + ypos);
}
public void goDown() {
ypos += 2;
System.out.println("xpos: " + xpos + " ypos: " + ypos);
}

//get methods
public int getXpos() {
return xpos;
}
public int getYpos() {
return ypos;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}

//set methods
public void setXpos() {
this.xpos = xpos;
}
public void setYpos() {
this.ypos = ypos;
}
public void setHeight() {
this.height = height;
}
public void setWidth() {
this.width = width;
}

}

然后这个类会跟踪椭圆的位置,并在按下按键时更新它。 (或者应该)

无论如何,如有任何反馈,我们将不胜感激。我确信我只是犯了一些菜鸟错误。提前致谢。

最佳答案

  1. 上面的代码从未使用 MoveShip 类。也许您打算在 main 方法中创建此类的实例,而不仅仅是 JFrame
  2. 如果您更改某物的坐标(在本例中为 KeyListener 方法中的 Ship),并且想要重新绘制以使坐标更改生效,则应显式调用 repaint 您想要绘制的对象(在本例中为 PaintObjects)。
  3. MoveShipPaintObjects 都包含它们自己的 Ship 实例 - 如果一个实例移动,则不会影响另一个实例。相反,创建一个可以在两个类之间共享的单个实例。
  4. 要触发 KeyListener,组件必须具有焦点。您可以考虑使用 KeyBindings .

关于java - 使用 keylistener 在 jpanel 周围移动圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30107018/

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