Java: How can I bring a JFrame to the front?(Java:如何将 JFrame 放在最前面?)
问题描述
我目前正在等待一个非常重要的公告,我创建了一个简单的应用程序,每 30 分钟检查一次公告是否发布
I am currently waiting for a very important announcement and I have created a simple application to check every 30 minutes whether the announcement was made or not
发布公告后,我希望弹出一个窗口并通知我.该窗口只是一个简单的 JFrame,其中包含一些文本.
When the announcement is made, I want a window to pop up and inform me about it. The window is just a simple JFrame that has some text in it.
我有一个类叫做 Announcement.
I have a class called Announcement.
在这个类中,我为名为 Form 的框架定义了另一个类,所以我有这样的事情:
Inside this class I define another class for the frame called Form, so I have something like this:
class Form{
public Form(){
//create frame and show it
JFrame frame = new JFrame("anouncement");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = frame.getContentPane();
frame.setSize(420, 100);
JLabel l1 = new JLabel("The announcement was just made");
l1.setFont(new Font("Courier New", Font.ITALIC, 20));
c.add(l1);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
frame.toFront(); //this thing doesn't work
}
}
public class Announcement{
public static void main(String[] args){
//do the checking every 30 minutes part
}
}
toFront 函数根本不起作用.
The toFront function doesn't work at all.
我想要它做的是,例如:当我浏览时,我希望这个窗口弹出并设置焦点在这个窗口上.
What I want it do is, for example: while I'm browsing, I want this window to pop up and set the focus on this window.
所以无论我已经打开了多少个其他窗口,我都希望它位于所有其他窗口的前面.
So I want it to be in front of all the other windows no matter how many other windows I have already opened.
推荐答案
如果我理解正确的话,你的代码至少有两个窗口——一个是你称之为表单"的基本程序,另一个是你希望的窗口跳出"基本窗口,对吗?如果是这样,那么不要使用第二个 JFrame,而是让跳出"窗口成为 JDialog 或 JOptionPane 并使其依赖于第一个窗口.
If I understand you correctly, your code has at least two windows -- one the basic program which you call "Form" and the other a window that you wish to "jump out" over the basic window, correct? If so, then don't use a second JFrame, but rather have the "jump-out" window be a JDialog or JOptionPane and make it dependent on the first window.
如果我的假设不正确,请纠正我.
Please correct me if my assumptions are incorrect.
这篇关于Java:如何将 JFrame 放在最前面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java:如何将 JFrame 放在最前面?
基础教程推荐
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01