반응형
JFrame 창을 모니터 중앙에 띄울 수 있는 코드입니다.
1
2
3
4
5
6
|
Dimension frameSize = getSize();
Dimension windowSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((windowSize.width - frameSize.width) / 2,
(windowSize.height - frameSize.height) / 2);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
|
cs |
예시 1 / JFrame을 상속받을때
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.company;
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
public Main(){
super("티스토리");
setSize(500, 600);
Dimension frameSize = getSize();
Dimension windowSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((windowSize.width - frameSize.width) / 2,
(windowSize.height - frameSize.height) / 2);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
// write your code here
new Main();
}
}
|
cs |
Dimension frameSize = getSize();
Dimension windowSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((windowSize.width - frameSize.width) / 2,
(windowSize.height - frameSize.height) / 2);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
예시 2 / JFrame 객체를 이용할 때
package com.company;
import javax.swing.*;
import java.awt.*;
public class Main {
public Main(){
JFrame jFrame = new JFrame("티스토리 2");
jFrame.setSize(500, 600);
Dimension frameSize = jFrame.getSize();
Dimension windowSize = Toolkit.getDefaultToolkit().getScreenSize();
jFrame.setLocation((windowSize.width - frameSize.width) / 2,
(windowSize.height - frameSize.height) / 2);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
public static void main(String[] args) {
// write your code here
new Main();
}
}
|
cs |
반응형
'Programming > Java' 카테고리의 다른 글
[Java] GridLayout 예제 #1 (GUI) (0) | 2021.02.08 |
---|---|
[Java] BorderLayout 예제 #1 (GUI) (0) | 2021.02.07 |
[Java] AWT, Swing 클래스 계층도 (GUI) (0) | 2021.02.07 |
[Java] 자바 접근제어자 정리 (public/private/protected/default) (0) | 2021.02.04 |
[Java] 자바란? / 자바 특징 (8) | 2021.02.01 |
댓글