I have a Java app that launches, creates a GUI and works great. If the user changes the screen resolution (switches from 1440x900 to 1280x768), I'd like to be able to listen for that event.
我有一个Java应用程序启动,创建一个GUI,并且工作得很好。如果用户更改屏幕分辨率(从1440x900切换到1280x768),我希望能够收听该事件。
Any ideas?
有任何想法吗?
PS - I'd like to do this in event/listener mode, not in poll mode, so that extra CPU cycles aren't wasted on something like a timer constantly polling the screen size every N seconds to see if it's changed.
PS - 我想在事件/监听器模式下执行此操作,而不是在轮询模式下执行此操作,这样就不会浪费额外的CPU周期,例如定时器每隔N秒不断轮询屏幕大小以查看它是否已更改。
5 个解决方案
#1
4
This post is old, but: - polling the screen size once every second will not have any impact on performance - when the screen is resized, every window should receive a repaint() call (you need to test this for the OSs you target)
这篇文章很旧,但是: - 每秒轮询屏幕大小一次不会对性能产生任何影响 - 当屏幕调整大小时,每个窗口都应该接收一个repaint()调用(你需要为你的目标操作系统测试这个)
#2
2
I don't think that Java can do this by itself. You would have to have a "hook" into the operating system that detects this change and may wish to consider using JNA, JNI, or a separate utility such as perhaps Sigar that reports to your Java program. Out of curiosity, why do you wish to do this? Is it for a game you're making or so that you can re-size a GUI application?
我不认为Java可以自己做到这一点。您必须在操作系统中有一个“钩子”来检测此更改,并且可能希望考虑使用JNA,JNI或单独的实用程序(例如可能向您的Java程序报告的Sigar)。出于好奇,你为什么要这样做?是你正在制作的游戏还是可以重新调整GUI应用程序的大小?
#3
2
Apart from Hovercrafts's suggestion you might consider a background thread that checks the current screen resolution using Toolkit.getScreenSize()
.
除了Hovercrafts的建议,您可以考虑使用Toolkit.getScreenSize()检查当前屏幕分辨率的后台线程。
You would need to test this to find out how big the performance impact of that thread to the system is. How often it checks for changes depends on your requirements (how quick your application needs to react to the change)
您需要对此进行测试,以了解该线程对系统的性能影响有多大。检查更改的频率取决于您的要求(应用程序对更改做出反应的速度)
#4
1
You can create a global PAINT listener to detect screen resize.
您可以创建全局PAINT侦听器来检测屏幕大小调整。
// screen resize listener
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
@Override
public void eventDispatched(AWTEvent event) {
// take a look at http://*.com/questions/10123735/get-effective-screen-size-from-java
Rectangle newSize = getEffectiveScreenSize();
if (newSize.width != windowSize.width || newSize.height != windowSize.height)
resize();
}
}, AWTEvent.PAINT_EVENT_MASK);
#5
-1
Here is my suggestion for this problem,
这是我对这个问题的建议,
- Every swing object can implement an interface called
java.awt.event.ComponentListener
. - 每个swing对象都可以实现一个名为java.awt.event.ComponentListener的接口。
- One of its method is
componentResized(ComponentEvent e)
. - 其中一个方法是componentResized(ComponentEvent e)。
- Your main application frame should implement this interface and override the resize event method. This is how you listen to the resize event, Checkout this link . I hope this helps you.
- 您的主应用程序框架应实现此接口并覆盖resize事件方法。这是你如何收听resize事件,Checkout这个链接。我希望这可以帮助你。
#1
4
This post is old, but: - polling the screen size once every second will not have any impact on performance - when the screen is resized, every window should receive a repaint() call (you need to test this for the OSs you target)
这篇文章很旧,但是: - 每秒轮询屏幕大小一次不会对性能产生任何影响 - 当屏幕调整大小时,每个窗口都应该接收一个repaint()调用(你需要为你的目标操作系统测试这个)
#2
2
I don't think that Java can do this by itself. You would have to have a "hook" into the operating system that detects this change and may wish to consider using JNA, JNI, or a separate utility such as perhaps Sigar that reports to your Java program. Out of curiosity, why do you wish to do this? Is it for a game you're making or so that you can re-size a GUI application?
我不认为Java可以自己做到这一点。您必须在操作系统中有一个“钩子”来检测此更改,并且可能希望考虑使用JNA,JNI或单独的实用程序(例如可能向您的Java程序报告的Sigar)。出于好奇,你为什么要这样做?是你正在制作的游戏还是可以重新调整GUI应用程序的大小?
#3
2
Apart from Hovercrafts's suggestion you might consider a background thread that checks the current screen resolution using Toolkit.getScreenSize()
.
除了Hovercrafts的建议,您可以考虑使用Toolkit.getScreenSize()检查当前屏幕分辨率的后台线程。
You would need to test this to find out how big the performance impact of that thread to the system is. How often it checks for changes depends on your requirements (how quick your application needs to react to the change)
您需要对此进行测试,以了解该线程对系统的性能影响有多大。检查更改的频率取决于您的要求(应用程序对更改做出反应的速度)
#4
1
You can create a global PAINT listener to detect screen resize.
您可以创建全局PAINT侦听器来检测屏幕大小调整。
// screen resize listener
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
@Override
public void eventDispatched(AWTEvent event) {
// take a look at http://*.com/questions/10123735/get-effective-screen-size-from-java
Rectangle newSize = getEffectiveScreenSize();
if (newSize.width != windowSize.width || newSize.height != windowSize.height)
resize();
}
}, AWTEvent.PAINT_EVENT_MASK);
#5
-1
Here is my suggestion for this problem,
这是我对这个问题的建议,
- Every swing object can implement an interface called
java.awt.event.ComponentListener
. - 每个swing对象都可以实现一个名为java.awt.event.ComponentListener的接口。
- One of its method is
componentResized(ComponentEvent e)
. - 其中一个方法是componentResized(ComponentEvent e)。
- Your main application frame should implement this interface and override the resize event method. This is how you listen to the resize event, Checkout this link . I hope this helps you.
- 您的主应用程序框架应实现此接口并覆盖resize事件方法。这是你如何收听resize事件,Checkout这个链接。我希望这可以帮助你。