我在 JFrame 中的重绘不起作用
My repaint in JFrame is not working
我在 JFrame 中重绘有问题我之前在制作动画时使用了 repaint() 并且一切正常
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Draw extends JComponent implements KeyListener {
int x = 0;
int y = 0;
public void paint(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(x, y, 50, 50);
}
public void keyPressed(KeyEvent k) {
if(k.getKeyCode() == KeyEvent.VK_UP){
y -= 2;
} else if(k.getKeyCode() == KeyEvent.VK_DOWN){
y += 2;
} else if(k.getKeyCode() == KeyEvent.VK_LEFT){
x -= 2;
} else if(k.getKeyCode() == KeyEvent.VK_RIGHT){
x += 2;
}
repaint();
}
public void keyReleased(KeyEvent k) {}
public void keyTyped(KeyEvent k) {}
}
这是我的画 class 如果我 运行 作为小程序一切正常但我不想要小程序
我的相框class
import javax.swing.*;
public class Frame {
Draw d = new Draw();
public Frame(){
JFrame f = new JFrame("Game");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(800, 600);
f.setVisible(true);
f.add(d);
f.addKeyListener(new Draw());
}
}
和我的主要 class
public class Main {
public static void main(String[] args) {
Frame f = new Frame();
}
}
repaint() 不工作我测试了它工作的关键侦听器那么为什么 repaint() 不工作?
KeyListener 不应该 工作,因为默认情况下 JComponent 无法获得程序焦点,这是 KeyListener 工作的必要条件。一种解决方案是通过 setFocusable(true)
使其可聚焦,然后对其调用 requestFocusInWindow()
。最好使用键绑定 (tutorial link)。请注意,您应该重写 paintComponent,而不是绘画,并且您不应该忘记在您的重写中调用 super 的方法。
For example
编辑:我错了,因为 JFrame 可以 获得焦点并且您正在将 KeyListener 添加到 JFrame。但是你的问题是你正在创建一个 new Draw object 来执行此操作,而不是原始显示的 Draw 对象。如果您使用相同的 Draw 对象来显示图像和 KeyListener,您的代码实际上可以工作:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Draw extends JComponent implements KeyListener {
int x = 0;
int y = 0;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(x, y, 50, 50);
}
public void keyPressed(KeyEvent k) {
if (k.getKeyCode() == KeyEvent.VK_UP) {
y -= 2;
} else if (k.getKeyCode() == KeyEvent.VK_DOWN) {
y += 2;
} else if (k.getKeyCode() == KeyEvent.VK_LEFT) {
x -= 2;
} else if (k.getKeyCode() == KeyEvent.VK_RIGHT) {
x += 2;
}
repaint();
}
public void keyReleased(KeyEvent k) {
}
public void keyTyped(KeyEvent k) {
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Draw d = new Draw();
JFrame f = new JFrame("Game");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(800, 600);
f.setVisible(true);
f.add(d);
f.addKeyListener(d);
}
});
}
}
不过使用键绑定更安全。
我在 JFrame 中重绘有问题我之前在制作动画时使用了 repaint() 并且一切正常
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Draw extends JComponent implements KeyListener {
int x = 0;
int y = 0;
public void paint(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(x, y, 50, 50);
}
public void keyPressed(KeyEvent k) {
if(k.getKeyCode() == KeyEvent.VK_UP){
y -= 2;
} else if(k.getKeyCode() == KeyEvent.VK_DOWN){
y += 2;
} else if(k.getKeyCode() == KeyEvent.VK_LEFT){
x -= 2;
} else if(k.getKeyCode() == KeyEvent.VK_RIGHT){
x += 2;
}
repaint();
}
public void keyReleased(KeyEvent k) {}
public void keyTyped(KeyEvent k) {}
}
这是我的画 class 如果我 运行 作为小程序一切正常但我不想要小程序
我的相框class
import javax.swing.*;
public class Frame {
Draw d = new Draw();
public Frame(){
JFrame f = new JFrame("Game");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(800, 600);
f.setVisible(true);
f.add(d);
f.addKeyListener(new Draw());
}
}
和我的主要 class
public class Main {
public static void main(String[] args) {
Frame f = new Frame();
}
}
repaint() 不工作我测试了它工作的关键侦听器那么为什么 repaint() 不工作?
KeyListener 不应该 工作,因为默认情况下 JComponent 无法获得程序焦点,这是 KeyListener 工作的必要条件。一种解决方案是通过 setFocusable(true)
使其可聚焦,然后对其调用 requestFocusInWindow()
。最好使用键绑定 (tutorial link)。请注意,您应该重写 paintComponent,而不是绘画,并且您不应该忘记在您的重写中调用 super 的方法。
For example
编辑:我错了,因为 JFrame 可以 获得焦点并且您正在将 KeyListener 添加到 JFrame。但是你的问题是你正在创建一个 new Draw object 来执行此操作,而不是原始显示的 Draw 对象。如果您使用相同的 Draw 对象来显示图像和 KeyListener,您的代码实际上可以工作:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Draw extends JComponent implements KeyListener {
int x = 0;
int y = 0;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(x, y, 50, 50);
}
public void keyPressed(KeyEvent k) {
if (k.getKeyCode() == KeyEvent.VK_UP) {
y -= 2;
} else if (k.getKeyCode() == KeyEvent.VK_DOWN) {
y += 2;
} else if (k.getKeyCode() == KeyEvent.VK_LEFT) {
x -= 2;
} else if (k.getKeyCode() == KeyEvent.VK_RIGHT) {
x += 2;
}
repaint();
}
public void keyReleased(KeyEvent k) {
}
public void keyTyped(KeyEvent k) {
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Draw d = new Draw();
JFrame f = new JFrame("Game");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(800, 600);
f.setVisible(true);
f.add(d);
f.addKeyListener(d);
}
});
}
}
不过使用键绑定更安全。