Java:JOptionPane 影响父 JFrame? (包括 SSCCE)
给出以下小程序:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为最好不要将 JFrame 与 JApplet 一起使用,而是使用与 JApplet 的 Window 祖先绑定的 JDialog:
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: