【问题标题】:Swing parameter for multiple monitors多显示器的摆动参数
【发布时间】:2016-11-17 13:03:42
【问题描述】:

我已经购买并安装了第二台显示器。 当我打开 eclipse 并将其拖到第二个监视器上时,如果我运行 swing 应用程序,它们会出现在第一个监视器上。

  1. 是否有一个 swing 参数可以让程序在第二个监视器上运行,或者至少在运行 eclipse 的监视器上运行?
  2. 如果我将摇摆程序从第一台显示器移动到第二台显示器,它会变黑。这是为什么呢?

其他信息:

我的驱动程序和 eclipse 都是最新的。

显示器 1:笔记本电脑集成显示器,QHD,17'。一切都在扩大,包括摇摆。由集成 Intel 显卡处理>>(Windows 决定,无法更改)

显示器 2:外部 HP 22' FHD 显示器。当一个窗口的 51% 被移动到这个屏幕上时,它会缩小(但仍然从原始大小向上,通常是 1080p)。由 NVIDIA Gpu 处理>>(Windows 决定,无法更改)

示例:

import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class test {

public static void main(String[] args){

    new test();     
}

public test(){

    JFrame testingBlack = new JFrame("MCVe");       
    JPantest testingB = new JPantest();

    testingBlack.add(testingB);
    testingBlack.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    testingBlack.setVisible(true);


}


private class JPantest extends JPanel{

    public JPantest(){          
        super();
        repaint();          
    }

    protected void paintComponent(Graphics g){

        super.paintComponent(g);
        g.drawLine(0, 0, 100, 100);

    }               
}   
}

这个程序画了一条简单的线。我从 ecplise 运行它,它从第一台显示器打开,这很好。我拖到第二台显示器上,它变黑了。

【问题讨论】:

标签: java swing autoscaling multiple-monitors


【解决方案1】:

是否有一个 swing 参数可以使程序在第二个监视器上运行,或者至少在运行 eclipse 的监视器上运行?

IDE 的位置无关紧要。任何无父窗口都将在屏幕的(0, 0) 坐标处初始化,与GraphicsEnvironment#getDefaultScreenDevice() 返回的坐标相同。您可以使用setLocationByPlatform 来允许窗口系统 (OS) 改为确定位置。如果你想强制定位你的第二台显示器,你可以这样做:

public Main() {

    JFrame testingBlack = new JFrame("MCVe");

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gds = ge.getScreenDevices();
    GraphicsConfiguration gc = gds[1].getDefaultConfiguration();
    Rectangle rect = gc.getBounds();
    testingBlack.setLocation(rect.getLocation());

    // or, if you like this style better

    testingBlack.setLocation(GraphicsEnvironment
                             .getLocalGraphicsEnvironment()
                             .getScreenDevices()[1]
                             .getDefaultConfiguration()
                             .getBounds()
                             .getLocation());

    testingBlack.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    testingBlack.setVisible(true);
}

只需确保您在 GraphicsDevice[] 中访问正确的设备,以避免 AIOOB 异常。 [0] 的那个应该是默认设备(虽然我不认为有这样的保证)。

如果我将摇摆程序从第一台显示器移到第二台显示器,它就会变黑。这是为什么呢?

不知道。一定与您的显示器设置有关。

【讨论】:

  • 设备切换后不会自动调用repaint()(自定义绘画)
  • @mKorbel 很有可能,为什么会这样?
  • 似乎是因为 API 中的实现不接受多个设备(对自定义绘画有效),可能是错误、功能、忘记或
猜你喜欢
  • 2014-12-22
  • 1970-01-01
  • 2012-09-08
  • 2018-01-31
  • 2011-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多