본문 바로가기
IT/JAVA

[Java] java.awt - (Button, Label, TextField, TextArea, Choice, CheckBox, List, Scrollbar 등)

by 행복한 용용이 2020. 9. 11.
반응형

[ java.awt ]

순서

1. Frame 상속

2. 선언부에 필요한 객체 생성

3. 배치부에 design (Component 배치)

**항상 작업은 작은 것 >> 큰 것 순으로 !


1.

결과)


코드)

 

public class DesignTest extends Frame{

//           선언부

       Panel pN = new Panel();

       Panel pS = new Panel();    //panel은 보통 default로 만듬

 

       Label l = new Label("입력", Label.CENTER);     //label은 한꺼번에 만듦

       TextField tf = new TextField();

       Button send = new Button("전송");

 

       TextArea ta = new TextArea();

 

       Choice ch = new Choice();//Choice는 default 생성자 밖에 없음

       Button exit = new Button("종료");

 

       public DesignTest() {

             super("Design Test !!!");

//           배치부

//           panel의 default는 flowlayout

 

             setLayout(new BorderLayout(0,10));

 

//          flowlayout은 default크기밖에 안됨으로 borderlayout써야함(가운데가 가장 큼)

//          LayoutManager lm = new BorderLayout();

//          pN.setLayout(lm);

 

             pN.setLayout(new BorderLayout(10,0));

//          익명으로 객체 생성/생성자 BorderLayout(int hgap,int vgap) : 사이사이 공백 여기선 hgap만 됨

             l.setBackground(new Color(150,210,210))//배경색

 

//           l.setForeground(new Color(100,30,70));  //글자색(생성자)

 

             l.setForeground(Color.magenta);   //글자색(static field 상수)

             Font f = new Font("굴림",Font.BOLD, 20);

             l.setFont(f);

             pN.add(l,"West");

             pN.add(tf,"Center");

             send.setFont(f);

             pN.add(send,"East");

 

 

//          pS의 두 버튼은 크기 같음으로 Gridelayout

             pS.setLayout(new GridLayout(1, 2, 10, 0));

//           choice에 값 넣기

             ch.add("서울");

             ch.add("경기,인천");

             ch.add("강원");

             ch.add("충청");

 

             pS.add(ch);

             pS.add(exit);

             exit.setFont(f);

             //frame의 default는 borderlayout

 

             add(pN, "North");

             add(ta, "Center");

             add(pS, BorderLayout.SOUTH);

 

             //항상 마지막이 setVisible!

//          setSize(300, 500);

//           setLocation(300, 200);

             setBounds(300, 200, 300, 500)//setSize와 setLocation합친것

             setVisible(true);

       }

 

       public static void main(String[] args) {

             new DesignTest(); //익명객체생성(해당 클래스의 메소드 등을 추가로 호출 할 필요가 없기 때문에 가능)

       }

}


2.

결과)


코드)

 

package com.kitri.awt.design;

import java.awt.*;

public class ItemTest extends Frame{

 

       Panel pN = new Panel();

       Panel pS = new Panel();

 

       CheckboxGroup cbg = new CheckboxGroup()//라디오버튼 1개만 선택됨

       Checkbox mor = new Checkbox("아침",cbg,true);

       Checkbox aft = new Checkbox("점심",cbg,false);

       Checkbox eve = new Checkbox("저녁",cbg,false);

 

       Checkbox app = new Checkbox("사과",true)//체크박스 다중선택 가능

       Checkbox banana = new Checkbox("바나나",false);

       Checkbox straw = new Checkbox("딸기",false);

 

       TextArea ta = new TextArea();

 

       Choice ch = new Choice();

       Button exit = new Button("종료");

 

       public ItemTest() {

             super("아이템 테스트!!!");

 

             pN.setLayout(new GridLayout(2,3,10,10));

             pN.add(mor);

             pN.add(aft);

             pN.add(eve);

 

             pN.add(app);

             pN.add(banana);

             pN.add(straw);

 

             pS.setLayout(new BorderLayout(10,0));

             ch.add("아침");

             ch.add("점심");

             ch.add("저녁");

             pS.add(ch,"Center");

             pS.add(exit,"East");

 

             setLayout(new BorderLayout(0,10));

 

             add(pN,"North");

             add(ta,"Center");

             add(pS,"South");

 

             setBounds(300, 200, 300, 500);

             setVisible(true);

       }

 

       public static void main(String[] args) {

             new ItemTest();

       }

}


3.

결과)


코드)

 

package com.kitri.awt.design;

import java.awt.*;

public class ListTest extends Frame{

 

       Panel p1 = new Panel();

       Panel p2 = new Panel();

       Panel p3 = new Panel();

 

       List listL = new List();

       List listR = new List();

       TextField tfL = new TextField();

       TextField tfR = new TextField();

 

       Button btR = new Button("▷");

       Button btRAll = new Button("▶");

       Button btL = new Button("◁");

