所以我有一个带有按钮的GUI来启动游戏。 menuPanel表示应显示所有内容的主面板。实际项目更复杂,但在这里我总结了问题。 主要有:
package bbGame;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
JFrame gameFrame = new JFrame();
gameFrame.setSize(700, 600);
gameFrame.setVisible(true);
JPanel menuPanel = new JPanel();
menuPanel.setLayout(null);
menuPanel.setBounds(0, 0, 700, 600);
menuPanel.setBackground(new Color(243, 207, 252));
menuPanel.setVisible(true);
gameFrame.add(menuPanel);
JButton startBricksBreakerButton = new JButton();
startBricksBreakerButton.setText("START BRICKS BREAKER");
startBricksBreakerButton.setLayout(null);
startBricksBreakerButton.setBounds(250, 200, 180, 50);
startBricksBreakerButton.setBackground(new Color(37, 242, 10));
startBricksBreakerButton.setVisible(true);
menuPanel.add(startBricksBreakerButton);
startBricksBreakerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
menuPanel.removeAll();
BricksBreakerGame bbGame=new BricksBreakerGame();
menuPanel.add(bbGame);
bbGame.setVisible(true);
menuPanel.repaint();
}
});
}}
有游戏。它尚未完成,但它显示了某些内容(没有错误)。
package bbGame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class BricksBreakerGame extends JPanel implements KeyListener, ActionListener {
private boolean play = false;
private int score = 0;
private int totalBricks = 21;
private Timer time;
private int speed = 8;
private int playerX = 310;
private int ballposX = 120;
private int ballposY = 350;
private int ballXdir = -1;
private int ballYdir = -2;
public BricksBreakerGame() {
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
// timer = new Timer(delay, this);
// timer.start();
}
public void paint(Graphics g) {
// background
g.setColor(Color.black);
g.fillRect(1, 1, 692, 592);
// borders
g.setColor(Color.yellow);
g.fillRect(0, 0, 3, 592);
g.fillRect(0, 0, 692, 3);
g.fillRect(691, 0, 3, 592);
// the paddle
g.setColor(Color.green);
g.fillRect(playerX, 550, 100, 8);
// the ball
g.setColor(Color.yellow);
g.fillRect(ballposX, ballposY, 20, 20);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
if (playerX >= 600) {
playerX = 600;
} else {
moveRight();
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
if (playerX < 100) {
playerX = 100;
} else {
moveLeft();
}
}
}
private void moveRight() {
play = true;
playerX += 20;
}
private void moveLeft() {
play = true;
playerX -= 20;
}
@Override
public void keyReleased(KeyEvent arg0) {
}
@Override
public void keyTyped(KeyEvent arg0) {
}}
当我点击startBricksBreaker按钮时,由BricksBreakerGame类生成的面板没有显示出来。怎么了?我没有错误,但游戏面板没有显示出来。我认为actionPerformed方法存在问题,但我不知道那里有什么问题...我对Java很新...
答案 0 :(得分:1)
1)menuPanel.removeAll()
。在这里删除面板的所有组件。我认为你应该删除JFrame的菜单面板,然后在里面添加新的BricksBreakerGame
。
你应该这样做:gameFrame.remove(menuPanel)
。
2)menuPanel.add(bbGame)
。在这里,您可以将面板添加到面板中。为什么?您应该将新面板添加到退出的JFrame
,如上所述:gameFrame.add(bbGame)
2)不应要求menuPanel.repaint()
。你应该打电话给revalidate()
,除此之外你应该在更高级别(JFrame
)容器而不是JPanel
上调用它:
gameFrame.revalidate()
3)bbGame.setVisible(true) is
不需要,因为JFrame
已经可见。
试试:
@Override
public void actionPerformed(ActionEvent e) {
gameFrame.remove(menuPanel);
BricksBreakerGame bbGame=new BricksBreakerGame();
gameFrame.add(bbGame);
gameFrame.revalidate();
}
答案 1 :(得分:0)
我想,你的问题是,你永远不会向你的BricksBrakerGame添加东西。尝试将以下内容添加到BricksBrakerGame构造函数中:
this.setSize(100, 100);
this.setBackground(Color.BLACK);
你应该在左上角看到一个黑色的JPanel。
或者你只是调用你的paint()方法并给它一个Graphic来绘画;)