[IT科技] 对话框(Dialog)的模式(Modal)测试发表于 @ 2009-6-8 18:00:26
package code.jdk.dialogmodality;
import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class DualModal {
public static void main(String args[]) {
final JFrame frame1 = new JFrame("Left");
final JFrame frame2 = new JFrame("Right");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button1 = new JButton("Left");
JButton button2 = new JButton("Right");
frame1.add(button1);
frame2.add(button2);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
JOptionPane pane = new JOptionPane("New label", JOptionPane.QUESTION_MESSAGE);
pane.setWantsInput(true);
JDialog dialog = pane.createDialog(frame2, "Enter Text");
// dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
dialog.setVisible(true);
String text = (String) pane.getInputValue();
if (!JOptionPane.UNINITIALIZED_VALUE.equals(text) && text.trim().length() > 0) {
source.setText(text);
}
}
};
button1.addActionListener(listener);
button2.addActionListener(listener);
frame1.setBounds(100, 100, 200, 200);
frame1.setVisible(true);
frame2.setBounds(400, 100, 200, 200);
frame2.setVisible(true);
}
}运行结果,弹出的对话框将不允许另外一个button获得焦点。
针对Dialog.ModalityType 的API 说明 public static enum Dialog.ModalityTypeextends Enum<Dialog.ModalityType>模式对话框阻塞对某些顶层窗口的所有输入。是否阻塞某一特定窗口取决于对话框的模式类型;这被称为“阻塞范围”。ModalityType 枚举指定模式类型及其相关范围。 从以下版本开始: 1.6 另请参见: Dialog.getModalityType(), Dialog.setModalityType(java.awt.Dialog.ModalityType), Toolkit.isModalityTypeSupported(java.awt.Dialog.ModalityType)
关联内容 |
|||||||