我正在研究Java项目,这是儿童游戏。我有菜单类也有主要方法,我有扩展JPanel的Board类,并且有游戏GUI,我有Player类,有玩家的移动方法。我在菜单类中设计了整个菜单,扩展了jframe,我在一个框架中有所有动作和事件,这是Menu对象,所以我使用了大量的repaint()和validate()方法来更改主框架中的JPanels。它适用于某些人,但是当我想为用户添加游戏面板以启动我的设计中的Board对象的游戏时。还有一个不必要的框架。在我的菜单中,第一个用户看到一个面板,我在我的代码中调用了startPanel"开始游戏按钮"," Top Scores"," Credits"和"退出"纽扣。然后用户点击开始游戏,然后userNamepanel到来并且用户输入他/她的名字,然后他/她通过使用启动游戏按钮来启动游戏,该按钮移除userNamePanel并将Board添加到框架。但问题出现了,java在那个框架中打开Board面板但同时它创建了一个新的Menu框架实例,我在屏幕上有2个框架,一个显示Board面板,另一个显示起始面板。那我怎么能摆脱这个额外的框架呢?所以这是我的代码;首先是董事会成员
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel implements ActionListener {
private Timer timer;
private Player player;
private final int DELAY = 10;
private Image image;
private JLabel label;
private JPanel panel;
private JButton button1, button2;
private int mapNumber;
private boolean talkProfessor = false;
private ImageIcon front = new ImageIcon("player_front.png");
private ImageIcon left = new ImageIcon("player_left.png");
private ImageIcon right = new ImageIcon("player_right.png");
private ImageIcon back = new ImageIcon("player_back.png");
private ImageIcon front2 = new ImageIcon("player_front2.png");
private ImageIcon left2 = new ImageIcon("player_left2.png");
private ImageIcon right2 = new ImageIcon("player_right2.png");
private ImageIcon back2 = new ImageIcon("player_back2.png");
private ImageIcon front3 = new ImageIcon("player_front3.png");
private ImageIcon left3 = new ImageIcon("player_left3.png");
private ImageIcon right3 = new ImageIcon("player_right3.png");
private ImageIcon back3 = new ImageIcon("player_back3.png");
public Board() {
initBoard();
}
private void initBoard() {
setPreferredSize(new Dimension(900,900));
image = Toolkit.getDefaultToolkit().createImage("map4.png");
mapNumber = 4;
addKeyListener(new TAdapter());
setFocusable(true);
player = new Player();
timer = new Timer(DELAY, this);
timer.start();
}
public Image getImage(){
return image;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image,0,0,null);
doDrawing(g);
}
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(player.getImage(), player.getX(), player.getY(), this);
}
public void actionPerformed(ActionEvent e) {
player.move();
if(((player.getY() == 350 && mapNumber == 1) && (player.getX() <= 500 && player.getX() >= 400)) ){
JOptionPane.showMessageDialog(null,"" +
" ");
player.setdY(0);
player.setY(player.getY() + 50);
}
if((player.getX() == 350 && mapNumber == 1) && (player.getY() <= 500 && player.getY() >= 400)){
JOptionPane.showMessageDialog(null,"");
player.setX(player.getX() + 50);
player.setdX(0);
}
if((player.getY() == 750 && mapNumber == 2) && (player.getX() <= 500 && player.getX() >= 400)){
JOptionPane.showMessageDialog(null,"" +
"");
player.setY(player.getY() - 50);
player.setdY(0);
talkProfessor = true;
}
if((player.getY() == 500 && mapNumber == 3) && (player.getX() <= 500 && player.getX() >= 400)){
JOptionPane.showMessageDialog(null,"" +
" " +
"");
player.setY(player.getY() - 50);
player.setdY(0);
}
if((player.getY() == 400 && mapNumber == 3) && (player.getX() <= 280 && player.getX() >= 270)){
JOptionPane.showMessageDialog(null,"");
player.setY(player.getY() + 10);
player.setdX(0);
player.setdY(0);
}
if((player.getX() == 200 && mapNumber == 4) && (player.getY() <= 500 && player.getY() >= 400)){
if(talkProfessor){
JOptionPane.showMessageDialog(null,"" +
" ");
}
else
JOptionPane.showMessageDialog(null,"" +
" ");
player.setX(player.getX() + 50);
player.setdX(0);
}
if((player.getX() == 100 && mapNumber == 5) && (player.getY() <= 500 && player.getY() >= 400)){
JOptionPane.showMessageDialog(null," " +
"");
player.setX(player.getX() + 50);
player.setdX(0);
}
if((player.getY() == 400 && mapNumber == 101) && player.getX() <= 50){
JOptionPane.showMessageDialog(null,"");
player.setX(player.getX() + 50);
player.setdY(0);
}
if((player.getX() >= 800 && mapNumber == 101) && player.getY() == 420){
player.setX(player.getX() - 50);
player.setdX(0);
player.setdY(0);
JOptionPane.showMessageDialog(null," ");
}
if(player.getX() == 850 && mapNumber == 1){
image = Toolkit.getDefaultToolkit().createImage("map2.png");
player.setX(10);
mapNumber = 2;
}
if(player.getX() == 0 && mapNumber == 2){
image = Toolkit.getDefaultToolkit().createImage("map.png");
player.setX(840);
mapNumber = 1;
}
if((player.getX() == 240 && mapNumber == 2) && player.getdX() == 1){
player.setImage(front2, left2, right2, back2);
}
if((player.getX() == 200 && mapNumber == 2) && player.getdX() == -1){
player.setImage(front, left, right, back);
}
if(player.getY() == 0 && mapNumber == 3){
image = Toolkit.getDefaultToolkit().createImage("map4.png");
player.setY(840);
mapNumber = 4;
}
if(player.getY() == 850 && mapNumber == 4){
image = Toolkit.getDefaultToolkit().createImage("map3.png");
player.setY(10);
mapNumber = 3;
}
if(player.getY() == 0 && mapNumber == 1){
image = Toolkit.getDefaultToolkit().createImage("map5.png");
player.setY(840);
mapNumber = 5;
}
if(player.getY() == 850 && mapNumber == 1){
image = Toolkit.getDefaultToolkit().createImage("map4.png");
player.setY(10);
mapNumber = 4;
}
if((player.getY() == 400 && mapNumber == 3) && (player.getX() >= 100 && player.getX() <= 200)){
image = Toolkit.getDefaultToolkit().createImage("house.png");
player.setX(400);
player.setY(840);
mapNumber = 101;
player.setImage(front3, left3, right3, back3);
}
if(player.getY() == 850 && mapNumber == 101){
image = Toolkit.getDefaultToolkit().createImage("map3.png");
player.setX(150);
player.setY(410);
mapNumber = 3;
player.setImage(front, left, right, back);
}
repaint();
}
private class TAdapter extends KeyAdapter {
@Override
public void keyReleased(KeyEvent e) {
player.keyReleased(e);
}
@Override
public void keyPressed(KeyEvent e) {
player.keyPressed(e);
}
}
}
然后这里是Menu类,它也有主要方法
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Menu extends JFrame {
private JButton buttonToStartTheQuest, buttonToShowCredits, buttonToExit, buttonToShowTopScores, buttonToTurnMenu, buttonToExit1, buttonToExit2, buttonToTurnMenu2;
private JPanel startingPanel, creditsPanel, topScoresPanel, userNamePanel;
private JLabel creditsLabel, topScoresLabel;
private JLabel inputLabel,outputLabel;
private JTextField userName;
private ImageIcon image;
private JButton startTheGame;
private Board board;
public Menu() {
buttonToStartTheQuest = new JButton("Start A New Quest");
buttonToShowTopScores = new JButton("View Top Scores");
buttonToShowCredits = new JButton("Credits");
buttonToExit = new JButton("Exit");
buttonToTurnMenu=new JButton("Turn Back To Main Menu");
buttonToExit1 = new JButton("Exit");
buttonToExit2 = new JButton("Exit");
buttonToTurnMenu2=new JButton("Turn Back To Main Menu");
startTheGame=new JButton("Start The Game");
startingPanel = new JPanel();
startingPanel.setPreferredSize(new Dimension(900,900));
startingPanel.setLayout(new GridLayout(4,1));
creditsPanel=new JPanel();
creditsPanel.setPreferredSize(new Dimension(900,900));
creditsLabel=new JLabel("Credits\nThis Game is Created By:\nPokeMan");
creditsPanel.add(creditsLabel);
image=new ImageIcon("bigKid.png");
userName=new JTextField(20);
userName.addActionListener(new UserListener());
inputLabel= new JLabel("Enter Your User Name To Start The Quest",image, SwingConstants.CENTER);
outputLabel=new JLabel("");
userNamePanel=new JPanel();
userNamePanel.setPreferredSize(new Dimension(900,900));
userNamePanel.setBackground(Color.white);
userNamePanel.add(inputLabel);
userNamePanel.add(userName);
userNamePanel.add(outputLabel);
topScoresPanel=new JPanel();
topScoresPanel.setPreferredSize(new Dimension(900,900));
topScoresLabel=new JLabel("Top Score\n 1----\n2-----\n3------");
topScoresPanel.add(topScoresLabel);
ButtonListener listener = new ButtonListener();
buttonToStartTheQuest.addActionListener(listener);
buttonToShowTopScores.addActionListener(listener);
buttonToShowCredits.addActionListener(listener);
buttonToExit.addActionListener(listener);
ButtonListener1 listener1= new ButtonListener1();
buttonToExit1.addActionListener(listener1);
buttonToTurnMenu.addActionListener(listener1);
ButtonListener2 listener2 = new ButtonListener2();
buttonToExit2.addActionListener(listener2);
buttonToTurnMenu2.addActionListener(listener2);
startingPanel.add(buttonToStartTheQuest);
startingPanel.add(buttonToShowTopScores);
startingPanel.add(buttonToShowCredits);
startingPanel.add(buttonToExit);
setTitle("Quest for Philosophy");
setSize(900, 900);
setResizable(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(startingPanel);
pack();
setVisible(true);
startTheGame.addActionListener(new GameStarter());
}
private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if(source == buttonToExit){
System.exit(0);
}
if(source == buttonToShowCredits){
remove(startingPanel);
creditsPanel.add(buttonToExit1);
creditsPanel.add(buttonToTurnMenu);
add(creditsPanel);
repaint();
validate();
}
if(source == buttonToShowTopScores){
remove(startingPanel);
topScoresPanel.add(buttonToExit2);
topScoresPanel.add(buttonToTurnMenu2);
add(topScoresPanel);
repaint();
validate();
}
if(source==buttonToStartTheQuest){
remove(startingPanel);
add(userNamePanel);
repaint();
validate();
}
}
}
private class ButtonListener1 implements ActionListener{
public void actionPerformed(ActionEvent e1){
Object source1= e1.getSource();
if(source1==buttonToExit1)
System.exit(0);
else if(source1==buttonToTurnMenu){
System.out.println("a");
remove(creditsPanel);
add(startingPanel);
repaint();
validate();
}
}
}
private class ButtonListener2 implements ActionListener{
public void actionPerformed(ActionEvent e2){
Object source2=e2.getSource();
if(source2==buttonToExit2)
System.exit(0);
else if(source2==buttonToTurnMenu2){
remove(topScoresPanel);
add(startingPanel);
repaint();
validate();
}
}
}
private class UserListener implements ActionListener{
public void actionPerformed(ActionEvent event){
String text=userName.getText();
outputLabel.setText("Game Is Started with User Name: " + text);
userNamePanel.add(startTheGame);
repaint();
validate();
}
}
private class GameStarter implements ActionListener{
public void actionPerformed(ActionEvent event){
Object source=event.getSource();
if(source==startTheGame){ //here is where problem occurs
System.out.println("a");
board= new Board();
remove(userNamePanel);
add(board);
repaint();
validate();
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
Menu ex = new Menu();
}
});
}
}
提前致谢。
答案 0 :(得分:1)
您可以使用JOptionPane
。
例如:
JOptionPane.showOptionDialog(_parentComponent_,
_message_, // Message String
_title_, // Title of the Dialog
_optionType_, // 0, as you give the options
_messageType_, // type - information, question etc
_icon_, // whatever icon you want
_options_, // the options as an array of Objects
_initialValue_ );// The selected value - index
这将创建一个带有按钮的对话框,其中包含来自o
元素的文本。它将返回所选项的索引。
对你来说,可能是:
Object o = {"Start", "Top Scores"....};
int selectedIndex = JOptionPane.showOptionDialog(null, "Choose an option", "Game", 0, -1, null,o, 0);