Java 에서는 JFrame 을 이용하여 다양한 GUI를 만들수 있습니다.
기본 예제)
setBounds(x, y, width, height) 창의 위치와 크기 설정
this.setBounds(100, 100, 500, 500);
프레임(MyFrame)의 x 버튼 (close 버튼) 을 눌렀을때 프로세스도 같이 종료
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
레이아웃 매니저는 사용하지 않겠다 (모든 UI 를 절대 좌표에 배치 하겠다)
this.setLayout(null);
JButton 객체를 생성하여 그 참조값을 btn1에 저장
JButton btn1 = new JButton("눌러봐요");
버튼 위치 설정
btn1.setLocation(x,y)
버튼 크기 설정
btn1.setSize(width, height)
btn1을 프레임에 추가
this.add(btn1)
프레임을 화면상에 실제 보이게 하기(false 하면 화면에 보이지 않음)
this.setVisible(true)
메인 메소드안에서 new MyFrame 객체를 생성해줘야 실행된다.
public static void main(String[] args) {
// MyFrame 객체 생성
new MyFrame("프로그램 이름");
}
Run 할 경우 다음과 같이 프로그램이 실행된다.
'Java' 카테고리의 다른 글
[Eclipse] Java FileWriter 예제 (0) | 2023.05.12 |
---|---|
[Eclipse] Java Input, Output (입력, 출력) (0) | 2023.05.12 |
[Eclipse] Java File 클래스 (0) | 2023.05.10 |
[Eclipse] Java Thread(스레드) 일시정지(sleep) (0) | 2023.05.09 |
[Eclipse] Java Exception(예외처리) (0) | 2023.05.09 |