package ch01;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class BorderLayoutEx1 extends JFrame {
// BorderLayout <-- 컴포먼트를 동서남북 센터로
JButton buttons1 = new JButton("동");
JButton buttons2 = new JButton("서");
JButton buttons3 = new JButton("남");
JButton buttons4 = new JButton("북");
JButton buttons5 = new JButton("선터");
public BorderLayoutEx1() {
initData();
setinitLayout();
}
private void initData() {
setTitle("borderlayout 연습");
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setinitLayout() {
// setLayout();
// borderlaout 은 매게변수 값 하나를 더 추가할수 있다(방향지정가능)
add(buttons1, BorderLayout.EAST);
add(buttons2, BorderLayout.WEST);
add(buttons3, BorderLayout.SOUTH);
add(buttons4, BorderLayout.NORTH);
add(buttons5, BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) {
new BorderLayoutEx1();
}// main
}
package ch01;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class BorderLayoutEx2 extends JFrame {
// BorderLayout <-- 컴포먼트를 동서남북 센터로
JButton[] buttons = new JButton[5];
String[] direction = { BorderLayout.EAST, BorderLayout.WEST, BorderLayout.SOUTH,
BorderLayout.NORTH,BorderLayout.CENTER };
// JButton buttons1 = new JButton("동");
// JButton buttons2 = new JButton("서");
// JButton buttons3 = new JButton("남");
// JButton buttons4 = new JButton("북");
// JButton buttons5 = new JButton("선터");
public BorderLayoutEx2() {
initData();
setinitLayout();
}
private void initData() {
setTitle("borderlayout 연습");
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(direction[i]);
}
}
private void setinitLayout() {
// setLayout();
// borderlaout 은 매게변수 값 하나를 더 추가할수 있다(방향지정가능)
for (int i = 0; i < buttons.length; i++) {
add(buttons[i], direction[i]);
}
setVisible(true);
}
public static void main(String[] args) {
new BorderLayoutEx2();
}// main
}
package ch01;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
//배치관리자 연습 - FlowLaout
//components 수평, 수직 으로 배치해주는 녀석
public class FlowLayoutEx extends JFrame {
private JButton button1;
private JButton button2;
private JButton button3;
// 문제1 버튼 6개를 만들어 화면 출력
private JButton button4;
private JButton button5;
private JButton button6;
public FlowLayoutEx() {
// 초기화 값 세팅(메서드를 통해서 역할 분담) 순서도 중요하다
initData();// 데이터
setInitLayout();
}
public void initData() {
setTitle("FlowLayout 연습");
setSize(500, 500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
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() {
// 배치관리자를 설정해보자.
// FlowLayout
setLayout(new FlowLayout(FlowLayout.LEFT, 10, 200));
add(button1);
add(button2);
add(button3);
add(button4);
add(button5);
add(button6);
}
// 코드테스드
public static void main(String[] args) {
new FlowLayoutEx();
}
}
package ch01;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FlowLayoutEx3 extends JFrame {
// 배열
// 8개생성
private JButton[] button = new JButton[8];
public FlowLayoutEx3() {
initData();
setInitLayout();
}
private void initData() {
setTitle("FlowLayoutEx3");
setSize(600, 600);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
for (int i = 0; i < button.length; i++) {
button[i] = new JButton("button" + (i + 1));
}
}
private void setInitLayout() {
setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
for (int i = 0; i < button.length; i++) {
add(button[i]);
}
}
public static void main(String[] args) {
new FlowLayoutEx3();
}
}
package ch01;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FlowLayoutEx4 extends JFrame {
// 맴버변수 선언 할게요
private JButton[] buttons = new JButton[5];
// 생성자는 메모리에 올라갈 떄 처음 실행 되는 코드
// 구현부 안에서 순서도 중요하다
public FlowLayoutEx4() {
initData();
setInitLayout();
}
private void initData() {
setTitle("배열 활용");
setSize(600, 600);
// static 변수나 함수는 클래스 이름으로도 접근 가능
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton("button" + (i + 1));
}
}
private void setInitLayout() {
setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30));
for (int i = 0; i < buttons.length; i++) {
add(buttons[i]);
}
setVisible(true);
}
public static void main(String[] args) {
new FlowLayoutEx4();
}
}
package ch01;
import javax.swing.JButton;
import javax.swing.JFrame;
//좌표기반으로 컴포먼트들을 배치해보자
public class NoLayoutEx1 extends JFrame {
private JButton button1;
private JButton button2;
private JButton button3;
private NoLayoutEx1() {
initData();
setinitLayout();
}
private void initData() {
setTitle("좌표기반 연습");
setSize(1_000, 1_000);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button1 = new JButton("1");
button1.setSize(100, 100);
button2 = new JButton("2");
button2.setSize(150, 50);
button3 = new JButton("3");
button3.setSize(200, 30);
}
private void setinitLayout() {
setLayout(null);
// 좌표기반으로 셋팅하면 각 컴포넌트에 크기를 지정해 주어야 한다 기본 값 0
button1.setLocation(100, 100);
add(button1);
button2.setLocation(200, 150);
add(button2);
button3.setLocation(400, 200);
add(button3);
setVisible(true);
}
public static void main(String[] args) {
new NoLayoutEx1();
}
}
package ch01;
import javax.swing.JButton;
import javax.swing.JFrame;
public class NoLayoutEx2 extends JFrame {
JButton[] buttons = new JButton[10];
public NoLayoutEx2() {
initData();
setInitLayout();
}
private void initData() {
setTitle("혼자해보는 레이아웃없는 좌표형식");
setSize(1_000, 1_000);
setDefaultCloseOperation(EXIT_ON_CLOSE);
buttons[0] = new JButton("버튼1");
buttons[0].setSize(100, 100);
buttons[1] = new JButton("버튼2");
buttons[1].setSize(100, 100);
buttons[2] = new JButton("버튼3");
buttons[2].setSize(100, 100);
buttons[3] = new JButton("버튼4");
buttons[3].setSize(100, 100);
buttons[4] = new JButton("버튼5");
buttons[4].setSize(100, 100);
buttons[5] = new JButton("버튼6");
buttons[5].setSize(100, 100);
buttons[6] = new JButton("버튼7");
buttons[6].setSize(100, 100);
buttons[7] = new JButton("버튼8");
buttons[7].setSize(100, 100);
buttons[8] = new JButton("버튼9");
buttons[8].setSize(100, 100);
buttons[9] = new JButton("버튼9");
buttons[9].setSize(100, 100);
}
private void setInitLayout() {
setLayout(null);
buttons[0].setLocation(0, 0);
add(buttons[0]);
buttons[1].setLocation(100, 100);
add(buttons[1]);
buttons[2].setLocation(200, 0);
add(buttons[2]);
buttons[3].setLocation(300, 100);
add(buttons[3]);
buttons[4].setLocation(400, 0);
add(buttons[4]);
buttons[5].setLocation(500, 100);
add(buttons[5]);
buttons[6].setLocation(600, 0);
add(buttons[6]);
buttons[7].setLocation(700, 100);
add(buttons[7]);
buttons[8].setLocation(800, 0);
add(buttons[8]);
buttons[9].setLocation(900, 100);
add(buttons[9]);
setVisible(true);
}
public static void main(String[] args) {
new NoLayoutEx2();
}
}
컴포먼트 확인 기본
package ch02;
import java.awt.Checkbox;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class MyComponents extends JFrame {
private JButton button1;
private JLabel lable;// 글자를 넣어서 화면에 띄울수 있다
private JTextField textField;// 사용자한테 입력값을 받을수 있는 컴포먼트
private JPasswordField jPasswordField;// 사용자 비번을 받는 컴포먼트
private JCheckBox jCheckBox;//
public MyComponents() {
initData();
setIntiLayout();
}
private void initData() {
setTitle("컴포먼트 확인");
setSize(800, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button1 = new JButton("Jbutton");
lable = new JLabel("글자를 적는 컴포먼트");
textField = new JTextField("아이디 입력", 20);
jPasswordField = new JPasswordField("비번입력", 10);
jCheckBox = new JCheckBox("동의 하시겠습니까");
}
private void setIntiLayout() {
add(button1);
add(lable);
add(textField);
add(jPasswordField);
add(jCheckBox);
setLayout(new FlowLayout());
setVisible(true);
}
public static void main(String[] args) {
new MyComponents();
}
}
패널 추가연습
package ch02;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyFramePanel extends JFrame {
private JButton button1;
private JButton button2;
// 패널 : 컴포먼트들을 그룹화 시킬수 있닥 즉 가가에 배치관리자를 지정하수있다
private JPanel panel1;
private JPanel panel2;
public MyFramePanel() {
initDate();
setIntiLayout();
}
private void initDate() {
setTitle("패널 추가 연습");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1 = new JPanel();
panel1.setBackground(Color.red);
panel2 = new JPanel();
panel2.setBackground(Color.yellow);
// 버튼 초기화
button1 = new JButton("버튼1");
button2 = new JButton("버튼2");
}
private void setIntiLayout() {
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);
// 루트패널 기본레이아웃은 borderLayout 였는데
// 하지만 추가적으로 만들어 사용하는 Panel 은 기본 레이아웃이 FLowLayout이다.
panel1.add(button1);
// panel1.setLayout(null);
panel2.add(button2);
setVisible(true);
}
public static void main(String[] args) {
new MyFramePanel();
}
}
package ch02;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyFramePanel2 extends JFrame {
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
// 패널 : 컴포먼트들을 그룹화 시킬수 있닥 즉 가가에 배치관리자를 지정하수있다
private JPanel panel1;
private JPanel panel2;
public MyFramePanel2() {
initDate();
setIntiLayout();
}
private void initDate() {
setTitle("패널 추가 연습");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1 = new JPanel();
panel1.setBackground(Color.red);
panel2 = new JPanel();
panel2.setBackground(Color.yellow);
// 버튼 초기화
button1 = new JButton("버튼1");
button2 = new JButton("버튼2");
button3 = new JButton("버튼3");
button4 = new JButton("버튼4");
button5 = new JButton("버튼5");
button6 = new JButton("버튼6");
}
private void setIntiLayout() {
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);
// 루트패널 기본레이아웃은 borderLayout 였는데
// 하지만 추가적으로 만들어 사용하는 Panel 은 기본 레이아웃이 FLowLayout이다.
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
panel2.add(button4);
panel2.add(button5);
panel2.add(button6);
panel2.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
setVisible(true);
}
public static void main(String[] args) {
new MyFramePanel2();
}
}
'자바' 카테고리의 다른 글
외부 클래스를 이용한 집만들기! (1) | 2023.02.21 |
---|---|
외부 클래스 내부클래스를 이용 GUI (0) | 2023.02.21 |
GUI 프로그램 (0) | 2023.02.20 |
Object 클래스 (0) | 2023.02.20 |
인터페이스(interface) (0) | 2023.02.20 |