如何在前台启动文件?
启动一个文件=启动与给定文件关联的程序,并在程序启动时自动打开该文件。
假设我运行 IntelliJ IDEA,运行我的代码,程序的主窗口(模式)显示出来。我的程序在前台。
然后我从我的程序启动一个 .pdf 文件(目前这意味着 AcroReader 将被执行)。 AA 将显示在 IntelliJ 前面但在我的程序后面。
问题
我希望 AA(当然这只是一个例子)显示在我的程序前面,而不是后面。怎么做呢?
请注意,这并不意味着我想将我的程序移至后台!
为了启动我使用的文件
java.awt.Desktop.getDesktop().open(new java.io.File(filepath));
我的 GUI 是在 Swing 中完成的。
更新 1
为了排除自定义小部件、事件等的任何影响,我将 JButton 简单地放在我的窗口底部(JDialog)——它是用 Scala 编写的,但这部分类似于 Java 语法:
var dlg = new javax.swing.JDialog(null,"test",Dialog.ModalityType.DOCUMENT_MODAL);
dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
var button = new JButton("Select Me");
var actionListener = new ActionListener() {
def actionPerformed( actionEvent : ActionEvent) = {
java.awt.Desktop.getDesktop().open(new java.io.File("test.pdf"))
}
};
button.addActionListener(actionListener);
dlg.add(button, BorderLayout.SOUTH);
dlg.setSize(300, 100);
dlg.setVisible(true);
单击后,AA 将显示在我的应用程序后面。由于运行 AA 需要几秒钟的时间,因此我还尝试单击该按钮,并将鼠标从窗口移开。完全相同的行为。
我还注意到 AA 显示在与我的窗口相同的相对位置,AA 的左上角靠近我的应用程序的右下角。
Launch a file = launch program associated with given file and automatically open that file on start of the program.
Let's say I run IntelliJ IDEA, I run my code and main window (modal) of my program shows up. My program is in the foreground.
Then I launch a .pdf file (for now it means AcroReader will be executed) from my program. AA will show up in front of IntelliJ but behind my program.
Question
I would like AA (it is just an example here of course) to be shown in front of my program, not behind. How to do it?
Please note, this does not mean I would like to move my program to the background!
For launching files I use
java.awt.Desktop.getDesktop().open(new java.io.File(filepath));
My GUI is done in Swing.
Update 1
To rule out, any influence of custom widgets, events, and so on, I put simply JButton at the bottom of my window (JDialog) -- it is in Scala, but this piece is similar to Java syntax:
var dlg = new javax.swing.JDialog(null,"test",Dialog.ModalityType.DOCUMENT_MODAL);
dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
var button = new JButton("Select Me");
var actionListener = new ActionListener() {
def actionPerformed( actionEvent : ActionEvent) = {
java.awt.Desktop.getDesktop().open(new java.io.File("test.pdf"))
}
};
button.addActionListener(actionListener);
dlg.add(button, BorderLayout.SOUTH);
dlg.setSize(300, 100);
dlg.setVisible(true);
Once clicked, AA is shown behind my app. Since it takes several seconds to run AA I also tried to click the button, and move the mouse away from my window. Exactly the same behaviour.
I also noted that AA is shown at the same relative position to my window, top left corner of AA is near bottom right corner of my app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以尝试这样的事情。在我的机器(带有 Gnome2 的 Ubuntu 10.4 LTS)上,它在前面给出了 evince(pdf-viewer),如果我关闭/隐藏 evince - JDialog 会被放回到前面。
在 Windows 上,情况可能非常不同,因为实际上没有“dlg.toBack();”调用行为是相同的。
You may try something like this. On my machine (Ubuntu 10.4 LTS with Gnome2) it gives the evince (pdf-viewer) in the front, and if I close/hide evince - JDialog is placed back to the front.
On windows it may be very different, since actually without "dlg.toBack();" invocation behavior is the same.