如何用Java检测当前显示?
我连接了两个显示器,因此我可以在主显示器或辅助显示器上启动Java应用程序如何用Java检测当前显示?,java,Java,我连接了两个显示器,因此我可以在主显示器或辅助显示器上启动Java应用程序 问题是:我如何知道哪个显示器包含我的应用程序窗口,即,是否有方法使用Java检测当前显示器?是的,您可以使用窗口、框架和图形配置类来执行此操作 请参阅此处的更多信息: 是所有顶级窗口(Frame、JFrame、Dialog等)的基类,它包含返回该窗口正在使用的。GraphicsConfiguration具有getGraphicsDevice()方法,该方法返回GraphicsConfiguration所属的。然后
问题是:我如何知道哪个显示器包含我的应用程序窗口,即,是否有方法使用Java检测当前显示器?是的,您可以使用窗口、框架和图形配置类来执行此操作 请参阅此处的更多信息:
getGraphicsDevice()
方法,该方法返回GraphicsConfiguration所属的。然后可以使用该类对系统中的所有GraphicsDevices进行测试,并查看该窗口属于哪一个
Window myWindow = ....
// ...
GraphicsConfiguration config = myWindow.getGraphicsConfiguration();
GraphicsDevice myScreen = config.getDevice();
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
// AFAIK - there are no guarantees that screen devices are in order...
// but they have been on every system I've used.
GraphicsDevice[] allScreens = env.getScreenDevices();
int myScreenIndex = -1;
for (int i = 0; i < allScreens.length; i++) {
if (allScreens[i].equals(myScreen))
{
myScreenIndex = i;
break;
}
}
System.out.println("window is on screen" + myScreenIndex);
windowmywindow=。。。。
// ...
GraphicsConfiguration config=myWindow.getGraphicsConfiguration();
GraphicsDevice myScreen=config.getDevice();
GraphicsEnvironment env=GraphicsEnvironment.getLocalGraphicsEnvironment();
//AFAIK-无法保证屏幕设备处于正常状态。。。
//但我用过的每一个系统都有。
GraphicsDevice[]allScreens=env.getScreenDevices();
int myScreenIndex=-1;
for(int i=0;i
当另一个监视器刚刚添加到系统中,并且用户将Java窗口重新定位到该监视器中时,Nate提出的方法不起作用。这是我的用户经常遇到的情况,对我来说,唯一的解决办法就是重新启动java.exe,强制它重新使用监视器
主要问题是myWindow.getGraphicsConfiguration().getDevice()始终返回启动Java小程序或应用程序的原始设备。您可能希望它显示当前监视器,但我自己的经验(非常耗时且令人沮丧)是,仅仅依靠myWindow.getGraphicsConfiguration().getDevice()并不是万无一失的。如果有人有更可靠的不同方法,请让我知道
执行屏幕匹配(使用allScreen[i].equals(myScreen)调用)后,将继续返回调用小程序的原始监视器,而不是可能重新定位小程序的新监视器 内特的解决方案似乎在大多数情况下都有效,但并非所有情况下都有效,正如我所经历的那样。困惑地提到他在连接显示器时遇到问题,我在“Win+Left”和“Win+Right”键命令方面遇到问题。我对这个问题的解决方案看起来是这样的(也许解决方案本身就有问题,但至少这比Nate的解决方案对我更有效):
GraphicsDeviceMyDevice=myFrame.getGraphicsConfiguration().getDevice();
对于(GraphicsDevice gd:GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()){
如果(frame.getLocation().getX()>=gd.getDefaultConfiguration().getBounds().getMinX())&&
frame.getLocation().getX()=gd.getDefaultConfiguration().getBounds().getMinY()&&
frame.getLocation().getY()
这对我很有用
public static GraphicsDevice getWindowDevice(Window window) {
Rectangle bounds = window.getBounds();
return asList(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()).stream()
// pick devices where window located
.filter(d -> d.getDefaultConfiguration().getBounds().intersects(bounds))
// sort by biggest intersection square
.sorted((f, s) -> Long.compare(//
square(f.getDefaultConfiguration().getBounds().intersection(bounds)),
square(s.getDefaultConfiguration().getBounds().intersection(bounds))))
// use one with the biggest part of the window
.reduce((f, s) -> s) //
// fallback to default device
.orElse(window.getGraphicsConfiguration().getDevice());
}
public static long square(Rectangle rec) {
return Math.abs(rec.width * rec.height);
}
稍有不同的用例:如果您想在某处创建窗口之前了解主显示,并且“显示”在技术上意味着
java.awt.GraphicsDevice
,那么相应的java.awt.GraphicsConfiguration
应该是
java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration()
图形配置的排序列表如下所示:
public static GraphicsConfiguration[] getConfigurations()
{
final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice def = ge.getDefaultScreenDevice();
final List<GraphicsConfiguration> cfgs = new ArrayList<GraphicsConfiguration>();
cfgs.add(def.getDefaultConfiguration());
for (final GraphicsDevice gd : ge.getScreenDevices())
{
if (gd!=def)
{
cfgs.add(gd.getDefaultConfiguration());
}
}
final GraphicsConfiguration[] res = cfgs.toArray(new GraphicsConfiguration[cfgs.size()]);
return res;
}
publicstaticgraphicsconfiguration[]getConfigurations()
{
final GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
最终图形设备定义=ge.getDefaultScreenDevice();
最终列表cfgs=新的ArrayList();
add(def.getDefaultConfiguration());
对于(最终图形设备gd:ge.getScreenDevices())
{
如果(gd!=def)
{
add(gd.getDefaultConfiguration());
}
}
最终图形配置[]res=cfgs.toArray(新图形配置[cfgs.size()]);
返回res;
}
其中默认显示为列表中的第一个。请检查我的答案。。。它可能对您有帮助,也可能没有帮助。您可以通过使用Rectangle.contains大大简化和澄清代码。
public static GraphicsConfiguration[] getConfigurations()
{
final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice def = ge.getDefaultScreenDevice();
final List<GraphicsConfiguration> cfgs = new ArrayList<GraphicsConfiguration>();
cfgs.add(def.getDefaultConfiguration());
for (final GraphicsDevice gd : ge.getScreenDevices())
{
if (gd!=def)
{
cfgs.add(gd.getDefaultConfiguration());
}
}
final GraphicsConfiguration[] res = cfgs.toArray(new GraphicsConfiguration[cfgs.size()]);
return res;
}