3

我一直无法弄清楚这一点。通常的嫌疑人不起作用。

鉴于:

FileDialog                  dlg=null;

dlg=new FileDialog(owner,"Select File to Load",FileDialog.LOAD);
dlg.setFile(null);
dlg.setVisible(true);

有什么办法可以让对话框居中吗?

一个关键点是在 setVisible() 处,调用线程被阻塞,直到对话框被关闭;而在此之前的任何定位似乎都被忽略了。

4

4 回答 4

7

下面的解决方案适用于 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 出现居中(这是我们需要的),并且“记住”它在屏幕上的绝对位置。通过后续调用,即使主应用程序窗口移动,它也会始终在同一个位置弹出。

尽管有些奇怪,但从我的角度来看,新的行为肯定比默认的不专业的对话框左上角停靠要好。

于 2012-11-05T13:45:31.553 回答
1

看来这可能仍然是一个错误....请参阅最后一行(尽管其日期为 2003 年)

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4333836

我把这个放在一起

        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]

但是屏幕还在左上角

于 2010-03-18T02:51:48.097 回答
0

使用 Java 7、Eclipse 4.4.1 和 Ubuntu 14.04,我能够找到居中 AWT 的解决方案FileDialog

我决心找到一个解决方案,因为Apple 建议使用 awt.FileDialog 而不是 SwingJFileChooser以获得更原生的外观和感觉。

诀窍是在设置它之前给你的FileDialog实例一个.sizelocation

使用boundscontentPane应用程序frame的 计算左角Point(minX, minY)FileDialogcontentPane的中心的距离Point

然后将location你的设置FileDialog为这个计算出来的Point,等瞧...居中。

    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);
于 2014-12-17T21:10:10.733 回答
0

试试这个代码:dlg.setLocationRelativeTo(null);

于 2013-05-23T13:11:13.650 回答