当我在同一JFrame窗口中单击开始按钮时,我无法找到一种从主菜单过渡到实际游戏的方法。现在,我将它设置为当您单击开始按钮时,它会关闭菜单窗口,并在一个单独的窗口中创建游戏正在运行的窗口。但是我希望能够在同一窗口中执行此操作,以使其看起来更有条理。有什么建议/想法可以让我推荐我来实现这一目标?
Main.java
import java.awt.Color;
import javax.swing.JFrame;
public class Main
{
JFrame window;
public Main()
{
window = new JFrame(); // Main JFrame where I want everything to be transitioned into
window.setSize(600, 600);
window.setTitle("Snake");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.black);
window.setLocationRelativeTo(null); // Centers the window
window.setLayout(null);
}
public static void main(String[] args)
{
new Menu();
}
}
Menu.java
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Menu extends Main
{
//JFrame window;
Container con;
JPanel TitleSection, StartButtonSection, LeaderboardsSection; //
Part/Section of the frame where the title is going to be put in
JLabel Title; // The actual title name
Font TitleFont = new Font("Times New Roman", Font.PLAIN, 60);
Font NormalFont = new Font("Times New Roman", Font.PLAIN, 20);
JButton Start, Leaderboards;
StartGame Startgame = new StartGame(); // Button function
public Menu()
{
//window = new JFrame();
/*
* window.setSize(600, 600); window.setTitle("Snake");
* window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
* window.getContentPane().setBackground(Color.black);
* window.setLocationRelativeTo(null); // Centers the window
* window.setLayout(null);
*/
con = window.getContentPane(); // Holds all the JPanels together
TitleSection = new JPanel();
TitleSection.setBounds(50, 100, 500, 100); // First two numbers is the left corner and the other two are
// for the width and height
TitleSection.setBackground(Color.red);
Title = new JLabel("Power Up Snake");
Title.setForeground(Color.black);
Title.setBackground(Color.white);
Title.setFont(TitleFont);
StartButtonSection = new JPanel();
StartButtonSection.setBounds(200, 250, 200, 100);
StartButtonSection.setBackground(Color.black);
Start = new JButton("Start");
Start.setForeground(Color.black);
Start.setBackground(Color.white);
Start.setPreferredSize(new Dimension(150, 75));
Start.setFont(NormalFont);
Start.addActionListener(Startgame);
LeaderboardsSection = new JPanel();
LeaderboardsSection.setBounds(200, 375, 200, 100);
LeaderboardsSection.setBackground(Color.black);
Leaderboards = new JButton("Leaderboards");
Leaderboards.setForeground(Color.black);
Leaderboards.setBackground(Color.white);
Leaderboards.setPreferredSize(new Dimension(150, 75));
Leaderboards.setFont(NormalFont);
TitleSection.add(Title);
StartButtonSection.add(Start);
LeaderboardsSection.add(Leaderboards);
con.add(TitleSection);
con.add(StartButtonSection);
con.add(LeaderboardsSection);
window.setVisible(true);
}
public JFrame getWindow()
{
return window;
}
public void setWindow(JFrame window)
{
this.window = window;
}
public void StartGame()
{
JFrame frame = new JFrame(); // The temporary game frame
Gamepanel gamepanel = new Gamepanel(); // Starts the game
frame.add(gamepanel);
frame.pack(); // Sets minimum width and height in case the given width and
// height are not big enough to be visible
frame.setTitle("Snake");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Centers the window
frame.setVisible(true);
}
public class StartGame implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
window.setVisible(false);
StartGame();
}
}
}
Gamepanel.java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import java.util.Scanner;
public class Gamepanel extends JPanel implements Runnable, KeyListener
{
// Runnable,能够将Threads和KeyListener用于键盘按钮
private static final long serialVersionUID = 1L;
public static final int WIDTH = 600, HEIGHT = 600;
private Thread thread;
private boolean running;
private boolean pause;
private boolean left, up, down;
private boolean right = true;
private SnakeBody s;
private ArrayList<SnakeBody> Snake;
private Fruit o;
private ArrayList<Fruit> Fruits;
private Random r;
private int xCoor = 10; // Where the snake starts
private int yCoor = 10; // Where the snake starts
private int SnakeSize = 5;
private int ticks = 0;
private int score = 0;
JPanel main = new JPanel();
//JLabel scoreLabel = new JLabel("Score: " + score, SwingConstants.CENTER);
Scanner in = new Scanner(System.in);
public boolean disappear = false;
private int ColorNum = 0;
public Gamepanel()
{
setFocusable(true);
setPreferredSize(new Dimension(WIDTH, HEIGHT)); // Sets size for Window frame
addKeyListener(this); // Creates a key listener that can take in keys
Snake = new ArrayList<SnakeBody>(); // Where the snake will be held
Fruits = new ArrayList<Fruit>();
r = new Random(); // An RNG
//main.add(scoreLabel);
start();
}
public void start()
{
setRunning(true);
thread = new Thread(this);
thread.start();
}
public void stop()
{
try
{
thread.join(); // Waits for the thread to die
}
catch(InterruptedException e)
{
e.printStackTrace(); // Look at what the error is
}
}
public void tick()
{
if(Snake.size() == 0)
{
s = new SnakeBody(xCoor, yCoor, 10); // Creates body part
Snake.add(s); // Puts body part into Snake
}
++ticks;
if(ticks > 250000 && pause == false)
{
if(left)
{
xCoor--;
}
if(right)
{
xCoor++;
}
if(up)
{
yCoor--;
}
if(down)
{
yCoor++;
}
ticks = 0;
s = new SnakeBody(xCoor, yCoor, 10);
Snake.add(s);
if(Snake.size() > SnakeSize)
{
Snake.remove(0);
}
}
if(Fruits.size() == 0) // If there's no fruit, put one at a random spot
{
int xCoor = r.nextInt(59);
int yCoor = r.nextInt(59);
o = new Fruit(xCoor, yCoor, 10);
Fruits.add(o);
}
for(int i = 0; i < Fruits.size(); i++) // If the snake eats the fruit
{
if(xCoor == Fruits.get(i).getxCoor() && yCoor == Fruits.get(i).getyCoor())
{
ColorNum = r.nextInt(8);
++SnakeSize;
++score;
Fruits.remove(i); // Gets rid of the fruit
++i;
}
}
if(xCoor < 0 || xCoor > 59 || yCoor < 0 || yCoor > 59) // Collision on border
{
setRunning(false);
}
for(int i = 0; i < Snake.size(); i++) // Collision on snake's body
{
if(xCoor == Snake.get(i).getxCoor() && yCoor == Snake.get(i).getyCoor() && (i != Snake.size() - 1))
{
setRunning(false);
}
}
}
public void paint(Graphics g) // Draws background and snake on screen
{
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT); // Actually sets the background color
for(int i = 0; i < WIDTH/10; i++) // Draws the grid
{
g.drawLine(i * 10, 0, i * 10, HEIGHT);
}
for(int i = 0; i < HEIGHT/10; i++) // Draws the grid
{
g.drawLine(0, i * 10, HEIGHT, i * 10);
}
for(int i = 0; i < Snake.size(); i++) // Draws the snake
{
Snake.get(i).draw(g, ColorNum);
}
for(int i = 0; i < Fruits.size(); i++) // Draws the fruit
{
Fruits.get(i).draw(g);
}
for(int i = 0; i < Fruits.size(); i++) // If the snake eats the fruit
{
if(xCoor == Fruits.get(i).getxCoor() && yCoor == Fruits.get(i).getyCoor())
{
Snake.get(i).draw(g, ColorNum);
}
}
g.setColor(Color.WHITE);
g.setFont(new Font("arial", Font.PLAIN, 20)); // Draws the score on the top left corner
g.drawString("Score: " + score, 280, 30);
if(pause == true)
{
g.setColor(Color.WHITE);
g.setFont(new Font("arial", Font.BOLD, 50)); // Draws the score on the top left corner
g.drawString("Paused", 212, 300);
}
if(running == false)
{
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT); // Actually sets the background color
g.setColor(Color.WHITE); g.setFont(new Font("arial", Font.BOLD, 50));
g.drawString("GAME OVER", 150, 300);
g.setColor(Color.WHITE); g.setFont(new Font("arial", Font.PLAIN, 20));
g.drawString("Do you want to continue? (Y/N)", 165, 350);
}
}
public void run() // Always called whenever a thread is created
{
while(running)
{
tick();
repaint();
}
}
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode(); // Takes input of keyboard key
if(key == KeyEvent.VK_RIGHT && !left) // So you can't go right when you're already going left
{
right = true;
up = false;
down = false;
}
if(key == KeyEvent.VK_LEFT && !right) // So you can't go left when you're already going right
{
left = true;
up = false;
down = false;
}
if(key == KeyEvent.VK_UP && !down) // So you can't go up when you're already going down
{
up = true;
left = false;
right = false;
}
if(key == KeyEvent.VK_DOWN && !up) // So you can't go down when you're already going up
{
down = true;
left = false;
right = false;
}
if(key == KeyEvent.VK_P)
{
pause = !pause;
}
//if(pause = true && key == KeyEvent.VK_Q)
{
// Send me back
}
if(running == false)
{
if(key == KeyEvent.VK_Y)
{
// Save score if it's better than any of the top 5 high scores and ask for username
score = 0;
setRunning(true);
run();
repaint();
}
else if(key == KeyEvent.VK_N)
{
stop();
System.exit(0);
}
}
}
@Override
public void keyReleased(KeyEvent e)
{
}
public boolean isRunning()
{
return running;
}
public void setRunning(boolean running)
{
this.running = running;
}
}