• <tfoot id='4hFCm'></tfoot>

      <small id='4hFCm'></small><noframes id='4hFCm'>

    1. <i id='4hFCm'><tr id='4hFCm'><dt id='4hFCm'><q id='4hFCm'><span id='4hFCm'><b id='4hFCm'><form id='4hFCm'><ins id='4hFCm'></ins><ul id='4hFCm'></ul><sub id='4hFCm'></sub></form><legend id='4hFCm'></legend><bdo id='4hFCm'><pre id='4hFCm'><center id='4hFCm'></center></pre></bdo></b><th id='4hFCm'></th></span></q></dt></tr></i><div id='4hFCm'><tfoot id='4hFCm'></tfoot><dl id='4hFCm'><fieldset id='4hFCm'></fieldset></dl></div>
        <bdo id='4hFCm'></bdo><ul id='4hFCm'></ul>

        <legend id='4hFCm'><style id='4hFCm'><dir id='4hFCm'><q id='4hFCm'></q></dir></style></legend>

        如何将 JFrame 设置为 JDialog 的父级

        How to set the JFrame as a parent to the JDialog(如何将 JFrame 设置为 JDialog 的父级)

          <tbody id='giq9k'></tbody>
          • <bdo id='giq9k'></bdo><ul id='giq9k'></ul>
            1. <legend id='giq9k'><style id='giq9k'><dir id='giq9k'><q id='giq9k'></q></dir></style></legend>
              • <i id='giq9k'><tr id='giq9k'><dt id='giq9k'><q id='giq9k'><span id='giq9k'><b id='giq9k'><form id='giq9k'><ins id='giq9k'></ins><ul id='giq9k'></ul><sub id='giq9k'></sub></form><legend id='giq9k'></legend><bdo id='giq9k'><pre id='giq9k'><center id='giq9k'></center></pre></bdo></b><th id='giq9k'></th></span></q></dt></tr></i><div id='giq9k'><tfoot id='giq9k'></tfoot><dl id='giq9k'><fieldset id='giq9k'></fieldset></dl></div>

                  <small id='giq9k'></small><noframes id='giq9k'>

                1. <tfoot id='giq9k'></tfoot>
                  本文介绍了如何将 JFrame 设置为 JDialog 的父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我无法将框架设置为对话框的所有者.通常,当我扩展 JDialog 类来创建对话框时,我会使用 super(frame) 来指定对话框的所有者,这样当您按下 <代码>alt+tab.但是,当我使用 new 之类的 JDialog dialog = new JDialog() 创建对话框时,我无法将框架指定为对话框的所有者.

                  I am having trouble to set the frame as a owner to the dialog. Normally when I extend JDialog class for creating a dialog then I use super(frame) to specify the owner of the dialog such that both of them are not disjoint when you press alt+tab. But when I create a dialog using new like JDialog dialog = new JDialog() then I am unable to specify the frame as owner to the dialog.

                  以下示例演示了上述两种方法.Top Click 按钮打开一个没有扩展JDialog的对话框.底部单击按钮打开一个对话框扩展JDialog.

                  Following example demonstrates above two approaches. Top Click button opens a dialog which is without extending JDialog. Bottom Click button opens a dialog with extending JDialog.

                  import java.awt.BorderLayout;
                  import java.awt.Dimension;
                  import java.awt.EventQueue;
                  import java.awt.event.ActionEvent;
                  import java.awt.event.ActionListener;
                  
                  import javax.swing.JButton;
                  import javax.swing.JDialog;
                  import javax.swing.JFrame;
                  
                  public class DialogEx {
                  
                      public static void main(String[] args) {
                          Runnable r = new Runnable() {
                              public void run() {
                                  new DialogEx().createUI();
                              }
                          };
                          EventQueue.invokeLater(r);
                      }   
                  
                      private void createUI() {
                          final JFrame frame = new JFrame();
                          frame.setLayout(new BorderLayout());
                  
                          JButton button1 = new JButton("Top Click");
                          JButton button2 = new JButton("Bottom Click");
                  
                          button2.addActionListener(new ActionListener() {
                              public void actionPerformed(ActionEvent arg0) {
                                  new DialogExtend(frame).createUI();
                              }
                          });
                  
                          button1.addActionListener(new ActionListener() {
                              public void actionPerformed(ActionEvent event) {
                                  new DialogWithoutExtend(frame).cretaUI();
                              }
                          });
                  
                          frame.setTitle("Test Dialog Instances.");
                          frame.add(button1, BorderLayout.NORTH);
                          frame.add(button2, BorderLayout.SOUTH);
                          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                          frame.setSize(new Dimension(300, 200));
                          frame.setVisible(true);
                      }
                  
                      class DialogExtend extends JDialog {
                          private JFrame frame;
                          public DialogExtend(JFrame frame) {
                              super(frame);
                              this.frame = frame;
                          }
                  
                          public void createUI() {
                              setLocationRelativeTo(frame);
                              setTitle("Dialog created by extending JDialog class.");
                              setSize(new Dimension(400, 100));
                              setModal(true);
                              setVisible(true);
                          }
                      }
                  
                      class DialogWithoutExtend {
                  
                          private JFrame frame;
                          public DialogWithoutExtend(JFrame frame) {
                              this.frame = frame;
                          }
                  
                          public void cretaUI() {
                              JDialog dialog = new JDialog();
                              dialog.setTitle("Dialog created without extending JDialog class.");
                              dialog.setSize(new Dimension(400, 100));
                              dialog.setLocationRelativeTo(frame);
                              dialog.setModal(true);
                              dialog.setVisible(true);
                          }
                      }
                  }
                  

                  推荐答案

                  对话框(或窗口)的所有者只能在构造函数中设置,所以设置它的唯一方法是使用以所有者为参数的构造函数,如:

                  A dialog's (or window's) owner can be set only in the constructor, so the only way to set it is by using a constructor which takes the owner as parameter, like:

                  class DialogWithoutExtend {
                  
                      private JFrame frame;
                      public DialogWithoutExtend(JFrame frame) {
                          this.frame = frame;
                      }
                  
                      public void cretaUI() {
                          JDialog dialog = new JDialog(frame);
                          dialog.setTitle("Dialog created without extending JDialog class.");
                          dialog.setSize(new Dimension(400, 100));
                          dialog.setLocationRelativeTo(frame);
                          dialog.setModal(true);
                          dialog.setVisible(true);
                      }
                  }
                  

                  这篇关于如何将 JFrame 设置为 JDialog 的父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Compiling C++ for the JVM(为 JVM 编译 C++)
                  Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                  How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                  Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                  How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                  Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)

                    <small id='jIbJ1'></small><noframes id='jIbJ1'>

                      <tbody id='jIbJ1'></tbody>
                      • <bdo id='jIbJ1'></bdo><ul id='jIbJ1'></ul>
                        <legend id='jIbJ1'><style id='jIbJ1'><dir id='jIbJ1'><q id='jIbJ1'></q></dir></style></legend>

                          <i id='jIbJ1'><tr id='jIbJ1'><dt id='jIbJ1'><q id='jIbJ1'><span id='jIbJ1'><b id='jIbJ1'><form id='jIbJ1'><ins id='jIbJ1'></ins><ul id='jIbJ1'></ul><sub id='jIbJ1'></sub></form><legend id='jIbJ1'></legend><bdo id='jIbJ1'><pre id='jIbJ1'><center id='jIbJ1'></center></pre></bdo></b><th id='jIbJ1'></th></span></q></dt></tr></i><div id='jIbJ1'><tfoot id='jIbJ1'></tfoot><dl id='jIbJ1'><fieldset id='jIbJ1'></fieldset></dl></div>
                        • <tfoot id='jIbJ1'></tfoot>