package SelfStudy_Ch9;
import javax.swing.*;
public class SS1112_1 extends JFrame{
public SS1112_1() {
setTitle("300 X 300 스윙 프레임 만들기");
setSize(300, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new SS1112_1();
}
}
package SelfStudy_Ch9;
import java.awt.*;
import javax.swing.*;
public class SS1112_1 extends JFrame{
public SS1112_1() {
super("타이틀 문자열");
setTitle("300 X 300 스윙 프레임 만들기");
setSize(300, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = this.getContentPane(); //컨탠트 팬 알아내기
JButton b = new JButton("Click");
c.add(b); //컨탠트 팬에 컴포넌트 달기
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new SS1112_1();
}
}
JLabel()
JLabel(string text)
JLabel(String text, int align)
align (라벨 정렬)
컨테이너마다 하나의 배치 관리(Layout Manager)가 존재함.
삽입되는 모든 컴포넌트(Component)의 위치와 크기를 결정하고 적절히 배치함.
컨테이너의 크기가 변하면 내부 컴포넌트들의 위치&크기 재조정.
<배치 관리자 삭제법>
JPanel p = new JPanel();
p.setLayout(null);


FlowLayout()
FlowLayout(int align)
FlowLayout(int align, int hGap, int vGap)
align

BorderLayout()
BorderLayout(int hGap, int vGap)
hGap
주의) 버튼 생성시 방향 지정 필수!
void add(Component comp, int index)
comp : 컴포넌트
index : 컴포넌트의 위치.
package SelfStudy_Ch9;
import javax.swing.*;
import java.awt.*;
public class SS1112_3 extends JFrame{
public SS1112_3() {
setTitle("BorderLayout Sample");
Container c = getContentPane();
c.setLayout(new BorderLayout(30, 20));
c.add(new JButton("Calculate"), BorderLayout.CENTER);
c.add(new JButton("add"), BorderLayout.NORTH);
c.add(new JButton("sub"), BorderLayout.SOUTH);
c.add(new JButton("div"), BorderLayout.WEST);
c.add(new JButton("mul"), BorderLayout.EAST);
setSize(500, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new SS1112_3();
}
}
GridLayout()
GridLayout(int rows, int cols)
GridLayout(int rows, int cols, int hGap, int vGap)
rows
setVgap()
setHgap()
주의) Label 칸도 grid를 차지함.
package SelfStudy_Ch9;
import javax.swing.*;
import java.awt.*;
public class SS1112_4 extends JFrame{
public SS1112_4() {
setTitle("GridLayout Sample");
Container c = getContentPane();
GridLayout grid = new GridLayout(4, 2);
grid.setVgap(5);
c.setLayout(grid);
c.add(new JLabel(" 이름"));
c.add(new JTextField(""));
c.add(new JLabel(" 학번"));
c.add(new JTextField(""));
c.add(new JLabel(" 학과"));
c.add(new JTextField(""));
c.add(new JLabel(" 과목"));
c.add(new JTextField(""));
setSize(500, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new SS1112_4();
}
}
c.setLayout(null);
JLabel - setLocation(x, y), setSize(w, h)
Integer.toString(i)
package SelfStudy_Ch9;
import javax.swing.*;
import java.awt.*;
public class SS1112_5 extends JFrame{
public SS1112_5() {
setTitle("Null Container Sample");
Container c = getContentPane();
c.setLayout(null);
JLabel la = new JLabel("Hello, Press Buttons!");
la.setLocation(130, 50);
la.setSize(200, 20);
c.add(la);
for (int i = 1; i <= 9; i++) {
JButton b = new JButton(Integer.toString(i));
//정수 i를 문자열로 변환.
b.setLocation(i * 15, i * 15);
b.setSize(50, 20);
c.add(b);
}
setSize(500, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new SS1112_5();
}
}
JLabel 배열 만들기.
JPanel을 각각 만들고 그걸 BorderLayout 컨탠트 팬에 부착.
(int)(Math.random() * 200) 0~199
package SelfStudy_Ch9;
import javax.swing.*;
import java.awt.*;
public class SS1112_6 extends JFrame{
public SS1112_6() {
setTitle("Multiple Panels and Layouts");
Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add(new NorthPanel(), BorderLayout.NORTH);
c.add(new CenterPanel(), BorderLayout.CENTER);
setSize(600, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class NorthPanel extends JPanel {
public NorthPanel() {
setBackground(Color.gray);
setLayout(new FlowLayout());
add(new JButton("Open"));
add(new JButton("Read"));
add(new JButton("Close"));
}
}
class CenterPanel extends JPanel {
private JLabel [] labels = {new JLabel("Hello"), new JLabel("Java"), new JLabel("Love")};
public CenterPanel() {
setLayout(null);
for (int i = 0; i < labels.length; i++) {
int x = (int)(Math.random() * 200);
int y = (int)(Math.random() * 200);
labels[i].setLocation(x, y);
labels[i].setSize(100, 20);
add(labels[i]);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new SS1112_6();
}
}
package SelfStudy_Ch9;
import javax.swing.*;
import java.awt.*;
public class SS1112_7 extends JFrame{
public SS1112_7() {
setTitle("SwingFlix - 나만의 OTT 플레이어");
Container c = getContentPane();
c.setBackground(Color.darkGray);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.darkGray);
centerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 100));
JLabel posterLabel = new JLabel("[우주선 포스터 이미지 영역]", JLabel.CENTER);
//JLabel 안의 텍스트를 중앙정렬함.
/*
* 구분해야 할 것.
* FlowLayout.CENTER -> Panel 안에서 컴포넌트들을 중앙정렬함.
*/
posterLabel.setForeground(Color.orange);
posterLabel.setFont(new Font("SansSerif", Font.BOLD, 10));
JLabel subTitleLabel = new JLabel("[특별 상영] 동물 친구들의 우주 대모험!", JLabel.CENTER);
subTitleLabel.setForeground(Color.white);
subTitleLabel.setFont(new Font("SansSerif", Font.BOLD, 10));
centerPanel.add(posterLabel);
centerPanel.add(subTitleLabel);
c.add(centerPanel);
setSize(800, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new SS1112_7();
}
}

package SelfStudy_Ch9;
import javax.swing.*;
import java.awt.*;
public class SS1112_7 extends JFrame{
public SS1112_7() {
setTitle("SwingFlix - 나만의 OTT 플레이어");
Container c = getContentPane();
c.setBackground(Color.darkGray);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.darkGray);
centerPanel.setLayout(new GridLayout(2, 1));
JLabel posterLabel = new JLabel("[우주선 포스터 이미지 영역]", JLabel.CENTER);
//JLabel 안의 텍스트를 중앙정렬함.
/*
* 구분해야 할 것.
* FlowLayout.CENTER -> Panel 안에서 컴포넌트들을 중앙정렬함.
*/
posterLabel.setForeground(Color.orange);
posterLabel.setFont(new Font("SansSerif", Font.BOLD, 10));
JLabel subTitleLabel = new JLabel("[특별 상영] 동물 친구들의 우주 대모험!", JLabel.CENTER);
subTitleLabel.setForeground(Color.white);
subTitleLabel.setFont(new Font("SansSerif", Font.BOLD, 10));
centerPanel.add(posterLabel);
centerPanel.add(subTitleLabel);
c.add(centerPanel);
setSize(800, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new SS1112_7();
}
}

package SelfStudy_Ch9;
import javax.swing.*;
import java.awt.*;
public class SS1112_7 extends JFrame{
public SS1112_7() {
setTitle("SwingFlix - 나만의 OTT 플레이어");
Container c = getContentPane();
c.setBackground(Color.darkGray);
JPanel centerPanel = new JPanel();
JPanel subPanel = new JPanel();
centerPanel.setBackground(Color.darkGray);
//여기가 핵심! centerPanel에도 FlowLayout 주기
centerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 100));
subPanel.setBackground(Color.darkGray);
subPanel.setLayout(new GridLayout(2, 1, 0, 10));
JLabel posterLabel = new JLabel("[우주선 포스터 이미지 영역]", JLabel.CENTER);
//JLabel 안의 텍스트를 중앙정렬함.
/*
* 구분해야 할 것.
* FlowLayout.CENTER -> Panel 안에서 컴포넌트들을 중앙정렬함.
*/
posterLabel.setForeground(Color.orange);
posterLabel.setFont(new Font("SansSerif", Font.BOLD, 10));
JLabel subTitleLabel = new JLabel("[특별 상영] 동물 친구들의 우주 대모험!", JLabel.CENTER);
subTitleLabel.setForeground(Color.white);
subTitleLabel.setFont(new Font("SansSerif", Font.BOLD, 10));
subPanel.add(posterLabel);
subPanel.add(subTitleLabel);
centerPanel.add(subPanel);
c.add(centerPanel);
setSize(800, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new SS1112_7();
}
}

package SelfStudy_Ch9;
import javax.swing.*;
import java.awt.*;
public class SS1112_8 extends JFrame{
Container c;
public SS1112_8() {
setTitle("SwingFlix - 나만의 OTT 플레이어");
c = getContentPane();
c.setLayout(new BorderLayout());
c.add(new NP(), BorderLayout.NORTH);
c.add(new CP(), BorderLayout.CENTER);
c.add(new SP(), BorderLayout.SOUTH);
setSize(600, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class NP extends JPanel {
public NP() {
setBackground(Color.black);
JLabel title = new JLabel("스윙플릭스", JLabel.CENTER);
title.setForeground(Color.white);
add(title);
}
}
class CP extends JPanel {
public CP() {
setBackground(Color.darkGray);
setLayout(new FlowLayout(FlowLayout.CENTER, 0, 80));
JPanel subPanel = new JPanel();
subPanel.setBackground(Color.darkGray);
subPanel.setLayout(new GridLayout(2, 1, 0, 10));
JLabel plabel = new JLabel("[우주선 포스터 이미지 영역]", JLabel.CENTER);
plabel.setFont(new Font("SansSerif", Font.BOLD, 10));
plabel.setForeground(Color.yellow);
JLabel slabel = new JLabel("[특별 상영] 동물 친구들의 우주 대모험!", JLabel.CENTER);
slabel.setFont(new Font("SansSerif", Font.BOLD, 10));
slabel.setForeground(Color.white);
subPanel.add(plabel);
subPanel.add(slabel);
add(subPanel);
}
}
class SP extends JPanel {
public SP() {
setBackground(Color.black);
setLayout(new FlowLayout());
add(new JButton("재생"));
add(new JButton("찜목록"));
add(new JButton("종료"));
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new SS1112_8();
}
}
색 적용하는 다양한 방법 정리할 것.
extends JFrame 잊지 않기.
import javax.swing.;
import java.awt.;
main에서 클래스 객체 생성 잊지 않기.
기본 설정값 4가지
setTitle("SwingFlix - 나만의 OTT 플레이어");
Container c = getContentPane();
setSize(500, 600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);