如何使 java.awt.FileDialog 在屏幕上居中

发布于 2024-08-25 04:43:02 字数 307 浏览 9 评论 0原文

我一直无法弄清楚这个问题;通常的怀疑不起作用。

鉴于:

FileDialog                  dlg=null;

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

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

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

I have never been able to figure this one out; the usual suspects don't work.

Given:

FileDialog                  dlg=null;

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

is there any way to get that dialog centered?

A key point is that at setVisible(), the calling thread is blocked until the dialog is dismissed; and any positioning prior to that seems to be ignored.

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

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

发布评论

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

评论(4

我恋#小黄人 2024-09-01 04:44:44

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

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

诀窍是在设置位置之前为您的FileDialog实例指定一个大小

使用主应用程序 framecontentPanebounds 来计算左角 Point 的距离 (minX , minY) 距 contentPane 中心 PointFileDialog

然后将您的FileDialoglocation 设置为这个计算出的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);

Using Java 7, Eclipse 4.4.1 and Ubuntu 14.04 I was able to find a solution for centering AWT FileDialog.

I was determined to find a solution because Apple recommends using awt.FileDialog over Swing's JFileChooser for a more native look and feel.

The trick is to give your FileDialog instance a size before setting its location.

Use the bounds of the contentPane of your main application frame to calculate the distance of the left corner Point (minX, minY) of FileDialog from the contentPane's center Point.

Then set the location of your FileDialog to this calculated Point, et voilá ... centered.

    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);
谎言月老 2024-09-01 04:44:41

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

Try this code: dlg.setLocationRelativeTo(null);

甜点 2024-09-01 04:44:35

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

https://bugs.java.com/bugdatabase/view_bug?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]

但屏幕仍然在左上角

Appears that this may still be a bug.... see last line of this (though its dated 2003)

https://bugs.java.com/bugdatabase/view_bug?bug_id=4333836

I threw this together

        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);

And my output was:

Dialog location: java.awt.Point[x=0,y=0]

Dialog location: java.awt.Point[x=840,y=525]

But the screen was still in the top left corner

枕头说它不想醒 2024-09-01 04:44:22

下面的解决方案适用于 SWT,也许它也可以为 AWT 解决问题...

由于它在当前 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 上我没有测试) - 第一次对话框相对于父外壳居中显示(这是我们所需要的),并且“记住”它在屏幕上的绝对位置。通过后续调用,即使主应用程序窗口移动,它也始终会在同一位置弹出。

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

The below solution works for SWT, probably it can do the trick for AWT as well...

As it shows the dialog in left top corner of the current shell, a quick-and-dirty solution is to create a new, well-positioned and invisible shell and to open FileDialog from it. I got an acceptable result with the following code:

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;
    }
}

The class can be used this way:

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();
} 

The class only partially solves the problem for Windows platform (On MacOS the dialog is screen-centered anyway; on Linux I did not test) - first time the dialog appears centered relatively to the parent shell (which is what we need), and "remembers" its absolute position on the screen. By subsequent calls it always pops up in the same place, even if the main application window moved.

Despite the oddity, from my perspective the new behaviour is definitely better than the default unprofessionally looking top-left docking of the dialog.

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