본문 바로가기

Java

[Eclipse] Java JFrame

Java 에서는 JFrame 을 이용하여 다양한 GUI를 만들수 있습니다.

 

기본 예제)

frame01

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 할 경우 다음과 같이 프로그램이 실행된다.