컨테이너

Dec 29, 2023
컨테이너

부모 클래스가 제공하는 속성과 메소드들은 자식 클래스인 JFrame이 사용할 수 있음
notion image
 

프레임에 버튼 추가하기


1) JBotton 객체 생성하기 → new해서 버튼 생성
2) add(객체 변수); → 프레임에 버튼 추가
package ex09; import javax.swing.*; import java.awt.*; public class MyFrame02 extends JFrame { public MyFrame02() { setSize(300,200); setTitle("My Frame"); setLayout(new FlowLayout()); JButton button = new JButton("버튼"); add(button); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { MyFrame01 f = new MyFrame01(); } }
notion image

 

JFrame 클래스


package ex09; import javax.swing.*; import java.awt.*; public class MyFrame03 extends JFrame { public MyFrame03() { setSize(300,150); setLocation(200,300); setTitle("My Frame"); setLayout(new FlowLayout()); getContentPane().setBackground(Color.yellow); JButton button1 = new JButton("확인"); JButton button2 = new JButton("취소"); this.add(button1); this.add(button2); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { MyFrame03 f = new MyFrame03(); } }
notion image

 
setTitle(String title)
JFrame 창의 제목
setSize(int width, int height)
JFrame 창의 크기
setLocation(int x, int y
JFrame 창의 위치를 화면 상의 좌표 (x, y)로 설정
setResizable(boolean resizable)
JFrame 창의 크기 조절 가능 여부
setVisible(boolean visible)
JFrame 창을 보이거나 숨기도록 설정 true이면 프레임 출력 모든 자식 컴포넌트를 추가한 후 맨 마지막에 위치
setDefaultCloseOperation(int operation)
JFrame 창이 닫힐 때의 동작
JFrame.EXIT_ON_CLOSE
프로그램 자동 종료 System.exit(0) 메서드가 호출되어강제로 종 프로그램이 종료되어야 할 때 사용
JFrame.HIDE_ON_CLOSE
창을 숨기는 동작
JFrame.DISPOSE_ON_CLOSE
Window.dispose() 메서드가 호출되어 창이 종료 프로그램의 완전히 종료 종료 이벤트 처리, 추가적인 작업을 수행할 때 사용
setLayout(LayoutManager manager)
레이아웃 매니저를 지정
add(Component component)
JFrame에 컴포넌트를 추가
setIconImage(IconImage)
윈도우 제목줄에 표시할 아이콘 설정
getContentPane()
JFrame의 Content Pane을 반환 Content Pane은 컴포넌트들이 배치되는 영역
 

JPanel 클래스


package ex09; import javax.swing.*; import java.awt.*; public class MyFrame04 extends JFrame { public MyFrame04() { JPanel panel1 = new JPanel(); panel1.setBackground(Color.orange); JButton b1 = new JButton("button1"); b1.setBackground(Color.yellow); JButton b2 = new JButton("button2"); b2.setBackground(Color.green); panel1.add(b1); panel1.add(b2); add(panel1); setSize(300, 150); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { MyFrame04 f = new MyFrame04(); } }
notion image

 
패널 : 컴포넌트들을 부탁할 수 있도록 설계된 컨테이너 중 하나
add(Component comp)
JPanel에 컴포넌트를 추가
setLayout(LayoutManager mgr)
JPanel 내부의 레이아웃 매니저를 설정
remove(Component comp)
JPanel에서 특정 컴포넌트를 제거
setBackground(Color bg)
JPanel의 배경색을 설정
setBorder(Border border)
JPanel에 테두리를 설정
setOpaque(boolean isOpaque)
JPanel의 불투명도를 설정
revalidate()
JPanel의 구성 요소들에 대한 레이아웃 재계산을 강제
repaint()
JPanel을 다시 그리도록 강제
Share article
RSSPowered by inblog