对不起标题!不知道怎么回答这个问题..
我使用命令revalidate()
,窗口变得清晰。
如果我想在窗口添加KeyListener
我应该放什么?
如果窗口获得windowDeiconified
该事件,则KeyListener可以正常工作。
所以我的主要问题是其他代码需要(重新处理)keyListener而不重新打开它?
我应该使用revalidate()
而不是重新打开窗口以使我的keyListener工作?
我试图谷歌但我真的不知道如何搜索问题..也没有见过有人在谈论那个......我不知道我在这里缺少什么。
下面是我正在使用的代码和类。
开始上课:
package MainPackage;
import javax.swing.JFrame;
public class Starting_Window {
public static JFrame frame = new JFrame("The Simple Game By Erick");
public static GameFrameMenu menuG = new GameFrameMenu();
public static PlayingClass game;
public static void main(String[] args) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 800/4*3);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.add(menuG);
}
}
菜单类:
package MainPackage;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;
public class GameFrameMenu extends JPanel implements ActionListener {
public static Timer menuTimer;
public GameFrameMenu() {
setFocusable(true);
setLayout(null);
color = new Color(rand.nextInt(255),rand.nextInt(255),rand.nextInt(255));
menuTimer = new Timer(10,this);
menuTimer.start();
JButton btnStart = new JButton("Start Game!");
btnStart.setBounds(285, 180, 200, 40);
btnStart.setBackground(Color.BLACK);
btnStart.setForeground(Color.WHITE);
add(btnStart);
JButton btnOptions = new JButton("Options!");
btnOptions.setBounds(285, 240, 200, 40);
btnOptions.setBackground(Color.BLACK);
btnOptions.setForeground(Color.WHITE);
add(btnOptions);
JButton btnAboutUs = new JButton("About Us!");
btnAboutUs.setBounds(285, 300, 200, 40);
btnAboutUs.setBackground(Color.BLACK);
btnAboutUs.setForeground(Color.WHITE);
add(btnAboutUs);
JButton btnExit = new JButton("Exit!");
btnExit.setBounds(285, 360, 200, 40);
btnExit.setBackground(Color.BLACK);
btnExit.setForeground(Color.WHITE);
add(btnExit);
/////////////////////////////////////////////////
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Starting_Window.game = new PlayingClass();
menuTimer.stop();
Starting_Window.menuG.removeAll();
Starting_Window.menuG.repaint();
Starting_Window.frame.repaint();
Starting_Window.frame.add(Starting_Window.game);
Starting_Window.game.setFocusable(true);
Starting_Window.game.repaint();
Starting_Window.frame.remove(Starting_Window.menuG);
Starting_Window.frame.revalidate();
}
});
btnOptions.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnAboutUs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
Font HEADER = new Font("Verdana", Font.BOLD, 35);
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setFont(HEADER);
g2d.drawString("The Simple Game Here By Erick ", 90, 145);
setBackground(color);
pushTimer();
drawEffects(g2d);
}
public int limiterEffect = 0;
public void drawEffects(Graphics2D g2d) {
limiterEffect++;
if(limiterEffect<15) {
g2d.setColor(getRandomColor(color));
g2d.fillOval(rand.nextInt(1000), rand.nextInt(1000), 25, 25);
g2d.setColor(getRandomColor(color));
g2d.fillOval(rand.nextInt(1000), rand.nextInt(1000), 25, 25);
g2d.setColor(getRandomColor(color));
g2d.fillOval(rand.nextInt(1000), rand.nextInt(1000), 25, 25);
g2d.setColor(getRandomColor(color));
g2d.fillOval(rand.nextInt(1000), rand.nextInt(1000), 25, 25);
g2d.setColor(getRandomColor(color));
g2d.fillOval(rand.nextInt(1000), rand.nextInt(1000), 25, 25);
g2d.setColor(getRandomColor(color));
g2d.fillOval(rand.nextInt(1000), rand.nextInt(1000), 25, 25);
g2d.setColor(getRandomColor(color));
g2d.fillOval(rand.nextInt(1000), rand.nextInt(1000), 25, 25);
g2d.setColor(getRandomColor(color));
g2d.fillOval(rand.nextInt(1000), rand.nextInt(1000), 25, 25);
}
if(limiterEffect==30) {
limiterEffect = 0;
}
}
static int counterTimer = 0, counterTimerDelay = 50;
static int canDisplay = 0;
public void pushTimer() {
counterTimer++;
if(counterTimer==counterTimerDelay) {
color = getRandomColor(color);
counterTimer = 0;
counterTimerDelay = rand.nextInt(200)+50;
}
}
public Color getRandomColor(Color c) {
int R,G,B;
R = rand.nextInt(255);
G = rand.nextInt(255);
B = rand.nextInt(255);
c = new Color(R,G,B);
return c;
}
static Random rand = new Random();
static Color color;
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}
播放已从MenuClass调用的类:
package MainPackage;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class PlayingClass extends JPanel implements ActionListener {
Timer gameTimer;
Player player;
public PlayingClass() {
setFocusable(true);
setBackground(new Color(GameFrameMenu.rand.nextInt(255),GameFrameMenu.rand.nextInt(255),GameFrameMenu.rand.nextInt(255)));
gameTimer = new Timer(10,this);
gameTimer.start();
player = new Player(200,200);
addKeyListener(new KeyAdapt(player));
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
player.draw(g2d);
}
@Override
public void actionPerformed(ActionEvent e) {
player.update();
repaint();
}
}
实体类:
package MainPackage;
import java.awt.Graphics2D;
public class Entity {
int x, y;
public Entity(int x, int y) {
this.x = x;
this.y = y;
}
public void update() {
}
public void draw(Graphics2D g2d) {
}
}
扩展实体类的播放器类:
package MainPackage;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
public class Player extends Entity {
int velY = 0, velX = 0;
public Player(int x, int y) {
super(x, y);
}
public void update() {
x += velX;
y += velY;
}
public void draw(Graphics2D g2d) {
g2d.drawImage(getPlayerImg(), x, y, null);
}
public Image getPlayerImg() {
ImageIcon i = new ImageIcon("C:\\Java Pictures\\baseball pictures\\mini_player.png");
return i.getImage();
}
public void keyPressed(KeyEvent e) {
System.out.println("Something happened");
}
public void keyReleased(KeyEvent e) {
}
}
最后是KeyAdapt类与Player类的同步(同步):
package MainPackage;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class KeyAdapt extends KeyAdapter {
Player p;
public KeyAdapt(Player player) {
p = player;
}
public void keyPressed(KeyEvent e) {
p.keyPressed(e);
}
public void keyReleased(KeyEvent e) {
p.keyReleased(e);
}
}
所以这里的问题是,当我按下开始按钮时,屏幕变得清晰,我可以看到在PlayingClass中发生的任何事情,但KeyListener不起作用。为了完成这项工作,我必须最小化和去除窗口,以便启用KeyListener。所以我希望我能更好地解释它,你可以帮助我。