我是一个尝试创建游戏的初学者。游戏是空的,我没有开始写它。
我的问题:当我点击开始按钮时,我想在相同的框架中启动游戏,但是当我点击开始时,使用此代码,程序会打开另一个框架,并在原始框架中删除面板。
我该怎么做这个简单的菜单?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import panelimage.Image;
public class Project0 extends JFrame{
private static final long serialVersionUID = 1L;
private Game JPanelGame;
private Image MainMenu = new Image(new File("src/img/menu_background.jpg"));
JButton start = new JButton("START GAME"), exit = new JButton ("LEAVE GAME");
public Project0() {
super();
initFrame();
initMenu();
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
JPanelGame.keyPressed(evt);
}
public void keyReleased(KeyEvent evt) {
JPanelGame.keyReleased(evt);
}
});
}
private void initFrame() {
setResizable(false);
setBounds(new Rectangle(400, 400, 600, 400));
MainMenu.setLayout(new BorderLayout());
}
private void initMenu() {
initButtons();
MainMenu.setLayout(null);
MainMenu.add(start);
MainMenu.add(exit);
add(MainMenu);
setContentPane(MainMenu);
setTitle("Project0");
}
private void initButtons() {
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
JPanelGame = new Game();
remove(MainMenu);
add(JPanelGame, BorderLayout.CENTER);
setContentPane(JPanelGame);
invalidate(); validate();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Project0 Game = new Project0();
Game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Game.setVisible(true);
}
});
}
});
start.setLocation(225,100);
start.setSize(150,50);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
exit.setLocation(225,200);
exit.setSize(150,50);
}
public static void main(String[] args) {
Project0 Game = new Project0();
Game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Game.setVisible(true);
}
}
此框架将开始
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import highscores.*;
public class Game extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
Thread game;
BufferedImage background;
HighscoreManager scores = new HighscoreManager();
public Game(){
super(true);
try{
background = ImageIO.read(new File(""));
}catch(Exception e) {}
game=new Thread(this);
game.start();
}
public void paintComponent(Graphics graphic2d){
setOpaque(false);
super.paintComponent(graphic2d);
graphic2d.drawImage(background, 0, 0, null);
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
}
if (key == KeyEvent.VK_RIGHT) {
}
if (key == KeyEvent.VK_UP) {
}
if (key == KeyEvent.VK_DOWN) {
}
if (key == KeyEvent.VK_A) {
}
if (key == KeyEvent.VK_D) {
}
if (key == KeyEvent.VK_W) {
}
if (key == KeyEvent.VK_S) {
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
}
if (key == KeyEvent.VK_RIGHT) {
}
if (key == KeyEvent.VK_UP) {
}
if (key == KeyEvent.VK_DOWN) {
}
if (key == KeyEvent.VK_A) {
}
if (key == KeyEvent.VK_D) {
}
if (key == KeyEvent.VK_W) {
}
if (key == KeyEvent.VK_S) {
}
}
public void run(){
try {Thread.sleep(50);}catch(InterruptedException ex){}
}
}
答案 0 :(得分:2)
根据您的需要,我会看到两种选择。您可以从框架中删除所有组件并添加新内容。但通过这样做,你失去了'菜单框架'。另一种选择(以及两者中最好的IMO)是选择CardLayout
,其中一张牌是您的菜单,另一张是您的游戏。然后,“开始”按钮将简单地从“菜单卡”切换到“游戏卡”(如果需要,则启动游戏)。在你的游戏中,你可以有一个按钮/键/ ...反向(从游戏卡切换到菜单卡)
我正在查看你的代码,但我放弃了(当然,因为@orzechowskid已经指出了你需要调整的内容)。如果您要在此网站上发布更多代码,请提供一些提示:
panelimage.Image
课程与java.awt.Image
课程无关然后根据我所看到的代码提供一些与Swing相关的提示:
LayoutManager
并删除null
布局。 Visual guide to layout managers可能是一个很好的起点Thread.sleep
用法并将多个Thread
与Swing UI结合使用。确保您了解Swing threading rules 答案 1 :(得分:1)
当您在按钮的动作监听器内拨打SwingUtilities.invokeLater()
时,您需要创建第二个Project0
对象。无论何时创建Project0
,您都会在屏幕上绘制一个JFrame,其中包含一个主菜单。移除对SwingUtilities.invokeLater()
的呼叫,您应该留下一个帧。