       Button btLAll = new Button("◀");

 

       public ListTest() {

             super("리스트 테스트");

 

             p1.setLayout(new BorderLayout(0, 10));

             p1.add(listL,"Center");

             p1.add(tfL,"South");

 

             p2.setLayout(new GridLayout(6, 1, 0, 20));

             p2.add(new Label(""));

             p2.add(btR);

             p2.add(btRAll);

             p2.add(btL);

             p2.add(btLAll);

 

             p3.setLayout(new BorderLayout(0, 5));

             p3.add(listR,"Center");

             p3.add(tfR,"South");

 

             setLayout(new GridLayout(1, 3, 10, 0));

             add(p1);

             add(p2);

             add(p3);

 

             setBounds(300, 200, 300, 500);

             setVisible(true);

       }

 

       public static void main(String[] args) {

             new ListTest();

       }

}


4.

결과)


코드)

 

package com.kitri.awt.design;

import java.awt.*;

public class BaseBall extends Frame{

 

       Panel pC = new Panel();

       Panel pCS = new Panel();

       Panel pE = new Panel();

 

       TextArea ta = new TextArea();

       Label l = new Label("입력",Label.CENTER);

       TextField tf = new TextField();

 

       Button newG = new Button("새게임");

       Button clear = new Button("지우기");

       Button dap = new Button("정답");

       Button fontC = new Button("글자색");

       Button exit = new Button("종료");

 

       public BaseBall() {

 

                   pCS.setLayout(new BorderLayout(10,0));

                   pCS.add(l,"West");

                   pCS.add(tf,"Center");

 

                   pC.setLayout(new BorderLayout(0, 10));

                   pC.add(ta,"Center");

                   pC.add(pCS,"South");

 

                   pE.setLayout(new GridLayout(5, 1, 0, 10));

                   pE.add(newG);

                   pE.add(clear);

                   pE.add(dap);

                   pE.add(fontC);

                   pE.add(exit);

 

                   setLayout(new BorderLayout(10, 0));

                   add(pC,"Center");

                   add(pE,"East");

 

                   setBounds(300, 200, 500, 400);

                   setVisible(true);

 

       }

       public static void main(String[] args) {

             new BaseBall();

       }

}


5.

결과)


코드)

 

package com.kitri.awt.design;

 

 

import java.awt.*;

 

 

import javafx.geometry.HorizontalDirection;

 

 

public class ColorSelector extends Frame{

 

       Panel p1 = new Panel();

       Panel p11 = new Panel();

       Panel p12 = new Panel();

       Panel p13 = new Panel();

       Panel p2 = new Panel();

       Panel p21 = new Panel();

 

       Label lR = new Label("빨강");

       Label lG = new Label("초록");

       Label lB = new Label("파랑");

       //0~255를 찍고 싶다면 막대 두께 10을 고려하여 0~265로 해야함

       Scrollbar sbR = new Scrollbar(Scrollbar.HORIZONTAL, 127, 10, 0, 265);

       Scrollbar sbG = new Scrollbar(Scrollbar.HORIZONTAL, 127, 10, 0, 265);

       Scrollbar sbB = new Scrollbar(Scrollbar.HORIZONTAL, 127, 10, 0, 265);

 

       Panel colorP = new Panel();

 

       Label colorL = new Label();

       Button ok = new Button("선택");

 

       public ColorSelector() {

             super("컬러셀렉터");

 

             p1.setLayout(new GridLayout(5, 1, 0, 20));

             p1.add(new Label());

             p1.add(p11);

             p1.add(p12);

             p1.add(p13);

             p11.setLayout(new BorderLayout(10, 0));

             p12.setLayout(new BorderLayout(10, 0));

             p13.setLayout(new BorderLayout(10, 0));

             lR.setBackground(Color.red);

             lG.setBackground(Color.green);

             lB.setBackground(Color.blue);

             p11.add(lR,"West");

             p11.add(sbR,"Center");

             p12.add(lG,"West");

             p12.add(sbG,"Center");

             p13.add(lB,"West");

             p13.add(sbB,"Center");

 

 

             p2.setLayout(new BorderLayout(0, 10));

             p2.add(colorP,"Center");

             p2.add(p21,"South");

 

             p21.setLayout(new BorderLayout(10, 0));

             p21.add(colorL,"Center");

             p21.add(ok,"East");

             colorL.setBackground(new Color(220,220,220));

 

             setLayout(new GridLayout(1, 2, 10, 0));

             add(p1);

             add(p2);

 

             setBounds(300, 200, 500, 500);

             setVisible(true);

 

             int r = sbR.getValue();

             int g = sbR.getValue();

             int b = sbB.getValue();

             colorP.setBackground(new Color(r,g,b));

             colorL.setText("r = "+r+" g = "+g+" b = "+b);

       }

 

       public static void main(String[] args) {

             new ColorSelector();

       }

}


다음은 기능 추가 내용

 

 

[출처] 빡쏘끼룩

반응형