문제

I want to create a game and when I move the mouse to a button I want to play back a sound. This is the JFrame class:

package jatek;

    public class jatek extends javax.swing.JFrame {

        public jatek() {
            initComponents();

        }

        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {

            UjJatek = new javax.swing.JButton();
            Kilepes = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("AKASZTOFA");
            setBounds(new java.awt.Rectangle(500, 250, 0, 0));
            setFocusable(false);
            setMaximumSize(new java.awt.Dimension(800, 560));
            setMinimumSize(new java.awt.Dimension(800, 550));
            setPreferredSize(new java.awt.Dimension(800, 545));
            setResizable(false);
            getContentPane().setLayout(null);

            UjJatek.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jatek/szoveg.png"))); // NOI18N
            UjJatek.setBorderPainted(false);
            UjJatek.setContentAreaFilled(false);
            UjJatek.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    UjJatekMouseClicked(evt);
                }
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    UjJatekMouseEntered(evt);
                }
                public void mouseExited(java.awt.event.MouseEvent evt) {
                    UjJatekMouseExited(evt);
                }
            });
            getContentPane().add(UjJatek);
            UjJatek.setBounds(470, 160, 180, 50);

            Kilepes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jatek/kilepes1.png"))); // NOI18N
            Kilepes.setBorderPainted(false);
            Kilepes.setContentAreaFilled(false);
            Kilepes.setFocusable(false);
            Kilepes.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    KilepesMouseClicked(evt);
                }
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    KilepesMouseEntered(evt);
                }
                public void mouseExited(java.awt.event.MouseEvent evt) {
                    KilepesMouseExited(evt);
                }
            });
            getContentPane().add(Kilepes);
            Kilepes.setBounds(480, 340, 170, 50);

            jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jatek/hatter.png"))); // NOI18N
            getContentPane().add(jLabel1);
            jLabel1.setBounds(0, 0, 800, 533);

            pack();
        }// </editor-fold>                        

        private void UjJatekMouseEntered(java.awt.event.MouseEvent evt) {                                     
            // TODO add your handling code here:
            //Sound hang = new Sound("button.wav");
            //hang.playSoundOnce();
            UjJatek.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jatek/szoveg2.png")));
        }                                    

        private void UjJatekMouseExited(java.awt.event.MouseEvent evt) {                                    
            // TODO add your handling code here:
            UjJatek.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jatek/szoveg.png")));
        }                                   

        private void KilepesMouseEntered(java.awt.event.MouseEvent evt) {                                     
            // TODO add your handling code here:
            Kilepes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jatek/kilepes2.png")));
        }                                    

        private void KilepesMouseExited(java.awt.event.MouseEvent evt) {                                    
            // TODO add your handling code here:
            Kilepes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jatek/kilepes1.png")));
        }                                   

        private void KilepesMouseClicked(java.awt.event.MouseEvent evt) {                                     
            // TODO add your handling code here:
            System.exit(0);
        }                                    

        private void UjJatekMouseClicked(java.awt.event.MouseEvent evt) {                                     
            // TODO add your handling code here:
            this.setVisible(false);
            jatek2 j = new jatek2();
            j.setBounds(this.getBounds());
            j.setMinimumSize(this.getMinimumSize());
            j.setVisible(true);

        }                                    

        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(jatek.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(jatek.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(jatek.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(jatek.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>

            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new jatek().setVisible(true);
                }
            });
        }

        // Variables declaration - do not modify                     
        private javax.swing.JButton Kilepes;
        private javax.swing.JButton UjJatek;
        private javax.swing.JLabel jLabel1;
        // End of variables declaration                   
    }

and here is the Sound class:

package jatek;

import java.applet.*;
import javax.swing.*;
import java.io.*;
import java.net.*;


public class Sound extends JApplet// Holds one audio file
    {

        private AudioClip song; // Sound player
        private URL songPath; // Sound path

        public Sound(String filename) {
            try {
                songPath = new URL(getCodeBase(), filename); // Get the Sound URL
                song = Applet.newAudioClip(songPath); // Load the Sound
            } catch (Exception e) {
            } // Satisfy the catch
        }

        public void playSound() {
            song.loop(); // Play 
        }

        public void stopSound() {
            song.stop(); // Stop
        }

        public void playSoundOnce() {
            song.play(); // Play only once
        }
    }

Can anyone help me...or can anyone to give a better way to how can I play sound when I move the mouse on button. Thanks

도움이 되었습니까?

해결책

Since the applet class is run as an application, getCodeBase() will throw a NPE, silently in this case as you haven't displayed the exception trace information. There's no need to use an applet class in a Swing application. You can read the clip directly from the InputStream

AudioInputStream stream = 
   AudioSystem.getAudioInputStream(getClass().getResource("/sounds/button.wav"));
Clip clip = AudioSystem.getClip();
clip.open(stream);
clip.start();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top