我试图将RedSquare类的对象添加到CatchMeV2类的JFrame中。有什么问题?
public class CatchMeV2 implements ActionListener{
int width = 400;
int height = 450;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(400, 400);
frame.setTitle("CatchMe.V2");
RedSquare r = new RedSquare();
frame.add(r);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
public class RedSquare extends JPanel implements ActionListener {
int x = 20; int y = 20;
int velX = 4; int velY = 4;
public RedSquare(){
addActionListener(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.green);
g.fillRect(x, y, 50, 50);
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
x += velX;
y += velY;
if (x < 0) {
velX = 0;
x = 0;
}
if (x > 400 - 50) {
velX = 0;
x = 400 - 50;
}
if (y < 0) {
velY = 0;
y = 0;
}
if (y > 400 - 40) {
velY = 0;
y = 400 - 40;
}
repaint();
}
}
actionPerformed方法没有做任何事情。有人可以帮忙吗?或者有一个简单的方法来做到这一点? 背景:我试图通过使用一个类来制作游戏。我做到了,但问题是我一次只能输入一个关键输入而且它是滞后的。而我的老师说,如果我将它分成不同的类别,它就不会滞后。这是真的吗?
答案 0 :(得分:3)
您无法将非可视组件添加到JPanel
,因此您需要从组件中扩展RedSquare
类,例如JPanel
并覆盖paintComponent()
方法。
public class CatchMeV2 {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setTitle("CatchMe.V2");
RedSquare r = new RedSquare();
frame.setContentPane(r);
frame.setVisible(true);
}
}
class RedSquare extends JPanel implements ActionListener {
public RedSquare() {
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); // don't forget to call super method
g.setColor(Color.green);
g.fillRect(20, 20, 50, 50);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
更新&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT ;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;
你不能使用面板执行的操作,只能用于按钮等。如果你想点击面板做某事,那么你需要使用工具鼠标列表。并将动作代码放入鼠标点击方法。运行此示例
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
public class CatchMeV2 {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(400, 400);
frame.setTitle("CatchMe.V2");
RedSquare r = new RedSquare();
frame.add(r);
}
}
class RedSquare extends JPanel implements MouseListener {
int x = 20;
int y = 20;
int velX = 4;
int velY = 4;
public RedSquare() {
addMouseListener(this);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(x, y, 50, 50);
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("hi");
x += velX;
y += velY;
if (x < 0) {
velX = 0;
x = 0;
}
if (x > 400 - 50) {
velX = 0;
x = 400 - 50;
}
if (y < 0) {
velY = 0;
y = 0;
}
if (y > 400 - 40) {
velY = 0;
y = 400 - 40;
}
this.repaint();
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
}
那时你只想用鼠标点击移动方块所以你仍然可以使用awt鼠标事件但是当你使用键时你必须使用key binding