[Java] Window Programming - <Layout>
- BACKEND/Java
- 2017. 11. 6. 09:43
● Layout 1) Layout Manager -FlowLayout 순서대로 배치하는 기능, 화면이 커지거나 작아질때 알아서 조정해주는 기능을 가진다. -BorderLayout (왼쪽,오른쪽,위쪽 아래쪽,중앙) 테두리를 기준으로 배치하는 기능, 위치를 지정해줘야 한다. -GridLayout 몇행 몇열로 설정해서 테이블을 만들어준다. -GridBagLayout 불규칙하게 테이블 만들어준다. -CardLayout 카드가 셔플되는 것 처럼 만들어준다. |
FlowLayout 예제
package layout; import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; public class FlowLayoutTest extends Frame{ Button b1, b2, b3, b4, b5;
public FlowLayoutTest() { // setLayout(new FlowLayout()); //manager 생성 -> FlowLayout 매니저 인스턴스 생성 b1 = new Button("첫번째 버튼"); b2 = new Button("두번째 버튼"); b3 = new Button("세번째 버튼"); b4 = new Button("네번째 버튼"); b5 = new Button("다섯번째 버튼");
add(b1); add(b2); add(b3); add(b4); add(b5); }
public static void main(String[] args) { FlowLayoutTest test = new FlowLayoutTest(); test.setSize(300, 400); test.setVisible(true); } } |
실행결과↓
BorderLayout 예제
package layout; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Frame; public class BorderLayoutTest extends Frame{ Button b1, b2, b3, b4, b5;
public BorderLayoutTest() {
setLayout(new BorderLayout());
b1 = new Button("첫번째 버튼"); b2 = new Button("두번째 버튼"); b3 = new Button("세번째 버튼"); b4 = new Button("네번째 버튼"); b5 = new Button("다섯번째 버튼");
// West, East, South, North, Center로 지정해줘야함 add("North",b1); add("South",b2); add("East",b3); add("West",b4); add("Center",b5); }
public static void main(String[] args) { BorderLayoutTest test = new BorderLayoutTest(); test.setSize(300, 400); test.setVisible(true); } } |
실행결과↓
GridLayout예제
package layout; import java.awt.Button; import java.awt.Frame; import java.awt.GridLayout; public class GridLayoutTest extends Frame{ Button b1, b2, b3, b4, b5;
public GridLayoutTest() { setLayout(new GridLayout(2,3)); //2행 3열
b1 = new Button("첫번째 버튼"); b2 = new Button("두번째 버튼"); b3 = new Button("세번째 버튼"); b4 = new Button("네번째 버튼"); b5 = new Button("다섯번째 버튼");
add(b1); add(b2); add(b3); add(b4); add(b5); }
public static void main(String[] args) { GridLayoutTest test = new GridLayoutTest(); test.setSize(300, 400); test.setVisible(true); } } |
실행결과↓
CardLayout예제
package layout; import java.awt.Button; import java.awt.CardLayout; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class CardLayoutTest extends Frame implements ActionListener{ // ActionListener implements받아서 ActionEvent 오버라이딩한다. Button b1, b2, b3, b4, b5; CardLayout card; //오버라이딩 한 곳에서도 사용가능하도록 인스턴스 변수로 설정
public CardLayoutTest() { //카드 셔플 기능 card = new CardLayout(); setLayout(card);
b1 = new Button("첫번째 버튼"); b2 = new Button("두번째 버튼"); b3 = new Button("세번째 버튼"); b4 = new Button("네번째 버튼"); b5 = new Button("다섯번째 버튼");
add(b1); add(b2); add(b3); add(b4); add(b5);
b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); }
@Override public void actionPerformed(ActionEvent arg0) { card.next(this); // 카드를 다음으로 넘기는 기능 } public static void main(String[] args) { CardLayoutTest test = new CardLayoutTest(); test.setSize(300, 400); test.setVisible(true); } } |
실행결과↓
버튼을 누르면 버튼이 카드셔플되는 것 처럼 바뀐다.
'BACKEND > Java' 카테고리의 다른 글
[Java] ItemEvent/TextEvent 예제 (0) | 2017.11.07 |
---|---|
[Java] Window Programing - <Event> (0) | 2017.11.06 |
[Java] Window Programming (0) | 2017.11.06 |
[Java] IO 예제 (0) | 2017.11.02 |
[Java] Thread 예제 (0) | 2017.11.02 |