티스토리 뷰
BorderLayout
import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; class MyFrame6 extends JFrame{ JButton b1 = new JButton("버튼1"); JButton b2 = new JButton("버튼2"); JButton b3 = new JButton("버튼3"); JButton b4 = new JButton("버튼4"); JButton b5 = new JButton("버튼5"); public MyFrame6(){ // 배치관리자 BorderLayout : 컴포넌트를 동,서,남,북,중앙에 배치 // 컨테이너에 add를 할 때, 위치를 지정해야함 // 프레임의 기본 배치관리자 // setLayout -> 배치 관리자를 지정 // 배치관리자 클래스의 인스턴스를 인수로 함 setLayout(new BorderLayout()); add(b1, BorderLayout.EAST); // == LINE_END add(b2, BorderLayout.WEST); // == LINE_START add(b3, BorderLayout.SOUTH); // == PAGE_END add(b4, BorderLayout.NORTH); // == PAGE_START add(b5, BorderLayout.CENTER); setSize(500,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } } public class 배치관리자1 { public static void main(String[] args) { new MyFrame6(); } }
import java.awt.Color; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; class MyFrame7 extends JFrame{ JButton b1 = new JButton("버튼1"); JButton b2 = new JButton("버튼2"); JButton b3 = new JButton("버튼3"); JButton b4 = new JButton("버튼4"); JButton b5 = new JButton("버튼5"); JPanel p = new JPanel(); public MyFrame7(){ // 배치관리자 FlowLayout : 컴포넌트들의 add한 순서대로 나열, 패널의 기본 레이아웃 //setLayout(new FlowLayout()); -> 기본값 : 가운데정렬 p.setBackground(Color.BLUE); p.setLayout( new FlowLayout( FlowLayout.RIGHT ) ); // 오른쪽정렬 add(p); p.add(b1); p.add(b2); p.add(b4); p.add(b5); setSize(500,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } } public class 배치관리자2 { public static void main(String[] args) { new MyFrame7(); } }
GridLayout
import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; class MyFrame8 extends JFrame{ JButton b1 = new JButton("버튼1"); JButton b2 = new JButton("버튼2"); JButton b3 = new JButton("버튼3"); JButton b4 = new JButton("버튼4"); JButton b5 = new JButton("버튼5"); JButton b6 = new JButton("버튼5"); public MyFrame8(){ // 배치관리자 GridLayout // 생성자의 인수로 행과 열을 지정하여 컴포넌트를 배치 setLayout( new GridLayout(2,3) ); add(b1); add(b2); add(b3); add(b4); add(b5); add(b6); setSize(500,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } } public class 배치관리자3 { public static void main(String[] args) { new MyFrame8(); } }
import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; class MyFrame9 extends JFrame{ JButton b1 = new JButton("버튼1"); JButton b2 = new JButton("버튼2"); JButton b3 = new JButton("버튼3"); JButton b4 = new JButton("버튼4"); JButton b5 = new JButton("버튼5"); JButton b6 = new JButton("버튼5"); JPanel p = new JPanel(); public MyFrame9(){ // BoxLayout : 패널에다가만 가능 // 컴포넌트를 가로 또는 세로 방향으로 컨테이너에 배치 // 컨테이너는 패널이어야 함 p.setLayout( new BoxLayout(p, BoxLayout.Y_AXIS) ); add(p); p.add(b1); p.add(b2); p.add(b3); p.add(b4); p.add(b5); p.add(b6); setSize(500,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } } public class 배치관리자4 { public static void main(String[] args) { new MyFrame9(); } }
절대위치 지정
import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; // 절대위치로 컴포넌트를 배치하는 방법 // 1) Layout을 null로 지정하고 // 2) setBounds()메소드로 위치와 크기를 지정 class MyFrame10 extends JFrame{ JButton b1 = new JButton("버튼1"); JButton b2 = new JButton("버튼2"); public MyFrame10(){ setLayout(null); // 기본 레이아웃(FlowLayout) 해제 // setBounds : 위치와 크기를 동시에 지정하는 메소드 setBounds(700, 150, 300, 200); // setSize(300,200); // setLocation(700, 150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); add(b1); add(b2); b1.setBounds(0, 0, 100, 100); b2.setBounds(100, 0, 100, 100); } } public class 배치관리자5 { public static void main(String[] args) { new MyFrame10(); } }
'JAVA > PRIMER' 카테고리의 다른 글
[JAVA] GUI - JTextField, JTextArea (0) | 2018.04.19 |
---|---|
[JAVA] GUI - 이벤트 (0) | 2018.04.19 |
[JAVA] GUI - JPanel, JLabel, JButton (0) | 2018.04.19 |
[JAVA] GUI - JFrame (0) | 2018.04.19 |
[JAVA] 파일입출력 - FileInputStream, FileOutputStream (0) | 2018.04.19 |
댓글