Java Swing:在现有窗口顶部的定位对话框 [英] Java Swing: positioning dialog on top of existing window
本文介绍了Java Swing:在现有窗口顶部的定位对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以显示简单的Java Swing代码/Web资源,当单击JFrame的按钮时,该弹出窗口会将弹出对话框居中放置在现有JFrame窗口的顶部吗?
Can someone show simple Java Swing code/web resource that will position the popup dialog center-aligned on top of an existing JFrame window when the JFrame's button clicked?
推荐答案
哦.很简单:
假设您有一个包含JDialog的JFrame,并且希望该JDialog(打开时)位于JFrame的顶部.
Say you have a JFrame that contains a JDialog, and you want the JDialog (when opened) to be right on top of JFrame.
因此,在JDialog构造函数中,您应该具有以下内容:
So in JDialog constructor, you should have something like:
public class MyDialog extends JDialog
public MyDialog(JFrame parent)
{
super.setLocationRelativeTo(parent); // this will do the job
}
换句话说,将JFrame指针传递给对话框,然后调用setLocationRelativeTo(...);方法.
In other words, pass JFrame pointer to your dialog, and call setLocationRelativeTo(...); method.
这篇关于Java Swing:在现有窗口顶部的定位对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文