Java JFrame根本不显示

Java JFrame根本不显示,java,macos,swing,jframe,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 cl

我很清楚这个问题已经出现了很多次,我已经搜索过了,但我找不到解决办法,我正在尝试制作一个
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);


    }
}
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();
    }
}
声音类(如果需要):

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);


    }
}
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();
    }
}

如果有人能帮我解决这个问题,我将非常高兴。

你的代码无法编译<代码>声音应该是
最终的
,因为您在匿名
ActionListener
类中引用了它

final Sound s =

修正了这一点,框架应该显示出来。如果仍然没有,那么可能是
newsound()
引发了异常。确保您使用的是一个可以看到控制台输出的开发环境。

您的代码不会编译<代码>声音应该是
最终的
,因为您在匿名
ActionListener
类中引用了它

final Sound s =

修正了这一点,框架应该显示出来。如果仍然没有,那么可能是
newsound()
引发了异常。确保您使用的是一个可以看到控制台输出的开发环境。

您的代码不会编译<代码>声音应该是
最终的
,因为您在匿名
ActionListener
类中引用了它

final Sound s =

修正了这一点,框架应该显示出来。如果仍然没有,那么可能是
newsound()
引发了异常。确保您使用的是一个可以看到控制台输出的开发环境。

您的代码不会编译<代码>声音应该是
最终的
,因为您在匿名
ActionListener
类中引用了它

final Sound s =

修正了这一点,框架应该显示出来。如果仍然没有,那么可能是
newsound()
引发了异常。请确保您使用的是一个可以看到控制台输出的开发环境。

在反复操作之后,这似乎是一种情况,即不在事件调度线程的上下文中构建UI会导致某种死锁

基本上,我用
main
代码块包装在
EventQueue中

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);
        }
    });

}
您应该始终在事件调度线程的上下文中启动UI,有关更多详细信息,请参阅


接下来,该
线程.sleep
将导致一些问题,有关更多详细信息,请参阅在反复操作之后,这似乎是其中一种情况,即不在事件调度线程的上下文中构建UI将导致某种死锁

基本上,我用
main
代码块包装在
EventQueue中

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);
        }
    });

}
您应该始终在事件调度线程的上下文中启动UI,有关更多详细信息,请参阅


接下来,该
线程.sleep
将导致一些问题,有关更多详细信息,请参阅在反复操作之后,这似乎是其中一种情况,即不在事件调度线程的上下文中构建UI将导致某种死锁

基本上,我用
main
代码块包装在
EventQueue中

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);
        }
    });

}
您应该始终在事件调度线程的上下文中启动UI,有关更多详细信息,请参阅


接下来,该
线程.sleep
将导致一些问题,有关更多详细信息,请参阅在反复操作之后,这似乎是其中一种情况,即不在事件调度线程的上下文中构建UI将导致某种死锁

基本上,我用
main
代码块包装在
EventQueue中

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);
        }
    });

}
您应该始终在事件调度线程的上下文中启动UI,有关更多详细信息,请参阅


接下来,该
Thread.sleep
将导致一些问题,有关更多详细信息,请参见This
Thread.sleep(4000)是一个糟糕的开始。查看更多信息details@MadProgrammer即使在我移除线程之后。sleep(显然包括try&catch)并离开s.play();如果没有
声音
类,JFrame仍然无法显示自己,它对我来说运行得很好。我在用Java 8,你在用什么…?@MadProgrammer我也在用Java 8,也许我需要重新启动Eclipse。。。我将查看答案,重新启动它不会做任何事情。Thread.sleep(4000)是一个糟糕的开始。查看更多信息details@MadProgrammer即使在我移除线程之后。sleep(显然包括try&catch)并离开s.play();如果没有
声音
类,JFrame仍然无法显示自己,它对我来说运行得很好。我在用Java 8,你在用什么…?@MadProgrammer我也在用Java 8,也许我需要重新启动Eclipse。。。我将查看答案,重新启动它不会做任何事情。Thread.sleep(4000)是一个糟糕的开始。查看更多信息details@MadProgrammer即使在我移除线程之后。sleep(显然包括try&catch)并离开s.play();如果没有
声音
类,JFrame仍然无法显示自己,它对我来说运行得很好。我在用Java 8,你在用什么…?@MadProgrammer我也在用Java 8,也许我需要重新启动Eclipse。。。我将查看答案,重新启动它不会做任何事情。Thread.sleep(4000)是一个糟糕的开始。查看更多信息details@MadProgrammer即使在我移除线程之后。sleep(显然包括try&catch)并离开s.play();如果没有
声音
类,JFrame仍然无法显示自己,它对我来说运行得很好。我在用Java 8,你在用什么…?@MadProgrammer我也在用Java 8,也许我需要重新启动Eclipse。。。我会看看答案,它不会