你可以继承JDialog类,它里面有一个方法,就是setModal(boolean);
只要this.setModal(true);就可以将窗口设置为模式窗口,
这样只有等该模式窗体得到响应才会执行后续代码。JFrame中没有这种设置为模式窗体的方法的。

解决方案 »

  1.   

    楼上:
    那JColorChooser的那种功能是怎么实现的呢?听我们老师说在c++里可以通过一个sendmessage函数来实现,java没有类似的或是替代的方法吗?
      

  2.   

    其实可以用多线程之间通信解决,楼上说的在JAVA中也可以实现的,建议看看JAVA线程编程这本书,个人比较不错,如果楼主学过操作系统应该就可以明白了
      

  3.   

    可以利用事件队列来解决,具体的楼主可以参看JDK源码,
    JDK路径下的src.zip里,java\awt\Dialog.java
    具体只要看里面的show()方法,里面有详细注释,楼主可以自己研究一下。    public void show() {
            beforeFirstShow = false;
            if (!isModal()) {
                conditionalShow();
            } else {
                // Set this variable before calling conditionalShow(). That
                // way, if the Dialog is hidden right after being shown, we
                // won't mistakenly block this thread.
                keepBlocking = true;            // Store the app context on which this dialog is being shown.
                // Event dispatch thread of this app context will be sleeping until
                // we wake it by any event from hideAndDisposeHandler().
                showAppContext = AppContext.getAppContext();            if (conditionalShow()) {
                    // We have two mechanisms for blocking: 1. If we're on the
                    // EventDispatchThread, start a new event pump. 2. If we're
                    // on any other thread, call wait() on the treelock.                // keep the KeyEvents from being dispatched
                    // until the focus has been transfered
                    long time = Toolkit.getEventQueue().getMostRecentEventTime();
                    Component predictedFocusOwner = getMostRecentFocusOwner();
                    KeyboardFocusManager.getCurrentKeyboardFocusManager().
                         enqueueKeyEvents(time, predictedFocusOwner);                 Runnable pumpEventsForHierarchy = new Runnable() {
                            public void run() {
                                EventDispatchThread dispatchThread =
                                    (EventDispatchThread)Thread.currentThread();
                                dispatchThread.pumpEventsForHierarchy(new Conditional() {
                                        public boolean evaluate() {
                                            return keepBlocking && windowClosingException == null;
                                        }
                                    }, Dialog.this);
                            }
                        };                if (EventQueue.isDispatchThread()) {
                        /*
                         * dispose SequencedEvent we are dispatching on current
                         * AppContext, to prevent us from hang.
                         *
                         * BugId 4531693 ([email protected])
                         */
                        SequencedEvent currentSequencedEvent = KeyboardFocusManager.
                            getCurrentKeyboardFocusManager().getCurrentSequencedEvent();
                        if (currentSequencedEvent != null) {
                            currentSequencedEvent.dispose();
                        }                    pumpEventsForHierarchy.run();
                    } else {
                        synchronized (getTreeLock()) {
                            Toolkit.getEventQueue().
                                postEvent(new PeerEvent(this,
                                                        pumpEventsForHierarchy,
                                                        PeerEvent.PRIORITY_EVENT));
                            while (keepBlocking && windowClosingException == null) {
                                try {
                                    getTreeLock().wait();
                                } catch (InterruptedException e) {
                                    break;
                                }
                            }
                        }
                    }
                    KeyboardFocusManager.getCurrentKeyboardFocusManager().
                        dequeueKeyEvents(time, predictedFocusOwner);
                    if (windowClosingException != null) {
                        windowClosingException.fillInStackTrace();
                        throw windowClosingException;
                    }
                }
            }
        }