在第二个屏幕或监视器上居中显示JFrame-Java
我试图在第二个屏幕上打开JFrame并将其居中。我可以在第二个屏幕上打开JFrame,但是如何将其居中。以下是我制作的示例代码:在第二个屏幕或监视器上居中显示JFrame-Java,java,jframe,Java,Jframe,我试图在第二个屏幕上打开JFrame并将其居中。我可以在第二个屏幕上打开JFrame,但是如何将其居中。以下是我制作的示例代码: import javax.swing.*; import java.awt.*; public class Question { public Question() { JFrame f = new JFrame(); JPanel panel = new JPanel(); f.getContentPane().add(panel);
import javax.swing.*;
import java.awt.*;
public class Question {
public Question() {
JFrame f = new JFrame();
JPanel panel = new JPanel();
f.getContentPane().add(panel);
f.pack();
f.setTitle("Hello");
f.setSize(200,200);
showOnScreen(1,f);
f.setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
new Question();
}
};
SwingUtilities.invokeLater(r);
}
public static void showOnScreen( int screen, JFrame frame ) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gd = ge.getScreenDevices();
if( screen > -1 && screen < gd.length ) {
frame.setLocation(gd[screen].getDefaultConfiguration().getBounds().x, frame.getY());
} else if( gd.length > 0 ) {
frame.setLocation(gd[0].getDefaultConfiguration().getBounds().x, frame.getY());
} else {
throw new RuntimeException( "No Screens Found" );
}
}
}
它可以工作,但我不想硬编码,因为在实际的程序中,帧大小会发生变化
我也试过这个:
frame.setLocation((gd[screen].getDefaultConfiguration().getBounds().width)/2-frame.getSize().width/2, (gd[screen].getDefaultConfiguration().getBounds().height)/2-frame.getSize().height/2);
但它会在第一个屏幕上打开它。有道理
请有人可以帮助应用正确的修复。谢谢你的帮助。谢谢。我就是这样解决的。我的应用程序最多可以有两个屏幕
public void showOnScreen( int screen, JFrame frame ) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gd = ge.getScreenDevices();
if( screen > 0 && screen < 2 ) {
int x = frame.getWidth()/2;
int y = frame.getHeight()/2;
frame.setLocation((gd[screen].getDefaultConfiguration().getBounds().width * 3/2) - x, (gd[screen].getDefaultConfiguration().getBounds().height *1/2) - y);
} else if( gd.length > 0 ) {
frame.setLocationRelativeTo(null);
} else {
throw new RuntimeException( "No Screens Found" );
}
}
public void show屏幕(int屏幕,JFrame){
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[]gd=ge.getScreenDevices();
如果(屏幕>0&&屏幕<2){
int x=frame.getWidth()/2;
int y=frame.getHeight()/2;
frame.setLocation((gd[screen].getDefaultConfiguration().getBounds().width*3/2)-x,(gd[screen].getDefaultConfiguration().getBounds().height*1/2)-y);
}否则如果(gd.length>0){
frame.setLocationRelativeTo(空);
}否则{
抛出新的RuntimeException(“未找到屏幕”);
}
}
要在辅助屏幕上居中显示(本例中的最后一个屏幕),我执行了以下操作:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gd = ge.getScreenDevices();
GraphicsConfiguration gc = gd[gd.length - 1].getDefaultConfiguration();
frame.setLocationRelativeTo(new JFrame(gd));
根据setLocationRelativeTo(部件c)
文件:
如果组件不为null,但当前未显示,则
窗口位于由定义的目标屏幕的中心
与此组件关联的图形配置
将
f.setLocationRelativeTo(空)
在f.setVisible()之前或之后。
然后在第一个屏幕上打开框架。我不想让它打开,然后在第二个屏幕上居中。请参考谢谢Madhan。那是我早些时候提到的那一页。我找到了解决办法。请在下面查看。如果您发现任何问题,请告诉我。只要它解决了您的问题,就可以了
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gd = ge.getScreenDevices();
GraphicsConfiguration gc = gd[gd.length - 1].getDefaultConfiguration();
frame.setLocationRelativeTo(new JFrame(gd));