package ch01;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
// Swinig -> 배치 관리자 : FlowLayout
// 컴포넌트들을 (버튼, 라벨) 등을 수평, 수직으로 배치를 해주는 클래스 이다.
public class FlowLayoutEx extends JFrame {
private JButton button1;
private JButton button2;
// 4개더 만들어서 추가 하기
// 생성자
public FlowLayoutEx() {
super.setTitle("FlowLayout 연습");
super.setSize(500, 500);
super.setVisible(true);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 생성자에서 메서드 호출 가능하다.
initData();
setInitLayout();
}
// 멤버 변수를 초기화 하는 기능(값 넣다)
public void initData() {
button1 = new JButton("button1");
button2 = new JButton("button2");
}
// 컴포넌트들을 배치하는 기능
public void setInitLayout() {
// 배치 관리자 --> BorderLayout 이라는 배치관리자가 기본으로 활용 된다.
// FlowLayout flowLayout = new FlowLayout();
// super.setLayout(flowLayout); // 배치관리자 --> FlowLayout
// 배치관리자 생성 및 JFrame 셋팅
super.setLayout(new FlowLayout());
// 컴포넌트들을 붙이다.
super.add(button1);
super.add(button2);
}
// 코드 테스트
public static void main(String[] args) {
// FlowLayoutEx f1 = new FlowLayoutEx(); // <---- 부를 수 있는 이름이 있는 상태
new FlowLayoutEx(); // <---- 익명 클래스 : 다시접근해서 사용할 일 없으면...
} // end of main
}
코드 리팩토링 - 배열과 반복문을 활용해서 코드를 수정하시오.
문제
package ch01;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
// Swinig -> 배치 관리자 : FlowLayout
// 컴포넌트들을 (버튼, 라벨) 등을 수평, 수직으로 배치를 해주는 클래스 이다.
public class FlowLayoutEx2 extends JFrame {
// 배열 활용
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
// 4개더 만들어서 추가 하기
// 생성자
public FlowLayoutEx2() {
super.setTitle("FlowLayout 연습");
super.setSize(500, 500);
super.setVisible(true);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 생성자에서 메서드 호출 가능하다.
initData();
setInitLayout();
}
// 멤버 변수를 초기화 하는 기능(값 넣다)
public void initData() {
// 반복문 활용
button1 = new JButton("button1");
button2 = new JButton("button2");
button3 = new JButton("button3");
button4 = new JButton("button4");
button5 = new JButton("button5");
button6 = new JButton("button6");
}
// 컴포넌트들을 배치하는 기능
public void setInitLayout() {
// 배치 관리자 --> BorderLayout 이라는 배치관리자가 기본으로 활용 된다.
// FlowLayout flowLayout = new FlowLayout();
// super.setLayout(flowLayout); // 배치관리자 --> FlowLayout
// 배치관리자 생성 및 JFrame 셋팅
super.setLayout(new FlowLayout(FlowLayout.LEADING, 50, 50));
// 컴포넌트들을 붙이다.
// 반복문 활용
super.add(button1);
super.add(button2);
super.add(button3);
super.add(button4);
super.add(button5);
super.add(button6);
}
// 코드 테스트
public static void main(String[] args) {
// FlowLayoutEx f1 = new FlowLayoutEx(); // <---- 부를 수 있는 이름이 있는 상태
new FlowLayoutEx2(); // <---- 익명 클래스 : 다시접근해서 사용할 일 없으면...
} // end of main
}
풀이
package ch01;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
// Swing -> 배치 관리자 : FlowLayout
// 컴포넌트들을 (버츤, 라벨) 등을 수평, 수직으로 배치를 해주는 클래스이다.
public class FlowLayoutEx2 extends JFrame {
// 배열 활용 - 하나의 변수로 여러개 통으로 관리하고 싶다면 배열을 써보자.
private JButton[] buttons;
// 생성자
public FlowLayoutEx2() {
super.setTitle("FlowLayout 연습");
super.setSize(500, 500);
super.setVisible(true);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 생성자에서 메서드 호출 가능하다.
initData();
setIniLayout();
}
// 멤버 변수를 초기화 하는 기능 (값을 넣다)
public void initData() {
buttons = new JButton[6]; //공간만 선언 [][][][][][]
// 인덱스 번호를 활용 0 1 2 3 4 5
// 반복문
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton("button" + (i + 1)); // 인덱스 번호가 0부터 시작하기에 +1함
}
}
// 컴포넌트들을 배치하는 기능
public void setIniLayout() {
// 배치 관리자 --> BorderLayout 이라는 배치관리자가 기본으로 활용 된다.
// FlowLayout flowLayout = new FlowLayout();
// super.setLayout(flowLayout); // 배치 관리자 --> FlowLayout
// 배치 관리자 생성 및 JFrame 셋팅
super.setLayout(new FlowLayout(FlowLayout.LEFT, 50, 50)); // 배치 관리자 --> FlowLayout
// 컴포넌트들을 붙이다.
// 반복문
for (int i = 0; i < buttons.length; i++) {
super.add(buttons[i]);
}
}
// 코드 테스트
public static void main(String[] args) {
// FlowLayoutEx f1 = new FlowLayoutEx(); // <--- 부를 수 있는 이름이 있는 상태
// 다시 접근해서 사용할 일 없으면
new FlowLayoutEx2(); // <--- 익명 클래스
} // end of main
} // end of class
BorderLayout 연습
package ch01;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
// 단축키 습관 --> ctrl + shift + s, f, o
public class BorderLayoutEx2 extends JFrame {
JButton[] buttons;
String[] directions = {BorderLayout.EAST, BorderLayout.WEST, BorderLayout.NORTH,
BorderLayout.SOUTH, BorderLayout.CENTER};
public BorderLayoutEx2() {
initData();
setInitLayout();
}
public void initData() {
setTitle("borderLayout 연습");
setSize(600, 600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttons = new JButton[5];
}
public void setInitLayout() {
// 배치 관리자 선정 (컨테이너)
// BorderLayout -- 컴포넌트들을 동서남북가운데로 배치 시켜주는 레이아웃이다.
setLayout(new BorderLayout());
// 반복문을 활용해서 코드를 완성하세요
add();
}
public static void main(String[] args) {
new BorderLayoutEx2();
} // end of main
}
풀이
package ch01;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
// 단축키 습관 - ctrl + shift + s, f, o
public class BorderLayoutEx2 extends JFrame {
final int WIDTH = 600;
final int HEIGHT = 600;
JButton[] buttons;
String[] directions = { BorderLayout.EAST, BorderLayout.WEST, BorderLayout.SOUTH, BorderLayout.NORTH,
BorderLayout.CENTER };
// 생성자 만들기
public BorderLayoutEx2() {
initData();
setInitLayout();
}
public void initData() {
setTitle("borderLayout 연습");
setSize(WIDTH, HEIGHT);
setVisible(true);
// 프로그램 닫으면 종료해
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttons = new JButton[directions.length];
}
public void setInitLayout() {
// 배치 관리자 선정
// BorderLayout -- 컴포넌트들을 동서남북 가운데로 배치 시켜주는 레이아웃이다.
setLayout(new BorderLayout());
for (int i = 0; i < directions.length; i++) {
add(new JButton(directions[i]), directions[i]);
}
}
public static void main(String[] args) {
new BorderLayoutEx2();
}
}
'Java > Swing' 카테고리의 다른 글
Swing - 6 (Key Listener) (0) | 2024.04.30 |
---|---|
Swing - 5 (이벤트 리스너) (0) | 2024.04.29 |
Swing - 4 (이미지 겹치는 방법) (0) | 2024.04.29 |
Swing - 3 (이미지 올리기) (0) | 2024.04.29 |
Swing - 2 (0) | 2024.04.26 |