paintComponent中的JAVA setColor保持为空
一旦有人点击空格(32),我就试图改变我的小程序的背景。它就是不起作用,我一直在尝试不同的东西,我在互联网上能找到的一切,比如把paintComponent中的JAVA setColor保持为空,java,Java,一旦有人点击空格(32),我就试图改变我的小程序的背景。它就是不起作用,我一直在尝试不同的东西,我在互联网上能找到的一切,比如把g.setColor(Color.BLUE) 在公共组件(图形g)块的开头 这里我得到了下面的代码。 My Screen.java import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; public class Screen extends JPanel impl
g.setColor(Color.BLUE)代码>
在公共组件(图形g)
块的开头
这里我得到了下面的代码。
My Screen.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Screen extends JPanel implements Runnable{
Thread thread = new Thread(this);
Frame frame;
private int fps = 0;
public int scene = 0;
public boolean running = false;
public Screen(Frame frame){
this.frame = frame;
this.frame.addKeyListener(new KeyHandler(this));
thread.start();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
super.paint(g);
g.clearRect(0, 0, this.frame.getWidth(),this.frame.getHeight());
g.drawString(fps + "", 10, 10);
if (scene == 0) {
g.setColor(Color.BLUE);
} else if (scene == 1) {
g.setColor(Color.GREEN);
} else {
g.setColor(Color.white);
}
g.fillRect(0, 0, getWidth(), getHeight());
}
public void run() {
System.out.println("[Success] Frame Created!");
long lastFrame = System.currentTimeMillis();
int frames = 0;
running = true;
scene = 0;
while(running){
repaint();
frames++;
if(System.currentTimeMillis() - 1000 >= lastFrame){
fps = frames;
frames = 0;
lastFrame = System.currentTimeMillis();
}
try {
Thread.sleep(2);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
System.exit(0);
}
public class KeyTyped{
public void keySPACE() {
scene = 1;
}
public void keyESC(){
running = false;
}
}
}
KeyHandler.java
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class KeyHandler implements KeyListener {
public Screen screen;
public Screen.KeyTyped keyTyped;
public KeyHandler(Screen screen){
this.screen = screen;
this.keyTyped = this.screen.new KeyTyped();
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if(keyCode == 27){
this.keyTyped.keyESC();
}
if(keyCode == 32){
this.keyTyped.keySPACE();
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
我不知道为什么代码不在一个块中。好像我把它弄错了
您应该使用/ActionMap而不是KeyListener
您需要确保JComponent设置为isFocusable(true),并且在第一次显示时执行component.requestFocus(),以确保您的操作将触发
请更正:
1.使用JFrame
代替Frame
2.实现paintComponent
如下:
g.clearRect(0, 0, this.frame.getWidth(),this.frame.getHeight());
if (scene == 0) {
g.setColor(Color.BLUE);
} else if (scene == 1) {
g.setColor(Color.GREEN);
} else {
g.setColor(Color.white);
}
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.WHITE);
g.drawString(fps + "", 10, 10);
三,。确保添加了Screen
JFrame
4.确保在JFrame
上发出setVisible(true)
“它不工作”是什么意思?会发生什么?控制台上是否有任何消息或堆栈跟踪?你是如何尝试诊断这个问题的(从互联网向你的程序中添加随机代码通常不起作用)?哦,该死,在我发布这篇文章之前,我一直在阅读关于stackoverflow上的好文章的列表,现在我注意到我忘了描述问题的一半。背景就像正常的开始背景一样保持空白(我或多或少是色盲,只看到很少的变化,所以我不能告诉你背景到底是哪种颜色,但不是蓝色或绿色)我试图更改代码,把它放在其他地方,我过去在互联网上读过不同的方法。添加了super.paintComponent等。。遗憾的是,没有留言。不用担心,很高兴你抽出时间来阅读列表!你的开始比很多人都好。我会特别注意提到的部分-通常情况下,在创建一个包含在问题中的部分的过程中,您会发现自己意识到问题完全是自己造成的。如果使用步骤1,则不需要步骤2,这是使用键绑定的好处。组件不必有焦点。我将从阅读上的Swing教程开始。