Java:JOptionPane 影响父 JFrame? (包括 SSCCE)

发布于 2024-12-11 08:15:02 字数 1449 浏览 0 评论 0原文

给出以下小程序:

import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Example extends JApplet
{
   JPanel aPanel;

   @Override
   public void init()
   {
      try
      {
         javax.swing.SwingUtilities.invokeAndWait(new Runnable()
         {
            public void run()
            {
               makeGui();
            }
         });
      }
      catch (InterruptedException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      catch (InvocationTargetException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }      
   }

   public void makeGui()
   {
      aPanel = new JPanel(new BorderLayout());

      this.getContentPane().add(aPanel, BorderLayout.CENTER);

      JFrame aTestFrame =new JFrame();
      aTestFrame.setBounds(new Rectangle(200,200));
      JPanel aTestPanel = new JPanel(new BorderLayout());
      aTestPanel.setBounds(new Rectangle(200,200));

      aTestFrame.add(aTestPanel);
      aTestFrame.setVisible(true);
      JOptionPane.showMessageDialog(aTestFrame, "arfarf");


   }

}

为什么 JOptionPane 调用会关闭 aTestFrame?如果我省略调用,则 2 个帧会正确渲染,但是当我在 JOptionPane 中单击“确定”时,父 JFrame 将关闭。

第一个答案是正确的,显然存在焦点问题..谢谢!

Given the following applet:

import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Example extends JApplet
{
   JPanel aPanel;

   @Override
   public void init()
   {
      try
      {
         javax.swing.SwingUtilities.invokeAndWait(new Runnable()
         {
            public void run()
            {
               makeGui();
            }
         });
      }
      catch (InterruptedException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      catch (InvocationTargetException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }      
   }

   public void makeGui()
   {
      aPanel = new JPanel(new BorderLayout());

      this.getContentPane().add(aPanel, BorderLayout.CENTER);

      JFrame aTestFrame =new JFrame();
      aTestFrame.setBounds(new Rectangle(200,200));
      JPanel aTestPanel = new JPanel(new BorderLayout());
      aTestPanel.setBounds(new Rectangle(200,200));

      aTestFrame.add(aTestPanel);
      aTestFrame.setVisible(true);
      JOptionPane.showMessageDialog(aTestFrame, "arfarf");


   }

}

Why does the JOptionPane call closes aTestFrame? If i leave out the call the 2 frames render correctly, but when i click on OK in the JOptionPane the parent JFrame is closed.

The first answer is correct, apparently there is a focus issue.. THANKS!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此生挚爱伱 2024-12-18 08:15:02

我认为最好不要将 JFrame 与 JApplet 一起使用,而是使用与 JApplet 的 Window 祖先绑定的 JDialog:

   public void makeGui() {
      aPanel = new JPanel(new BorderLayout());

      this.getContentPane().add(aPanel, BorderLayout.CENTER);

      Window win = SwingUtilities.getWindowAncestor(Example.this);

      JDialog dialog = new JDialog(win, "My Dialog", ModalityType.MODELESS);

      JPanel dialogPanel = new JPanel();
      dialogPanel.setPreferredSize(new Dimension(200, 200));
      dialog.add(dialogPanel);
      dialog.pack();
      dialog.setVisible(true);

      JOptionPane.showMessageDialog(dialog, "arfarf");
   }

I think that you're better off not using a JFrame with a JApplet, but instead using a JDialog that is tied into the JApplet's Window ancestor:

   public void makeGui() {
      aPanel = new JPanel(new BorderLayout());

      this.getContentPane().add(aPanel, BorderLayout.CENTER);

      Window win = SwingUtilities.getWindowAncestor(Example.this);

      JDialog dialog = new JDialog(win, "My Dialog", ModalityType.MODELESS);

      JPanel dialogPanel = new JPanel();
      dialogPanel.setPreferredSize(new Dimension(200, 200));
      dialog.add(dialogPanel);
      dialog.pack();
      dialog.setVisible(true);

      JOptionPane.showMessageDialog(dialog, "arfarf");
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文