본문 바로가기

[JAVA][SWING]화면 중앙에 창 띄우기

반응형

열심히 포스팅 했더니 완전 간략한 방법이 있었다

 

출처 : [java] 프레임 스크린 중앙에 배치하기

 

단 한줄 코딩 

frame.setLocationRelativeTo(null);

 

하지만 아직 듀얼모니터 환경에서는 아래 내가 작성한 방법대로 해야하지 않을까?

뭔가 짧고 간결하면서도 정확 명확 그런글이 발견될때까지 일단 그대로 두기로 한다

 

 

 

실행하게 될 어떤 컴퓨터건 그 해당 컴퓨터의 화면 정 중앙에 프로그렘을 띄워준다

 

다중모니터인경우 첫 모니터의 중앙에 띄워주도록 작성했다.

 

GraphicsEnvironment, GraphicsDevice 를 사용한다

 

작성 코드는 다음과 같다. 

 

https://developer-salieri.tistory.com/185

 

위 링크의 소스을 가져다 놓고 class PanelChangeExample 를 수정하였다.

 

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class PanelChangeExample {
    public static void main(String[] args) {
 
        JPanelTest win = new JPanelTest();
        
        win.setTitle("frame test");
        win.jpanel01 = new JPanel01(win);
        win.jpanel02 = new JPanel02(win);
 
        win.add(win.jpanel01);
        
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice [] gd = ge.getScreenDevices();
        
        if(gd.length > 0) {
            int width  = gd[0].getDefaultConfiguration().getBounds().width;
            int height = gd[0].getDefaultConfiguration().getBounds().height;
            
            win.setSize(width / 2, height / 2);
            
            win.setLocation(
                ((width  / 2- (win.getSize().width  / 2)) + gd[0].getDefaultConfiguration().getBounds().x,
                ((height / 2- (win.getSize().height / 2)) + gd[0].getDefaultConfiguration().getBounds().y);
            win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            win.setVisible(true);
        }
        else {
            GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(win);
        }
    }
}
cs

 

 

띄어지게될 모니터 크기의 1/4 크기로 띄워지게 된다

 

작성 끝

반응형