java - JInternalFrame 作为模态

标签 java swing modal-dialog jdialog jinternalframe

我有以下代码:

import java.awt.AWTEvent;
import java.awt.ActiveEvent;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.MenuComponent;
import java.awt.event.MouseEvent;
import javax.swing.JInternalFrame;
import javax.swing.SwingUtilities;

public class modalInternalFrame extends JInternalFrame {

// indica si aquest es modal o no.
    boolean modal = false;

    @Override
    public void show() {
        super.show();
        if (this.modal) {
            startModal();
        }
    }

    @Override
    public void setVisible(boolean value) {
        super.setVisible(value);
        if (modal) {
            if (value) {
                startModal();
            } else {
                stopModal();
            }
        }
    }

    private synchronized void startModal() {

        try {
            if (SwingUtilities.isEventDispatchThread()) {
                EventQueue theQueue =
                        getToolkit().getSystemEventQueue();
                while (isVisible()) {
                    AWTEvent event = theQueue.getNextEvent();
                    Object source = event.getSource();
                    boolean dispatch = true;

                    if (event instanceof MouseEvent) {
                        MouseEvent e = (MouseEvent) event;
                        MouseEvent m =
                                SwingUtilities.convertMouseEvent((Component) e.getSource(), e, this);
                        if (!this.contains(m.getPoint()) && e.getID() != MouseEvent.MOUSE_DRAGGED) {
                            dispatch = false;
                        }
                    }

                    if (dispatch) {
                        if (event instanceof ActiveEvent) {
                            ((ActiveEvent) event).dispatch();
                        } else if (source instanceof Component) {
                            ((Component) source).dispatchEvent(
                                    event);
                        } else if (source instanceof MenuComponent) {
                            ((MenuComponent) source).dispatchEvent(
                                    event);
                        } else {
                            System.err.println(
                                    "Unable to dispatch: " + event);
                        }
                    }
                }
            } else {
                while (isVisible()) {
                    wait();
                }
            }
        } catch (InterruptedException ignored) {
        }

    }

    private synchronized void stopModal() {
        notifyAll();
    }

    public void setModal(boolean modal) {
        this.modal = modal;
    }

    public boolean isModal() {
        return this.modal;
    }
}

然后我使用 NetBeans GUI 绘制 JInternalFrame,但只是更改了类声明中的代码以扩展 modalInternalFrame 而不是 JInternalFrame:

public class myDialog extends modalInternalFrame { 
....

然后使用它从我的顶级“桌面”JFrame(包含 jDesktopPane1)实际显示它:

myDialog d = new myDialog();
d.setModal(true);
d.setBounds(160, 180, 550, 450);
jDesktopPane1.add(d);
d.setVisible(true);

我的问题是:如果内部框架有 JComboBox 或 PopupMenu,当 PopupMenu 的一部分超出内部框架的边界时,该部分不会处理鼠标事件(您无法滚动该部分)。

有什么想法吗?

最佳答案

如何使用JOptionPane.showInternalMessageDialog(...):

  • 我在 Windows 7 x64 上运行 JDK 1.7.0_21:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class ModalInternalFrameTest {
  private final JDesktopPane desktop = new JDesktopPane();
  private final String[] items = new String[] {
    "bananas", "pizza", "hot dogs", "ravioli"
  };
  private final Action openAction = new AbstractAction("open") {
    @Override public void actionPerformed(ActionEvent e) {
      JComboBox<String> combo = new JComboBox<String>(items);
      combo.setEditable(true);
      JOptionPane.showInternalMessageDialog(desktop, combo);
      System.out.println(combo.getSelectedItem());
    }
  };
  public JComponent makeUI(JFrame frame) {
    frame.setJMenuBar(createMenuBar());

    JButton button = new JButton(openAction);
    button.setMnemonic(KeyEvent.VK_S);
    JInternalFrame internal = new JInternalFrame("Button");
    internal.getContentPane().add(button);
    internal.setBounds(20, 20, 100, 100);
    desktop.add(internal);
    internal.setVisible(true);

    JButton b = new JButton(new AbstractAction("beep") {
      @Override public void actionPerformed(ActionEvent e) {
        Toolkit.getDefaultToolkit().beep();
      }
    });
    b.setMnemonic(KeyEvent.VK_B);

    JPanel p = new JPanel(new BorderLayout());
    p.add(b, BorderLayout.SOUTH);
    p.add(desktop);
    return p;
  }
  private JMenuBar createMenuBar() {
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Frame");
    menu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(menu);

    JMenuItem menuItem = new JMenuItem(openAction);
    menuItem.setMnemonic(KeyEvent.VK_1);
    menuItem.setAccelerator(
      KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
    menu.add(menuItem);

    return menuBar;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new ModalInternalFrameTest().makeUI(f));
    f.setSize(640, 480);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

关于java - JInternalFrame 作为模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16422939/

相关文章:

java - 运行 Servlet 时收到错误消息(第 1 行 : Incorrect syntax near 'password' .)

java - 布局JPopupMenu : How to provide different size for menu items in same menu

jquery - 如何使用浏览器后退按钮关闭 Bootstrap 模式而不是返回页面?

batch-file - 使用自定义 JRE 运行 java

java.sql.SQLException : Data truncated for column 'MonthlyIncome' at row 1 error 异常

java - 消除面板上复选框之间的巨大差距

jquery - Twitter Bootstrap : How to clear out a modal

jquery - 如何打开一个与 DIV 中所有元素的宽度完全相同的 JQuery 模式对话框?

java - 如何在一个pom.xml中安装其他Maven包?

Java "Asteroids"喜欢游戏 - 放慢船速