我如何居中java.awt.FileDialog中的在屏幕上 [英] How do I center a java.awt.FileDialog on the screen

查看:185
本文介绍了我如何居中java.awt.FileDialog中的在屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有能够计算这一个;通常的嫌疑人不工作。

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

假设:

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?

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

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.

推荐答案

下面的解决方案适用于SWT,大概可以做AWT的伎俩,以及...

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

因为它显示了在当前shell的左上角的对话框,快速和肮脏的解决方案是创建一个新的,良好的定位和无形的外壳,并从它打开的FileDialog。我用下面的code可接受的结果:

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

类只是部分解决了Windows平台的问题(在MacOS上的对话是屏幕为中心无妨;在Linux上我没有测试) - 第一次对话出现较集中于母公司的外壳(这是我们所需要的)和记住在屏幕上的绝对位置。通过后续调用它总是在同一个位置弹出,即使主应用程序窗口移动。

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.

这篇关于我如何居中java.awt.FileDialog中的在屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