如果我在点击开始后通过主菜单运行,战舰应用程序崩溃

时间:2010-08-23 17:06:10

标签: java swing

我正在使用套接字创建一个包含4个类的战舰游戏。计算机,播放器消息和菜单类。为了开始游戏,我运行了作为服务器的计算机类,然后运行菜单类,调出菜单。通过菜单我点击开始创建一个看起来像这个

的新玩家对象
public static void runPlayer()
{
    Player client = new Player();   //creates player
    client.createBoard();
    client.run();


}

如果我运行计算机类然后运行播放器类,游戏运行成功,则运行完全没有菜单类。但是当我在菜单中调用run player方法时,会弹出一个窗口中没有任何内容。这是我的菜单类

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import sun.audio.*;

public class Menu  {
    private javax.swing.JLabel image;
    private static final int EXIT_ON_CLOSE = 0;
    JFrame frame = new JFrame();
    JPanel panel = new JPanel(new BorderLayout());
    JLabel statusbar = new JLabel(" Battleship");
    JLabel Battleship = new JLabel("          Battleship ");
    static AudioPlayer MGP = AudioPlayer.player;
    static AudioStream BGM;
    AudioData MD;

    static ContinuousAudioDataStream loop = null;
    public static void waiting (int n)
    {

        long t0, t1;

        t0 =  System.currentTimeMillis();

        do{
            t1 = System.currentTimeMillis();
        }
        while (t1 - t0 < n);
    }


    public Menu() 
    {

        frame.setTitle("Battleship");


        statusbar.setBorder(BorderFactory.createEtchedBorder(
                EtchedBorder.RAISED));
        Battleship.setBorder(BorderFactory.createEtchedBorder(
                EtchedBorder.RAISED));

        panel.setLayout(null);

        JButton start = new JButton("Start");
        start.setBounds(100, 660, 80, 25);


        JButton exit = new JButton("Exit");
        exit.setBounds(190, 660, 80, 25);

        JButton StopMusic = new JButton("Stop Music");
        StopMusic.setBounds(300, 660, 160, 25);

        JButton StartMusic = new JButton("Start Music");
        StartMusic.setBounds(470, 660, 160, 25);


        Battleship.setFont(new Font("Courier New", Font.ITALIC, 36));
        Battleship.setForeground(Color.BLACK);



        image = new javax.swing.JLabel();
        image.setIcon(new javax.swing.ImageIcon("./battleship2.jpg"));
        frame.add(image, BorderLayout.EAST);
        frame.pack();



        frame.add(start);
        frame.add(exit);
        frame.add(StopMusic);
        frame.add(StartMusic);


        frame.add(panel);
        frame.add(statusbar, BorderLayout.SOUTH);
        frame.add(Battleship, BorderLayout.NORTH );



        frame.setSize(700, 800);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setVisible(true);

        music();



        start.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e)
            {

                {                           
                        frame.dispose();                //closes frame
                        stopMusic();                    //stops music
                        //waiting(500);
                        runPlayer();





            }}
         });
        StopMusic.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e)
            {

                {                           
                    stopMusic();

                }
            }
         });
        StartMusic.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e)
            {

                {                           
                    startMusic();

                }
            }
         });

       exit.addActionListener(new ActionListener() 
       {
        public void actionPerformed(ActionEvent e)
        {

            System.out.println( "Ending Game" );
            System.exit(0);
        }
     });}




    public static void music() 
    {       
        try
        {
            InputStream test = new FileInputStream("./battle.wav");
            BGM = new AudioStream(test);
            AudioPlayer.player.start(BGM);




        }
        catch(FileNotFoundException e){
            System.out.print(e.toString());
        }
        catch(IOException error)
        {
            System.out.print(error.toString());
        }
        MGP.start(loop);





    }
    public void stopMusic() 
    {
        if (BGM != null)
          AudioPlayer.player.stop(BGM);
        if (loop != null)
          AudioPlayer.player.stop(loop);
      }
    public void startMusic() {
        try
        {
            InputStream test = new FileInputStream("./battle.wav");
            BGM = new AudioStream(test);
            AudioPlayer.player.start(BGM);




        }
        catch(FileNotFoundException e){
            System.out.print(e.toString());
        }
        catch(IOException error)
        {
            System.out.print(error.toString());
        }
        MGP.start(loop);
      }
    public static void runPlayer()
    {
        Player client = new Player();   //creates player
        client.createBoard();
        client.run();


    }

    public static void main(String[] args) 
    {       
        new Menu();                       
    }
}

