如何在屏幕上居中显示java.awt.FileDialog
我一直没能弄明白这一点;通常的嫌疑犯不起作用 鉴于:如何在屏幕上居中显示java.awt.FileDialog,java,awt,filedialog,Java,Awt,Filedialog,我一直没能弄明白这一点;通常的嫌疑犯不起作用 鉴于: FileDialog dlg=null; dlg=new FileDialog(owner,"Select File to Load",FileDialog.LOAD); dlg.setFile(null); dlg.setVisible(true); 有没有办法让对话居中 一个关键点是,在setVisible()处,调用线程被阻塞,直到对话框被取消;在此之前的任何定位似乎都会被忽略。似乎这仍然是一个bu
FileDialog dlg=null;
dlg=new FileDialog(owner,"Select File to Load",FileDialog.LOAD);
dlg.setFile(null);
dlg.setVisible(true);
有没有办法让对话居中
一个关键点是,在setVisible()处,调用线程被阻塞,直到对话框被取消;在此之前的任何定位似乎都会被忽略。似乎这仍然是一个bug。。。。见本文件最后一行(尽管日期为2003年) 我把这个拼凑起来的
FileDialog fd = new FileDialog(f, title, FileDialog.LOAD);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = fd.getSize().width;
int h = fd.getSize().height;
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;
System.out.println("Dialog location: " + fd.getLocation().toString());
fd.setLocation(x, y);
System.out.println("Dialog location: " + fd.getLocation().toString());
fd.setVisible(true);
我的结果是:
对话框位置:java.awt.Point[x=0,y=0]
对话框位置:java.awt.Point[x=840,y=525]
但是屏幕仍然在左上角下面的解决方案适用于SWT,可能它也可以用于AWT 正如它在当前shell的左上角显示的对话框一样,一个快速而肮脏的解决方案是创建一个新的、位置良好且不可见的shell,并从中打开FileDialog。我使用以下代码获得了一个可接受的结果:
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
public class CenteredFileDialog extends Dialog {
protected Shell shell;
public FileDialog dialog;
private int width = 560; // WinXP default
private int height = 420;
public CenteredFileDialog(Shell parent, int style) {
super(parent, style);
shell = new Shell(getParent(), SWT.APPLICATION_MODAL);
dialog = new FileDialog(shell, style);
}
public Object open() {
shell.setSize(width, height);
Rectangle parentBounds = getParent().getBounds();
shell.setLocation(
parentBounds.x + (parentBounds.width - width) / 2,
parentBounds.y + (parentBounds.height - height) / 2);
Object result = dialog.open();
shell.dispose();
return result;
}
}
该类可以这样使用:
CenteredFileDialog saveDialog = new CenteredFileDialog(getShell(), SWT.SAVE);
saveDialog.dialog.setFilterExtensions(new String[] { "*.txt" });
saveDialog.dialog.setFilterNames(new String[] { "Text (*.txt)" });
...
String f = (String)saveDialog.open();
if ( f != null ) {
name = f;
recentPath = saveDialog.dialog.getFilterPath();
}
该类仅部分解决了Windows平台的问题(在MacOS上,对话框始终以屏幕为中心;在Linux上,我没有测试)-第一次对话框相对于父shell居中显示(这是我们需要的),并“记住”其在屏幕上的绝对位置。通过后续调用,即使主应用程序窗口移动,它也总是在同一位置弹出
尽管有点奇怪,但从我的角度来看,新的行为肯定比默认的对话框左上角的非专业停靠要好。尝试以下代码:dlg.setLocationRelativeTo(null) 使用Java7、Eclipse4.4.1和Ubuntu14.04,我能够找到一个解决方案来集中AWT
FileDialog
我决定找到一个解决方案,因为在Swing的JFileChooser
上使用awt.FileDialog可以获得更为自然的外观和感觉
诀窍是在设置位置之前,为文件对话框
实例提供一个大小
使用主应用程序框架的contentPane
的bounds
计算FileDialog
左角点
与contentPane
中心点
的距离
然后将文件对话框的位置设置为该计算的点,等等。。。居中
final FileDialog fileDialog = new FileDialog(applicationFrame,
"Choose a file", FileDialog.LOAD);
/* Lots of code to be able to center an awt.FileDialog on screen... */
Rectangle rect = applicationFrame.getContentPane().getBounds();
/*
* Making sure FileDialog has a size before setVisible, otherwise
* left corner's distance from contentPane center cannot be found.
*/
fileDialog.pack();
fileDialog.setSize(800, 600);
fileDialog.validate();
double width = fileDialog.getBounds().getWidth();
double height = fileDialog.getBounds().getHeight();
double x = rect.getCenterX() - (width / 2);
double y = rect.getCenterY() - (height/ 2);
/* Could be new Point(x, y) */
Point leftCorner = new Point();
leftCorner.setLocation(x, y);
fileDialog.setLocation(leftCorner);
fileDialog.setVisible(true);
这就是我所看到的。这不是正确的错误链接,因为报告者将setLocationRealativeTo放在setVisible之后,所以它不被认为是错误。也许另一个链接指向正确的bug,设置作为最后一个操作可见。。我正在寻找..在setVisible(),调用线程被阻塞,直到对话框被取消;似乎setVisible()以某种方式重新定位了对话框;问题是在setVisible()处,调用线程被阻塞,直到对话框被取消;而在此之前的任何定位似乎都被忽略了。哈!有趣的是,我看到了这篇文章,因为我正在编写你发布的@ChrisK代码。它在我的Mac电脑上实际上不起作用。我已经为我正在处理的保存对话框代码复制了你的加载对话框代码,并试图使其在应用程序上居中,这时我注意到我为打开的对话框复制的加载代码应该已经在应用程序窗口上居中,但从来没有。“打开”对话框始终位于屏幕的中心,而不是其后面的应用程序窗口的中心。它也没有800x600尺寸。