【问题标题】:JFrame behaving weirdlyJFrame 行为怪异
【发布时间】:2012-12-03 18:52:37
【问题描述】:

在我的程序中,我在背景中显示带有歌曲的图像。 歌曲完成后,我需要关闭框架和JInternalFrame。 代码如下:

package projectfinal;
import javax.swing.*;
import java.io.File;
import java.io.IOException;

/* MORE IMPORTS */
public class ImagePanel extends javax.swing.JFrame {

    public static class audio extends JApplet {

        private static final int EXTERNAL_BUFFER_SIZE = 128000;

        public void aaudio() {

            String strFilename = "E:/zehreelay.wav";
            File soundFile = new File(strFilename);


            AudioInputStream audioInputStream = null;
            try {
                audioInputStream = AudioSystem.getAudioInputStream(soundFile);
            } catch (Exception e) {

                e.printStackTrace();
                System.exit(1);
            }


            AudioFormat audioFormat = audioInputStream.getFormat();

            SourceDataLine line = null;
            DataLine.Info info = new DataLine.Info(SourceDataLine.class,
                audioFormat);
            try {
                line = (SourceDataLine) AudioSystem.getLine(info);

                line.open(audioFormat);
            } catch (LineUnavailableException e) {
                e.printStackTrace();
                System.exit(1);
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
            line.start();
            int nBytesRead = 0;
            byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
            while (nBytesRead != -1) {
                try {
                    nBytesRead = audioInputStream.read(abData, 0, abData.length);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (nBytesRead >= 0) {
                    int nBytesWritten = line.write(abData, 0, nBytesRead);
                }

            }
            line.drain();

            line.close();

            jInternalFrame1.setVisible(false);
            /**
            * closing internal frame
            */
        }
    }

    /**
    * Creates new form NewJFrame
    */
    public ImagePanel() {

        initComponents();

    }

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

        jInternalFrame1 = new javax.swing.JInternalFrame();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());

        //////////more code////////////////////////////////////
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1.addComponentListener(new java.awt.event.ComponentAdapter() {

            public void componentHidden(java.awt.event.ComponentEvent evt) {
                hiden(evt);
            }
        });
        /////adding a listner for component///
    }

    public static void main(String args[]) {


        start();

    }

    private void hiden(java.awt.event.ComponentEvent evt) {

        setVisible(false);
    }

    public static void start() {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ImagePanel().setVisible(true);
            }
        });
        audio obj = new audio();
        obj.aaudio();
    }
    private static javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
/////more variables...//////
}

这段代码很好,但是当我尝试没有添加组件监听器时, 也就是说,getRootPane().SetVisible(false)jInternalFrame1.getContentPane().setVisible(false)setVisible(false) 都没有工作,但只有在添加组件隐藏侦听器后才能工作。为什么?有什么原因吗?那么JInternalFrame 是如何工作的(JInternalFrame.setVisible(false))?这和rootPane()有关吗?

【问题讨论】:

  • Swing GUI 对象应event dispatch thread 上构造和操作。
  • @trashgod 那么如果我们通常说 setVisible(false) ,为什么它可以正常工作?为什么在这种情况下不可以呢?
  • 垃圾神是对的。为什么不能在 SwingUtilities.invokeLater 中包装调用?
  • @StanislavL 我不想使用 AWT 调度线程。
  • 我引用了两种方法here

标签: java swing components jframe jinternalframe


【解决方案1】:

Initial Threads 中所述,“初始线程通过调用invokeLaterinvokeAndWait 来调度GUI 创建任务。”您正在尝试在同一个初始线程上播放声音和操作 GUI。相反,在初始线程上启动声音并在 EDT 上构建 GUI,如 here 所示,或者启动一个单独的线程来播放声音,如 here 所示。

我不想使用 AWT 调度线程。

是的,您不想使用 AWT 事件调度线程来播放声音,但您必须将它用于 GUI。

这个问题有些悬而未决。声音的播放很轻,所以必须由单一的EDT处理。

我只能推测使用不同的组件会改变时间,足以影响您平台上的结果。在程序正确同步之前,总会有一些环境可以工作,也有一些环境会失败。基本规则是:不要阻塞 EDT。另请参阅此answer、此answerJava Sound 标签。

【讨论】:

  • 谢谢。但是,这个问题有些悬而未决。声音的播放是轻的,所以必须由单个EDT处理
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 2012-08-05
  • 2023-03-02
相关资源
最近更新 更多