gpt4 book ai didi

java - setContentPane 覆盖了所有内容并且 JButton 无法正常工作

转载 作者:行者123 更新时间:2023-11-30 08:14:52 25 4
gpt4 key购买 nike

我正在制作一款游戏,玩家试图在迷宫中导航,但我遇到了两个绝对难倒我的错误。我的第一个问题是使用setContentPane()。每当我将其设置为我想要的背景时,它都会覆盖屏幕上的其他所有内容。我尝试过将它重新安置到我认为可能的每个地方,但它仍然掩盖了它。我的第二个问题是关于我的 JButton。每当我单击它时,它都会按预期更改播放速度,但之后我无法移动。当我在 Keylistener 中使用该方法时,它会起作用,并且它们内部具有相同的代码。我已经包含了我认为必要的所有代码,但如果您需要更多代码,请告诉我。该项目的完整源代码可以在 Here 找到。 .

package game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;

public class Game extends JPanel implements ActionListener, KeyListener {

private boolean upPressed = false;
private boolean downPressed = false;
private boolean rightPressed = false;
private boolean leftPressed = false;

private int tileSize = 200;
private int playerSize = tileSize / 4;
private int playerSpeed = playerSize / 10;
private int mazeX = 51;
private int mazeY = 51;

private int[][] maze = new int[mazeX][mazeY];
private int[][] initX = new int[mazeX][mazeY];
private int[][] initY = new int[mazeX][mazeY];
private boolean[][] visited = new boolean[mazeX][mazeY];

private int currentCellX = 0;
private int currentCellY = 0;
private int deltaX = 0;
private int deltaY = 0;

JComponent background = new JLabel(new ImageIcon(Main.class.getResource("/assets/ui/mainMenu.png")));
JButton adventure = new JButton();
JButton freePlay = new JButton();
JButton exit = new JButton();
JButton pause = new JButton();
JButton resume = new JButton();

private String screen = "menu";
public Main main;


public Game(Main main) {
this.main = main;

setFocusable(true);
addKeyListener(this);
addButtons();
setUpInitialCoordinates();
Timer timer = new Timer(1000 / 120, this);
timer.start();
}


private void tick() {
switch (screen) {
case "playing" :
background.setVisible(false);
if (upPressed) {
deltaY += playerSpeed;
} else if (downPressed) {
deltaY -= playerSpeed;
}
if (rightPressed) {
deltaX -= playerSpeed;
} else if (leftPressed) {
deltaX += playerSpeed;
}
break;
case "paused":
background.setVisible(false);
adventure.setVisible(false);
freePlay.setVisible(false);
pause.setVisible(false);
exit.setVisible(true);
break;
case "menu":
main.setContentPane(new JLabel(new ImageIcon(Main.class.getResource("/assets/ui/mainMenu.png"))));
adventure.setVisible(true);
freePlay.setVisible(true);
pause.setVisible(false);
exit.setVisible(true);
break;
}
repaint();
}

private void addButtons() {
setLayout(null);
add(freePlay);
freePlay.setBounds(10, 100, 180, 80);
freePlay.setSize(180, 80);
freePlay.setAction(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
downPressed = false;
rightPressed = false;
leftPressed = false;
generateMaze();
screen = "playing";
}
});
freePlay.setIcon(new ImageIcon(Main.class.getResource("/assets/ui/buttons/freePlayUnselected.png")));
freePlay.setRolloverIcon(new ImageIcon(Main.class.getResource("/assets/ui/buttons/freePlaySelected.png")));
}

private void setUpInitialCoordinates() {
}


private void generateMaze() {
}


private boolean checkForUnvisitedCells() {
}


private boolean checkForUnvisitedAdjacentCells() {
}


@Override
public void actionPerformed(ActionEvent e) {
tick();
}


@Override
public void keyTyped(KeyEvent e) {}


@Override
public void keyPressed(KeyEvent e) {
}


@Override
public void keyReleased(KeyEvent e) {
}


@Override
public void paintComponent(Graphics g) {
}
}

最佳答案

setContentPane 就是这样做的,它用新组件替换了以前用作内容的内容。

RootPane

仔细看看How to Use Root Panes了解更多详情。

我建议更好的解决方案是使用 CardLayout,但在这种情况下,更好的解决方案是仅更改 icon 属性当前内容 Pane ...

background.setIcon(new ImageIcon(Main.class.getResource("/assets/ui/mainMenu.png")));

然后简单地将背景设置为框架的内容 Pane ...

JLabel background = new JLabel(new ImageIcon(Main.class.getResource("/assets/ui/mainMenu.png")));

//....
public Game(Main main) {
this.main = main;
setContentPane(background);
//...

那就永远不要更改内容 Pane ...

然后,您可以使用 CardLayout 更改内容 Pane 上显示的内容

My second question is about my JButton. Whenever I click it, it changes to the playing speed as intended, but I can't move afterwards. It works when I use the method in my Keylistener

欢迎来到为什么不应该使用 KeyListener 的奇妙世界,按钮已经窃取了键盘焦点,并且 KeyListener 将不再响应键盘输入,因为它依赖于它注册为可聚焦和聚焦的组件。请改用键绑定(bind)。 How to Use Key Bindings

关于java - setContentPane 覆盖了所有内容并且 JButton 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29811630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com