JFrame 클래스
✒️ 2025-05-15 14:33 내용 수정
Swing
- 자바에서 AWT(Abstract Window Toolkit)의 제약을 개선한 GUI(Graphic User Interface) 구현 개발 툴킷
- 위키백과 스윙(자바) 참고.
JFrame 클래스
Frame을 상속받는 클래스
- 최상위 레벨의 Swing container 중 하나로, JComponent를 상속받는 Component를 사용하려면 JFrame을 비롯한 최상위 레벨 Swing container를 root로 두는 계층 구조에 배치해야 한다.
- 참고 자료 : Oracle JComponent
- Component 클래스와 Container 클래스의 메서드를 상속 받기에 JFrame을 비롯한 여러 JCompnent 클래스에는 비슷한 메서드가 많다.(예:add(Component c))
- 상속(Inheritance) 참고.
JFrame 프레임이름 = new JFrame();
JFrame 프레임이름 = new JFrame(GraphicsConfiguration gc);
JFrame 프레임이름 = new JFrame(String title);
JFrame 프레임이름 = new JFrame(String title, GraphicsConfiguration gc);
- JFrame 추가 방법
- JFrame 클래스 상속 받아서 새 JFrame 만들기 : 생성자에서
super();로 JFrame 호출 - JFrame 클래스 객체 생성 후 지정
종료 관련 상수
- 사용자가 frame을 닫으려 할 때 실행되는 명령을 설정
- 참고 자료 : KAZU's 자바 JFrame 클래스
| 종료 관련 상수 | 설명 |
|---|---|
| HIDE_ON_CLOSE | defalutCloseOperation(기본 종료 옵션) 창을 닫으면 frame을 숨김 |
| DO_NOTHING_ON_CLOSE | frame을 닫을 때 아무것도 하지 않음 |
| DISPOSE_ON_CLOSE | frame을 닫으면 자원을 반납 |
| EXIT_ON_CLOSE | frame을 닫으면 프로그램 종료 |
메서드
| 메서드 | 설명 |
|---|---|
setLocation(int a, int b) |
x=a, y=b 위치에 frame 이 표시될 위치 설정 |
setSize(int a, int b) |
x=a, y=b 크기의 frame 설정 |
setVisible(true) |
frame을 보이게 할지 결정 |
Component add(Component c) |
특정 Component를 container에 추가 |
add(Component comp, Object constraints) |
특정 Component를 옵션으로 추가 |
remove(Component comp) |
frame에 추가되었던 Component를 제거 |
setLayout(LayoutManager mgr) |
Layout Manager를 설정 |
setDefaultCloseOperation() |
frame을 종료할 때 기본 옵션 설정 |
int getDefaultCloseOperation() |
frame을 종료할 때 기본 옵션을 반환 |
setBounds(int x, int y, int width, int height) |
Component의 위치와 크기를 원하는대로 지정 |
setResizable(boolean value) |
frame의 크기 변경 허용 여부를 결정 |
setBackground(Color bgColor) |
프레임의 배경 색을 적용 |
setBorder(Border b) |
Component의 테두리를 지정, Border 클래스 객체를 넣는다 |
setJMenuBar(final JMenuBar menubar) |
해당 frame의 메뉴바를 설정 |
JMenuBar getJMenuBar() |
frame의 JMenuBar를 반환 |
setIconImage(Image image) |
frame의 아이콘 이미지를 설정 |
Graphics getGraphics() |
frame의 Graphics를 반환 |
void repaint(long time, int x, int y, int width, int height) |
frame에 직사각형의 Component를 채색 |
JRootPane getRootPane() |
frame의 RootPane을 반환(setRootPane()은 protected method) |
Container getContentPane() |
frame의 ContentPane을 반환 |
setContentPane(Container contentPane) |
frame의 ContentPane을 설정 |
JLayeredPane getLayeredPane() |
frame의 LayeredPane을 반환 |
setLayeredPane(JLayeredPane layeredPane) |
frame의 LayeredPane을 설정 |
Component getGlassPane() |
frame의 GlassPane을 반환 |
setGlassPane(Component glassPane) |
frame의 GlassPane을 설정 |
setBackground(Color bgColor)에서 지정하면 적용이 잘 안되기 때문에 JPanel의setBackground(Color bgColor)메서드로 Panel의 배경을 바꾸고 JFrame에 적용하는 방법을 사용했다.- JFrame의 레이아웃을 null로 설정하면 수동 배치를 해야 한다.