[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)的模式(Modal)测试

针对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)
[size=+2]枚举常量摘要
APPLICATION_MODAL
APPLICATION_MODAL 对话框阻塞同一 Java 应用程序中的所有顶层窗口(它自己的子层次结构中的顶层窗口除外)。
DOCUMENT_MODAL
DOCUMENT_MODAL 对话框阻塞对同一文档中所有顶层窗口的输入(它自己的子层次结构中的顶层窗口除外)。
MODELESS
MODELESS 对话框不阻塞任何顶层窗口。
TOOLKIT_MODAL
TOOLKIT_MODAL 对话框阻塞从同一工具包运行所有顶层窗口(它自己的子层次结构中的顶层窗口除外)。



Copyright © 2009 老紫竹
网站地图 | 最新文章 | 未读文章 | 24小时内热点文章 |津ICP备09000085号