JFrame根本没有显示

时间:2014-11-07 21:47:17

标签: java macos swing jframe

我非常清楚这个问题已经出现了很多,我已经搜索过,我找不到解决方法,我正试图让JFrame弹出并显示一个按钮然后播放一些音乐4秒,但程序甚至不会显示JFrame

这是主要课程:

package practs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {

    private static JButton button;

    public static void main(String[] args){
        Sound s = new Sound("/Users/seb/Documents/workspace(NormalJava)/practs/res/backgroundMusic.wav");

        JFrame f = new JFrame("Sound Meister");
        f.setFocusable(true);
        f.setSize(300,300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button = new JButton("Press to view 4 secs of music");
        f.add(button);
        button.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent event){

                        s.play();
                        try {
                            Thread.sleep(4000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        s.stop();
                    }
                }
                );
        f.setVisible(true);


    }
}

Sound类,如果需要:

package practs;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Sound {
    private Clip clip;
    public Sound(String fileName) {
        // specify the sound to play
        // (assuming the sound can be played by the audio system)
        // from a wave File
        try {
            File file = new File(fileName);
            if (file.exists()) {
                AudioInputStream sound = AudioSystem.getAudioInputStream(file);
             // load the sound into memory (a Clip)
                clip = AudioSystem.getClip();
                clip.open(sound);
            }
            else {
                throw new RuntimeException("Sound: file not found: " + fileName);
            }
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Malformed URL: " + e);
        }
        catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Unsupported Audio File: " + e);
        }
        catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Input/Output Error: " + e);
        }
        catch (LineUnavailableException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Line Unavailable Exception Error: " + e);
        }

    // play, stop, loop the sound clip
    }
    public void play(){
        clip.setFramePosition(0);  // Must always rewind!
        clip.start();
    }
    public void loop(){
        clip.loop(Clip.LOOP_CONTINUOUSLY);
    }
    public void stop(){
        clip.stop();
    }
}

如果有人能为我解决这个问题,我会很高兴。

2 个答案:

答案 0 :(得分:3)

原样,您的代码无法编译。 Sounds s应该是final,因为您在匿名ActionListener课程中引用它。

final Sound s =

修复一下,框架应显示。如果它仍然没有,则可能new Sound()正在抛出异常。确保您使用的是可以查看控制台输出的开发环境。

答案 1 :(得分:0)

经过多次调整后,这似乎是在事件调度线程的上下文中没有构建UI导致某种死锁的情况之一......

基本上,我带了main代码块,包裹在EventQueue.invokeLater中,它工作得很好......

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }

            Sound s = new Sound("/Users/swhitehead/Downloads/checked.wav");

            JFrame f = new JFrame("Sound Meister");
            f.setFocusable(true);
            f.setSize(300, 300);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            button = new JButton("Press to view 4 secs of music");
            f.add(button);
            button.addActionListener(
                    new ActionListener() {
                        public void actionPerformed(ActionEvent event) {
                            s.play();
                            try {
                                Thread.sleep(4000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            s.stop();
                        }
                    }
            );
            f.setVisible(true);
        }
    });

}

您应该始终在Event Dispatching Thread的上下文中启动UI,有关详细信息,请参阅Initial Threads ...

接下来,Thread.sleep会导致一些问题,请参阅Concurrency in Swing了解详情