我认为我的问题出在这个侦听器方法的某个地方

start.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e)
            {

                {                           
                        frame.dispose();                //closes frame
                        stopMusic();                    //stops music
                        //waiting(500);
                        runPlayer();





            }}
         });

2 个答案:

答案 0 :(得分:2)

使用提供的信息回答这个问题有点困难。但我必须猜测runPlayer()方法正在做什么。我假设有一些类型的while循环阻止Event Dispatch Thread刷新你创建的新JFrame。

尝试将stopMusic()和runPlayer()放在新线程中。

基本上是这样的

start.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    frame.dispose(); // closes frame

    new Thread(){
      public void run(){
        stopMusic(); // stops music
        runPlayer();
      }
    }.start();
  }
}); 

答案 1 :(得分:1)

我认为问题出在您展示的代码之外;它可以是从缺少setVisible()synchronization问题的任何内容。 FWIW,我对代码有一些看法:

  1. 在刚刚建议的EDT @David Young上构建您的GUI。

  2. 使用静态常量以避免重复自己。

  3. 请勿使用空格格式化标签;使用JLabel对齐常量。

  4. 使用嵌套布局代替空布局,以获得所需的结果。

  5. 为避免在构造函数中调用公共方法,您复制了startMusic()的代码。相反,在构造函数完成后调用

  6. 以下是一个例子:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.border.EtchedBorder;
    import sun.audio.*;
    
    public class Menu {
    
        private static final String TITLE = "Battleship";
        private static final String SOUND_FILE = "./battle.wav";
        private javax.swing.JLabel image;
        JFrame frame = new JFrame(TITLE);
        JPanel center = new JPanel(new BorderLayout());
        JLabel statusbar = new JLabel(TITLE);
        JLabel battleship = new JLabel(TITLE, JLabel.CENTER);
        static AudioPlayer MGP = AudioPlayer.player;
        static AudioStream BGM;
        static ContinuousAudioDataStream loop = null;
        AudioData MD;
    
        public Menu() {
    
            frame.setTitle("Battleship");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            statusbar.setBorder(BorderFactory.createEtchedBorder(
                EtchedBorder.RAISED));
            battleship.setBorder(BorderFactory.createEtchedBorder(
                EtchedBorder.RAISED));
    
            JButton start = new JButton("Start");
            JButton exit = new JButton("Exit");
            JButton StopMusic = new JButton("Stop Music");
            JButton StartMusic = new JButton("Start Music");
    
            battleship.setFont(new Font("Courier New", Font.ITALIC, 36));
            battleship.setForeground(Color.BLACK);
    
            image = new JLabel();
            image.setHorizontalAlignment(JLabel.CENTER);
            image.setIcon(new ImageIcon("./battleship2.jpg"));
            center.add(image, BorderLayout.CENTER);
    
            JPanel panel = new JPanel();
            panel.add(start);
            panel.add(exit);
            panel.add(StopMusic);
            panel.add(StartMusic);
            center.add(panel, BorderLayout.SOUTH);
    
            frame.add(battleship, BorderLayout.NORTH);
            frame.add(center, BorderLayout.CENTER);
            frame.add(statusbar, BorderLayout.SOUTH);
    
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
    
            start.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    //frame.dispose();
                    stopMusic();
                    runPlayer();
                }
            });
            StopMusic.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    stopMusic();
                }
            });
            StartMusic.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    startMusic();
                }
            });
    
            exit.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
    
                    System.out.println("Ending Game");
                    System.exit(0);
                }
            });
        }
    
        public void stopMusic() {
            if (BGM != null) {
                AudioPlayer.player.stop(BGM);
            }
            if (loop != null) {
                AudioPlayer.player.stop(loop);
            }
        }
    
        public void startMusic() {
            try {
                InputStream test = new FileInputStream(SOUND_FILE);
                BGM = new AudioStream(test);
                AudioPlayer.player.start(BGM);
                MGP.start(loop);
            } catch (FileNotFoundException e) {
                System.out.print(e.toString());
            } catch (IOException error) {
                System.out.print(error.toString());
            }
        }
    
        public static void runPlayer() {
            Player client = new Player();
            client.createBoard();
            client.run();
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    Menu menu = new Menu();
                    menu.startMusic();
                }
            });
        }
    
        // stub for missing class
        private static class Player {
            void createBoard() {}
            void run() {}
        }
    }