MidiSystem.getReceiver()冻结JFrame [英] MidiSystem.getReceiver() freezes up JFrame

查看:72
本文介绍了MidiSystem.getReceiver()冻结JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个涉及播放MIDI声音的程序,而就在今天,我在调用时遇到了问题

I'm making a program that involves playing MIDI sounds, and just today I encountered a problem where calling

MidiSystem.getReceiver()

或打开MidiDevice,完全阻止了我在屏幕上显示框架.然后,如果我尝试在冻结所有内容时终止所有操作,那么Eclipse会告诉我终止失败".

or opening up a MidiDevice, totally prevents me from making a frame show up on the screen. And then if I try to terminate everything while everything is frozen up, Eclipse tells me that "terminate failed".

这里有一些示例代码向您展示我的意思:

Here's some sample code to show you what I mean:

public static void main(String args[]) {

    Receiver receiver;

    try {
        receiver = MidiSystem.getReceiver();
    } catch (MidiUnavailableException e) {
        e.printStackTrace();
    }

    JFrame frame = new JFrame("here's a frame");
    Dimension d = new Dimension(500,500);
    frame.setSize(d);
    frame.setPreferredSize(d);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

getReceiver()部分和JFrame部分各自可以正常工作;只是当我同时看到这两个部分时,这些东西才停止工作.

The getReceiver() part and the JFrame part each work fine on their own; it's just when I have both pieces there that stuff stops working.

(顺便说一句,几周前运行类似代码时,我没有这个问题...?)

(Btw I was not having this problem when running similar code a couple weeks ago...?)

任何帮助将不胜感激.谢谢!

Any help would be greatly appreciated. Thanks!

推荐答案

您的Receiver踩在Swing线程上,导致GUI无法运行.您必须在Swing事件线程或EDT( E vent D ispatch T hread)的后台线程中运行Receiver.有关更多信息,请查看Oracle教程: Swing中的并发

Your Receiver is stepping on the Swing thread preventing the GUI from running. You must run the Receiver in a thread that's background to the Swing event thread or EDT (Event Dispatch Thread). For more on this, please check out the Oracle tutorial: Concurrency in Swing

例如

import java.awt.*;
import javax.sound.midi.*;
import javax.swing.*;

public class MidiFoo {
   public static void main(String args[]) {

      new Thread(new Runnable() {
         public void run() {
            try {
               Receiver receiver = MidiSystem.getReceiver();
            } catch (MidiUnavailableException e) {
               e.printStackTrace();
            }
         }
      }).start();

      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            JFrame frame = new JFrame("here's a frame");
            Dimension d = new Dimension(500, 500);
            frame.setSize(d);
            frame.setPreferredSize(d);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         }
      });
   }
}

这篇关于MidiSystem.getReceiver()冻结JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