简体   繁体   中英

JFrame Components Loading Slowly

I'm building a keyboard for an assignment at school, and I've noticed an issue I've never had before. My JFrame is opening right away, but the JPanels within it, mainly the content pane, are loading in slowly after the fact. This gives an unfinished feel to the project, and I'm looking for a fix. They do load eventually, but I'm looking for a way to make the frame and the panels open at the same time.

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

    }
}

I've found a solution to the issue, though I'm still not quite sure as to what caused it to execute slowly. As soon as I added a component to the other JPanel, option, my program ran exactly as I wanted it to. It didn't stall out like it was.

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

}
}

You should always use Threads for heavy tasks like loading files or read from a database so they can run on the background without affecting your GUI. I hope this can help you:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM