我的监听器被添加到JFrame / JPanel。问题是,当我按下按钮时,什么也没发生。这可能是什么问题?
以下是使用的代码:
public class GAMEGAMEHA extends JPanel{
public static JFrame PacmanMainFrame = new JFrame();
TheJGame newGame;
GAMEGAMEHA() {
setSize(new Dimension(1200, 700));
JLabel BG = new JLabel();
JButton start = new JButton();
JButton howTo = new JButton();
JButton credit = new JButton();
JButton exit = new JButton();
setLayout(null);
start.setIcon(new ImageIcon(getClass().getResource("/data/SB.png")));
start.setContentAreaFilled(false);
start.addActionListener((ActionEvent e) -> {
newGame = new TheJGame();
GAMEGAMEHA.PacmanMainFrame.getContentPane().removeAll();
System.out.println("Removed Components");
GAMEGAMEHA.PacmanMainFrame.add(newGame);
System.out.println("Added Game");
GAMEGAMEHA.PacmanMainFrame.addKeyListener(newGame);
System.out.println("Added KeyListeners");
GAMEGAMEHA.PacmanMainFrame.setVisible(true);
System.out.println("Set Visiblity to True");
});
start.setBorderPainted(false);
add(start);
start.setBounds(440, 140, 300, 70);
howTo.setIcon(new ImageIcon(getClass().getResource("/data/HowToButton.jpg")));
howTo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e
) {
HowTo HowToScreen = new HowTo();
GAMEGAMEHA.PacmanMainFrame.getContentPane().removeAll();
GAMEGAMEHA.PacmanMainFrame.add(HowToScreen);
GAMEGAMEHA.PacmanMainFrame.setVisible(true);
}
});
howTo.setBorderPainted(false);
add(howTo);
howTo.setBounds(440, 220, 300, 70);
credit.setIcon(new ImageIcon(getClass().getResource("/data/CreditsButton.jpg")));
credit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e
) {
Credit CreditScreen = new Credit();
GAMEGAMEHA.PacmanMainFrame.getContentPane().removeAll();
GAMEGAMEHA.PacmanMainFrame.add(CreditScreen);
GAMEGAMEHA.PacmanMainFrame.setVisible(true);
}
});
credit.setBorderPainted(false);
add(credit);
credit.setBounds(440, 300, 300, 70);
exit.setIcon(new ImageIcon(getClass().getResource("/data/ExitButton.jpg")));
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e
) {
System.exit(JFrame.EXIT_ON_CLOSE);
}
}
);
exit.setBorderPainted(
false);
add(exit);
exit.setBounds(
440, 380, 300, 70);
BG.setIcon(
new ImageIcon(getClass().getResource("/data/MainMenu.jpg")));
BG.setText(
"BackgroundImage");
add(BG);
BG.setBounds(
0, 0, 1200, 700);
}
public static void main(String[] args) {
GAMEGAMEHA GameMENU = new GAMEGAMEHA();
PacmanMainFrame.setSize(new Dimension(1200, 700));
PacmanMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PacmanMainFrame.setTitle("THE JUNGLE GAME: A Object-Oriented Programming Project");
PacmanMainFrame.setResizable(false);
PacmanMainFrame.setLocationRelativeTo(null);
PacmanMainFrame.add(GameMENU);
PacmanMainFrame.setVisible(true);
}
}
这是游戏开始:问题出在这里。主菜单很好但是当它到这里时,它不接受也不读任何键。
public class TheJGame extends JPanel implements KeyListener {
BackgroundGame BG = new BackgroundGame();
MainCharacter bens = new MainCharacter();
static int MainCx = 22;
static int MainCy = 20;
static int BGx = 0;
static int BGy = 545;
TheJGame() {
bens.initializeMChar();
BG.initializeGGround();
setBounds(0, 0, 1200, 700);
setFocusable(true);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, 1200, 700);
g.drawImage(BG.GGround, BGx, BGy, this);
g.drawImage(BG.GGround, BGx + 1200, BGy, this);
g.drawImage(bens.MCharacter, MainCx, MainCy, this);
updateBG();
updateFall();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE) {
MainCy+=5;
repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
public void updateFall() {
if (MainCy <= 445) {
MainCy += 1;
repaint();
}
}
private void updateBG() {
BGx -= 1;
if (BGx == -1200) {
BGx = 0;
}
repaint();
}
}
“主要”问题是,KeyListener
在Swing中监视键盘输入是一个糟糕的选择,它受到焦点问题的困扰,它注册的组件必须是可聚焦的并具有键盘焦点,因为你要向UI添加按钮,他们会从组件中窃取焦点。
更好的整体解决方案是使用Key Bindings API,它可以让您控制确定应该触发绑定的焦点水平
众所周知,null
布局难以管理和维护。决定组件在屏幕上的显示效果有很多因素。您应尽可能避免使用null
布局并使用一个或多个布局管理器,它们将节省您的大量头发
你已经超越了paint
,但是你没有兑现油漆链(没有调用super.paint
)。绘画是一个复杂的过程,由许多步骤组成。
作为一般规则,您应该覆盖paintComponent
(并在进行任何自定义绘制之前调用super.paintComponent
)。
阅读Painting in AWT and Swing和Performing Custom Painting,了解有关Swing绘画的更多细节
你没有将关键监听器添加到JPnael只是实现接口是不够的,你必须将它添加到面板。
addKeyListner(this);
将此行代码添加到构造函数中。