1

Java Swing アプリケーションと JInternalFrame があります。

JInternalFrame にはいくつかの入力フィールドがあり、ホットキーを押すと次のようになります。

  1. 現在の JInternalFrame でのすべての実行が中断され、どの入力フィールドもフォーカスを受け取ることができなくなります。
  2. 新しいフレーム (inputFrame) がある種のモーダル モードで開き、ユーザーが正しい値を入力するのを支援します。(データは EJB からフェッチされ、ユーザーの選択に従ってフィルタリングされます)
  3. ユーザーが [OK] をクリックすると、inputFrame が閉じられ、データが mainFrame に返されます。
  4. メインフレームはデータを受け取り、処理を続行します。

可能であれば、inputFrame を JInternalFrame にしたいのですが、それは主な問題ではありません。

これを達成する方法を知っている人はいますか?

4

2 に答える 2

2

内部フレームの使用方法を見て、で検索してくださいshowInternal。そこに答えがあります。

于 2013-05-16T14:51:30.667 に答える
0

JOptionPane のソース コードで答えを見つけたので、createInternalFrame と startModal の 2 つの静的メソッドをコピーし (わずかな変更を加えて)、以下の独自のクラスを作成しました。

public class ModalInternalPanel {

    public static  Object showInternalDialog(Component parentComponent, Container container, HasReturnValue hasReturnValue, String title)  {
        JInternalFrame frame = createInternalFrame(parentComponent, title, container);
        startModal(frame);
        if (hasReturnValue!=null) return hasReturnValue.getReturnValue(); else return null;
    }


    public static JInternalFrame createInternalFrame(Component parentComponent, String title, Container container) throws RuntimeException  {
        // Try to find a JDesktopPane.
        JLayeredPane toUse = JOptionPane.getDesktopPaneForComponent(parentComponent);
        // If we don't have a JDesktopPane, we try to find a JLayeredPane.
        if (toUse == null)  toUse = JLayeredPane.getLayeredPaneAbove(parentComponent);
        // If this still fails, we throw a RuntimeException.
        if (toUse == null) throw new RuntimeException   ("parentComponent does not have a valid parent");

        JInternalFrame frame = new JInternalFrame(title); 


        frame.setContentPane(container);
        frame.setClosable(true);

        toUse.add(frame);
        frame.setLayer(JLayeredPane.MODAL_LAYER);

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

        return frame;
    }



    private static void startModal(JInternalFrame f) {
        // We need to add an additional glasspane-like component directly
        // below the frame, which intercepts all mouse events that are not
        // directed at the frame itself.
        JPanel modalInterceptor = new JPanel();
        modalInterceptor.setOpaque(false);
        JLayeredPane lp = JLayeredPane.getLayeredPaneAbove(f);
        lp.setLayer(modalInterceptor, JLayeredPane.MODAL_LAYER.intValue());
        modalInterceptor.setBounds(0, 0, lp.getWidth(), lp.getHeight());
        modalInterceptor.addMouseListener(new MouseAdapter(){});
        modalInterceptor.addMouseMotionListener(new MouseMotionAdapter(){});
        lp.add(modalInterceptor);
        f.toFront();

        // We need to explicitly dispatch events when we are blocking the event
        // dispatch thread.
        EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
        try {
            while (! f.isClosed())       {
                if (EventQueue.isDispatchThread())    {
                    // The getNextEventMethod() issues wait() when no
                    // event is available, so we don't need do explicitly wait().
                    AWTEvent ev = queue.getNextEvent();
                    // This mimics EventQueue.dispatchEvent(). We can't use
                    // EventQueue.dispatchEvent() directly, because it is
                    // protected, unfortunately.
                    if (ev instanceof ActiveEvent)  ((ActiveEvent) ev).dispatch();
                    else if (ev.getSource() instanceof Component)  ((Component) ev.getSource()).dispatchEvent(ev);
                    else if (ev.getSource() instanceof MenuComponent)  ((MenuComponent) ev.getSource()).dispatchEvent(ev);
                    // Other events are ignored as per spec in
                    // EventQueue.dispatchEvent
                } else  {
                    // Give other threads a chance to become active.
                    Thread.yield();
                }
            }
        }
        catch (InterruptedException ex) {
            // If we get interrupted, then leave the modal state.
        }
        finally {
            // Clean up the modal interceptor.
            lp.remove(modalInterceptor);

            // Remove the internal frame from its parent, so it is no longer
            // lurking around and clogging memory.
            Container parent = f.getParent();
            if (parent != null) parent.remove(f);
        }
    }
}
于 2013-05-18T08:11:42.337 に答える