我有以下代码:
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 的一部分超出内部框架的边界时,该部分不处理鼠标事件(您不能滚动该部分)。
有任何想法吗?