본문 바로가기
GUI

[2] JPanel | Container

by TR. 2021. 1. 7.

JPanel (Container)

*관계 : 최상위 컨테이너 - 컨테이너 - 컴포넌트

 

  1. JPanel 을 상속 받음
  2. 기본생성자에서 JPanel 설정
    ㄴ setBounds(int x,y,w,h)
     setBackground(Color c)
  3. JFrame에 붙여서 사용
    add(JPanel p)

package GUI;

import java.awt.*;

import javax.swing.*;

class MyPanel extends JPanel{
	
	public MyPanel() {
		setBounds(0, 0, 200, 200);
		setBackground(Color.orange); // java.awt.Color
	}
}


public class MyFrame extends JFrame {
	
	public MyFrame() {
		super("JFrame");
		setBounds(100,100,800,600);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setVisible(true);
		add(new MyPanel()); // JFrame에 JPanel 추가
	}
	
	public static void main(String[] args) {
		MyFrame frame = new MyFrame();
		
	}

}

'GUI' 카테고리의 다른 글

[4] Component | Jbutton  (0) 2021.01.12
[3] Container와 Component  (0) 2021.01.12
[1] JFrame | Top-level Container  (0) 2021.01.07
[0] 준비  (0) 2021.01.07
GUI  (0) 2021.01.07

댓글