JFrame组件加载缓慢

时间:2018-10-23 16:04:59

标签: java performance swing jframe jpanel

我正在为学校的作业构建键盘,我注意到了一个从未遇到过的问题。我的JFrame立即打开,但是其中的JPanels(主要是内容窗格)在加载后缓慢加载。这给项目带来一种未完成的感觉,我正在寻找解决方案。它们最终确实会加载,但是我正在寻找一种使框架和面板同时打开的方法。

public class main {

    public static JFrame frame;
    public static JPanel panel, option, keys;
    public static JButton A, B, C, D, E, F, G, A3, C3, D3, F3, G3;
    public static AudioInputStream audioInputStream;
    public static Clip clip;

    public static void start() {
        frame = new JFrame("PIANO");
        frame.setPreferredSize(new Dimension(700, 500));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        panel = new JPanel();
        panel.setOpaque(true);
        panel.setLayout(null);
        frame.setContentPane(panel);

        option = new JPanel();
        option.setOpaque(true);
        option.setSize(new Dimension(700, 100));
        option.setLocation(0, 0);
        option.setBackground(Color.GRAY);
        panel.add(option);

        keys = new JPanel();
        keys.setOpaque(true);
        keys.setSize(new Dimension(700, 400));
        keys.setLocation(0, 100);
        keys.setLayout(null);
        panel.add(keys);

        C3 = new JButton();
        C3.setSize(new Dimension(60, 190));
        C3.setLocation(70, 0);
        C3.setBackground(Color.BLACK);
        C3.setOpaque(true);
        C3.addActionListener((ActionEvent ae) -> {

            try {
                audioInputStream = AudioSystem.getAudioInputStream(new File("C_s.wav").getAbsoluteFile());

                // create clip reference
                clip = AudioSystem.getClip();

                // open audioInputStream to the clip
                clip.open(audioInputStream);

                clip.start();
            } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
                Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
            }

        });
        keys.add(C3);

        D3 = new JButton();
        D3.setSize(new Dimension(60, 190));
        D3.setLocation(170, 0);
        D3.setBackground(Color.BLACK);
        D3.setOpaque(true);
        D3.addActionListener((ActionEvent ae) -> {

            try {
                audioInputStream = AudioSystem.getAudioInputStream(new File("D_s.wav").getAbsoluteFile());

                // create clip reference
                clip = AudioSystem.getClip();

                // open audioInputStream to the clip
                clip.open(audioInputStream);

                clip.start();
            } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
                Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
            }

        });
        keys.add(D3);

        frame.pack();
        frame.setVisible(true);

    }
}

2 个答案:

答案 0 :(得分:-1)

我已经找到了解决该问题的方法,尽管我仍然不确定是什么原因导致它执行缓慢。一旦将组件添加到另一个JPanel选项,我的程序就会完全按照我想要的方式运行。它并没有像现在那样停滞不前。

public class main {

public static JFrame frame;
public static JPanel panel, option, keys;
public static JButton A, B, C, D, E, F, G, A3, C3, D3, F3, G3;
public static AudioInputStream audioInputStream;
public static Clip clip;
public static boolean octave1, octave2;

public static void start() {
    frame = new JFrame("PIANO");
    frame.setPreferredSize(new Dimension(700, 500));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);

    panel = new JPanel();
    panel.setOpaque(true);
    panel.setLayout(null);
    frame.setContentPane(panel);

    option = new JPanel();
    option.setOpaque(true);
    option.setSize(new Dimension(700, 100));
    option.setLocation(0, 0);
    option.setBackground(Color.GRAY);
    panel.add(option);

    //ADDED NEW

    oct1 = new JRadioButton("Low Octave");
    oct1.setSelected(true);
    oct1.addActionListener((ActionEvent ae) -> {

        oct2.setSelected(false);
        octave1 = true;
        octave2 = false;

    });
    option.add(oct1);

    oct2 = new JRadioButton("High Octave");
    oct2.addActionListener((ActionEvent ae) -> {

        oct1.setSelected(false);
        octave1 = false;
        octave2 = true;

    });

    //END OF NEW

    keys = new JPanel();
    keys.setOpaque(true);
    keys.setSize(new Dimension(700, 400));
    keys.setLocation(0, 100);
    keys.setLayout(null);
    panel.add(keys);

    C3 = new JButton();
    C3.setSize(new Dimension(60, 190));
    C3.setLocation(70, 0);
    C3.setBackground(Color.BLACK);
    C3.setOpaque(true);
    C3.addActionListener((ActionEvent ae) -> {

        try {
            audioInputStream = AudioSystem.getAudioInputStream(new File("C_s.wav").getAbsoluteFile());

            // create clip reference
            clip = AudioSystem.getClip();

            // open audioInputStream to the clip
            clip.open(audioInputStream);

            clip.start();
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        }

    });
    keys.add(C3);

    D3 = new JButton();
    D3.setSize(new Dimension(60, 190));
    D3.setLocation(170, 0);
    D3.setBackground(Color.BLACK);
    D3.setOpaque(true);
    D3.addActionListener((ActionEvent ae) -> {

        try {
            audioInputStream = AudioSystem.getAudioInputStream(new File("D_s.wav").getAbsoluteFile());

            // create clip reference
            clip = AudioSystem.getClip();

            // open audioInputStream to the clip
            clip.open(audioInputStream);

            clip.start();
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        }

    });
    keys.add(D3);

    frame.pack();
    frame.setVisible(true);

}
}

答案 1 :(得分:-2)

您应该始终将线程用于繁重的任务,例如加载文件或从数据库中读取,以便它们可以在后台运行而不影响GUI。我希望这可以为您提供帮助:

new Runnable() {
        @Override
        public void run() {
            C3.addActionListener((ActionEvent ae) -> {

                try {
                    audioInputStream = AudioSystem.getAudioInputStream(new File("C_s.wav").getAbsoluteFile());

                    // create clip reference
                    clip = AudioSystem.getClip();

                    // open audioInputStream to the clip
                    clip.open(audioInputStream);

                    clip.start();
                } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
                    ex.printStackTrace();
                    //Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
                }

            });
        }

    }.run();

//You can leave your listeners on the same lines as your the example

new Runnable() {
        @Override
        public void run() {
            D3.addActionListener((ActionEvent ae) -> {

                try {
                    audioInputStream = AudioSystem.getAudioInputStream(new File("D_s.wav").getAbsoluteFile());

                    // create clip reference
                    clip = AudioSystem.getClip();

                    // open audioInputStream to the clip
                    clip.open(audioInputStream);

                    clip.start();
                } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
                    ex.printStackTrace();
                    //Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
                }

            });
        }

    }.run();