我正在开发这个基本Pong-style
游戏是为了好玩,但我在这GUI
方面遇到了一些困难。本质上,我有一个JFrame
包含所有内容的调用窗口和一个JPanel
调用内容。内容应该是指ContentPanel
我所拥有的。到目前为止,这是我的源代码(对不起,它太长了,其中很多只与游戏有关,而不是这个问题,但我不确定我的问题需要源代码的哪些部分才有意义):
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class Pong extends JApplet {
public static void main(String[] args) {
JFrame window = new JFrame("Pong: The Game of Champions");
JPanel content = new JPanel();
window.setContentPane(content);
window.setSize(1000,600);
window.setLocation(100,100);
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible(true);
window.setResizable(false);
JMenuBar settings = new JMenuBar();
ButtonHandler listener = new ButtonHandler();
window.setJMenuBar(settings);
JMenu toolsMenu = new JMenu("Settings");
settings.add(toolsMenu);
JMenuItem preferences = new JMenuItem("Preferences");
JMenuItem about = new JMenuItem("About");
toolsMenu.add(preferences);
toolsMenu.add(about);
about.addActionListener(listener);
preferences.addActionListener(listener);
}
public void init() {
setContentPane( new ContentPanel() );
}
public static class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent evt) {
String command = evt.getActionCommand();
if (command.equals("About")) {
JOptionPane.showMessageDialog(null, "Made by Kyle Evans for Mr. Richardson's AP Computer Science class. Aww yeah.");
}
else if (command.equals("Preferences")) {
}
}
}
public static class ContentPanel extends JPanel implements KeyListener, FocusListener, MouseListener, ActionListener {
private Timer timer = new Timer(10, this);
private static final int PADDLE_WIDTH = 10; // Length of side of square.
private static final int PADDLE_HEIGHT = 100;
private static final int BALL_WIDTH = 10;
private static final int UP = 1, DOWN = 2;
private Color squareColor; // The color of the square.
private int paddleOneDirection = -1;
private int paddleTwoDirection = -1;
private int paddleOneTop, paddleOneLeft, paddleTwoTop, paddleTwoLeft, paddleSpeed,
ballTop, ballLeft, ballSpeedHorizontal, ballSpeedVertical;
public ContentPanel() {
paddleOneTop = 230; // Initial position of top-left corner of square.
paddleOneLeft = 900;
paddleTwoTop = 230;
paddleTwoLeft = 90;
paddleSpeed = 4;
ballTop = 270;
ballLeft = 490;
ballSpeedHorizontal = (int)(Math.random()*2);
if (ballSpeedHorizontal == 0) {
ballSpeedHorizontal = -1;
}
ballSpeedVertical = (int)(Math.random()*6) - 3;
squareColor = Color.WHITE; // Initial color of square.
setBackground(Color.BLACK);
addKeyListener(this); // Set up event listening.
addFocusListener(this);
addMouseListener(this);
} // end init();
public void paintComponent(Graphics g) {
super.paintComponent(g); // Fills the panel with its
// background color, which is white.
g.setColor(squareColor);
g.fillRect(paddleOneLeft, paddleOneTop, PADDLE_WIDTH, PADDLE_HEIGHT);
g.fillRect(paddleTwoLeft, paddleTwoTop, PADDLE_WIDTH, PADDLE_HEIGHT);
g.fillRect(ballLeft, ballTop, BALL_WIDTH, BALL_WIDTH);
String messageH = "false";
String messageV1 = "false";
String messageV2 = "false";
if (isHittingPaddleHorizontal()) {
messageH = "true";
}
if (!isHittingPaddleHorizontal()) {
messageH = "false";
}
if (isHittingPaddleOneVertical()) {
messageV1 = "true";
}
if (!isHittingPaddleOneVertical()) {
messageV1 = "false";
}
if (isHittingPaddleTwoVertical()) {
messageV2 = "true";
}
if (!isHittingPaddleTwoVertical()) {
messageV2 = "false";
}
g.drawString("Is hitting horizontal: " + messageH,7,20);
g.drawString("Is hitting vertical right: " + messageV1,7,40);
g.drawString("Is hitting vertical left: " + messageV2,7,60);
} // end paintComponent()
// ------------------- Game controlling methods -------------------------
public void moveBall() {
ballLeft -= ballSpeedHorizontal;
ballTop -= ballSpeedVertical;
if (isHittingPaddleHorizontal()) {
if (isHittingPaddleOneVertical()) {
ballSpeedHorizontal = -ballSpeedHorizontal;
// ballSpeedHorizontal++;
}
else if (isHittingPaddleTwoVertical()) {
// ballSpeedHorizontal++;
ballSpeedHorizontal = -ballSpeedHorizontal;
}
}
if ((ballTop <= 0) || (ballTop >= 565)) // Sets the vertical boundaries so the ball bounces off the top and bottom.
ballSpeedVertical = -ballSpeedVertical;
if ((ballLeft > 1000) || ballLeft < 0) {
gameOver();
}
}
public boolean isHittingPaddleOneVertical() {
for (int i = 0; i < PADDLE_HEIGHT + (2 * BALL_WIDTH); i++) {
if (ballTop == (paddleOneTop - BALL_WIDTH + i)) {
return true;
}
}
return false;
}
public boolean isHittingPaddleTwoVertical() {
for (int i = 0; i < PADDLE_HEIGHT + (2 * BALL_WIDTH); i++) {
if (ballTop == (paddleTwoTop - BALL_WIDTH + i)) {
return true;
}
}
return false;
}
public boolean isHittingPaddleHorizontal() {
for (int i = 0; i < 50; i++) {
if ((ballLeft == paddleOneLeft + i) || (ballLeft == paddleTwoLeft + PADDLE_WIDTH - i)) {
return true;
}
}
return false;
}
public void robotControl() {
paddleTwoTop = ballTop - 45;
}
public void gameOver() {
paddleOneTop = 230;
paddleTwoTop = 230;
ballTop = 270;
ballLeft = 490;
ballSpeedHorizontal = (int)(Math.random()*2);
if (ballSpeedHorizontal == 0) {
ballSpeedHorizontal = -1;
}
ballSpeedVertical = (int)(Math.random()*6) - 3;
}
// ------------------- Event handling methods ----------------------
public void focusGained(FocusEvent evt) {
repaint(); // redraw with cyan border
}
public void focusLost(FocusEvent evt) {
repaint(); // redraw without cyan border
}
public void keyTyped(KeyEvent evt) {
char ch = evt.getKeyChar(); // The character typed.
} // end keyTyped()
public void keyPressed(KeyEvent evt) {
int key = evt.getKeyCode(); // keyboard code for the pressed key
timer.start();
if (key == KeyEvent.VK_UP) { // up arrow key
paddleOneDirection = UP;
}
else if (key == KeyEvent.VK_DOWN) { // down arrow key
paddleOneDirection = DOWN;
}
else {
paddleOneDirection = -1; // This prevents other keys from interfering with the paddle movement.
}
if (key == KeyEvent.VK_W) {
paddleTwoDirection = UP;
}
else if (key == KeyEvent.VK_S) {
paddleTwoDirection = DOWN;
}
else {
paddleTwoDirection = -1;
}
if ((paddleOneTop > 1000 - PADDLE_HEIGHT))
paddleOneTop = 1000 - PADDLE_HEIGHT;
if (paddleTwoTop > 1000 - PADDLE_HEIGHT)
paddleTwoTop = 1000 - PADDLE_HEIGHT;
} // end keyPressed()
public void keyReleased(KeyEvent evt) {
paddleOneDirection = -1;
paddleTwoDirection = -1;
}
public void mousePressed(MouseEvent evt) {
requestFocus();
}
public void mouseEntered(MouseEvent evt) { } // Required by the
public void mouseExited(MouseEvent evt) { } // MouseListener
public void mouseReleased(MouseEvent evt) { } // interface.
public void mouseClicked(MouseEvent evt) { }
public void actionPerformed(ActionEvent evt) {
switch (paddleOneDirection) {
case UP:
paddleOneTop -= paddleSpeed;
break;
case DOWN:
paddleOneTop += paddleSpeed;
break;
}
switch (paddleTwoDirection) {
case UP:
paddleTwoTop -= paddleSpeed;
break;
case DOWN:
paddleTwoTop += paddleSpeed;
break;
}
robotControl();
moveBall();
repaint();
}
} // end nested class ContentPanel
} // end class
(代码有点乱,我很抱歉,我对 Java 和一般编程都很陌生。)
以下是我的问题:
如何初始化默认
JFrame
显示内容?JPanel
我如何开始创建一个
JPanel
可以切换的单独的?我的最终目标是当用户点击“首选项”时有一个单独的JPanel
出现,然后能够在首选项JPanel
和内容之间来回切换JPanel
。
我将不胜感激任何帮助。