- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一直没弄清楚这个问题;通常的嫌疑人不起作用。
给定:
FileDialog dlg=null;
dlg=new FileDialog(owner,"Select File to Load",FileDialog.LOAD);
dlg.setFile(null);
dlg.setVisible(true);
有什么方法可以让对话框居中吗?
一个关键点是在 setVisible() 处,调用线程被阻塞,直到对话框被关闭;在此之前的任何定位似乎都被忽略了。
最佳答案
下面的解决方案适用于 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 居中(这是我们需要的) , 并“记住”它在屏幕上的绝对位置。通过后续调用,它总是在同一个地方弹出,即使主应用程序窗口移动了也是如此。
尽管很奇怪,但从我的角度来看,新行为肯定比默认的看起来不专业的对话框左上角停靠要好。
关于java - 如何使 java.awt.FileDialog 在屏幕上居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2467180/
我正在 Access 数据库中制作表单我需要多次打开文件对话框窗口。我只是不明白为什么在我更改了几次选项值并打开文件对话框窗口后它没有更改过滤器。 Public Sub Command17_Click
我正在使用 Java 开发一个 socket 程序。我在后台运行带有套接字服务器的GUI。套接字服务器正在运行一个线程,每 10 毫秒检查一次套接字消息。它们都运行良好,但是当我尝试在 gui 中打开
我使用以下代码遇到了奇怪的行为: FileDialog openFileDialog1; // ... openFileDialog1.CustomPlaces.Add(@"C:\whatever\"
谁能给我解释一下为什么FileDialog实现两个构造函数,一个用于 Frame,一个用于 Dialog?我的意思是,他们为什么不利用继承的好处而只是使用 Window 创建一个构造函数? ?我问这个
我有一个使用 FileDialog 浏览文件的按钮。我需要读取我选择的文件中的所有行,我尝试了很多方法,但对我没有任何作用:( 我有另一个按钮,当我按下它时,它会读取文件并打印它。 B
我想显示一个 Java FileDialog 并将其初始大小设置为合理的值(它不显示最大化按钮,很多用户可能不知道可以使用 alt-space X 代替)。我尝试了 setBounds 但它没有做任何
在开发WinForms应用程序时,我遇到了OpenFileDialog和SaveFileDialog控件中的一个错误。 Google搜索发现只有一个发现相同问题的其他人,但是没有提供解决方案或解决方法
我在 StackOverflow 上找到了这段代码: Dim fd As Office.FileDialog Set fd = Application.FileDialog(msoFileDi
您好,我正在尝试从设置路径中基于 FileDialog 的文件夹导入文本文件?我有一个导入文本文件的代码,但它只打开一个通用的 C:\\ 路径,我应该如何修改下面的代码才能打开指定路径的文件夹? Su
我试图在 QtQuick 中为 FileDialog 设置默认起始目录,但它不起作用(意味着它总是打开可执行文件的目录): property url defPath : "/home/saeid/Wo
我正在创建一个基于 SWT 的应用程序。我有一个名为“Import”的按钮,单击此按钮将打开一个 swt FileDialog 来选择文件。当此文件对话框打开时,我有一个独特的要求,我只想显示特定文件
我试图在 QtQuick 中为 FileDialog 设置默认起始目录,但它不起作用(意味着它总是打开可执行文件的目录): property url defPath : "/home/saeid/Wo
我需要打开一个 FileDialog,以便在使用 Java 7 的 Windows 7 Home 上使用 SWT 打开文件。我需要在按下 BrowseBtn1 按钮 (SWT) 时打开此 FileDi
我可以使用 JFileChooser 给出文件扩展名。示例: JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter fi
当我想在从控制台获得一些输入后打开文件对话框时,它失败了。请参阅下面的代码。当我第一次调用 openFileDialog 然后 chooseOption 时,它工作正常。有人知道这是怎么可能的吗? p
我确信这些都是非常白痴的问题...但我以前从未处理过 FileDialog,而且我似乎无法让我的编码工作。 这是我的 JButton 监听器,我知道它进入是因为弹出了一个 FileDialog: pu
如果我有这样的过滤器字符串(FileDialog 的过滤器): "Image Files (*.bmp, *.jpg)|*.bmp;*.jpg|All Files (*.*)|*.*" C# 中是否有
JMenuBar menubar = new JMenuBar(); JMenu file = new JMenu("File"); add(menubar,BorderLayout.NORTH);
我会将 FileDialog 组件本地化为英语。我的默认语言是意大利语。可能吗? 我使用 FileDialog 的项目是一个 Eclipse 插件项目。 我找到了仅适用于 JFileChooser 组
当窗口对话框打开时,我无法选择多个文件。 void setup() { size(100,100); select_multi_files_via_FileDialog(); } import
我是一名优秀的程序员,十分优秀!