Power Java 제 19 장배치관리자 이번장에서학습할내용 배치관리자의개요 배치관리자의사용 FlowLayout BorderLayout GridLayout BoxLayout CardLayout 절대위치로배치 컨테이너안에서컴포넌트를배치하는방법에대하여살펴봅시다.
배치관리자 (layout manager) 컨테이너안의각컴포넌트의위치와크기를결정하는작업 [3/70] 상당히다르게보인다. 배치관리자에따라서상당히다르게보인다. [4/70]
배치관리자의종류 BorderLayout BoxLayout CardLayout 배치관리자의종류 FlowLayout GridBagLayout GidL GridLayout
배치관리자의설정 1. 생성자를이용하는방법 JPanel panel = new JPanel(new BorderLayout()); 2. setlayout() 메소드이용 panel.setlayout(new FlowLayout()); 크기와정렬힌트 프로그래머가컴포넌트의크기와힌트를배치관리자에게주고싶은경우에는 setminimumsize(), setpreferredsize(), setmaximumsize() 메소드를사용 ( 예 ) component.setmaximumsize(new Dimension(Integer.MAX_VALUE,Integer.MAX_VALUE));
FlowLayout 컴포넌트들을왼쪽에서오른쪽으로버튼을배치한다. 패널과애플릿의디폴트배치관리자이다. import java.awt.*; import javax.swing.*; FlowLayout 예제 class MyFrame extends JFrame { public MyFrame() { settitle("flowlayouttest"); t") setdefaultcloseoperation(jframe.exit_on_close); JPanel panel; // 패널을생성하고배치관리자를 FlowLayout으로설정 panel = new JPanel(); panel.setlayout(new FlowLayout(FlowLayout.CENTER)); // 패널에버튼을생성하여추가 panel.add(new JButton("Button1")); panel.add(new JButton("Button2")); panel.add(new JButton("Button3")); panel.add(new JButton("B4")); panel.add(new JButton("Long Button5")); add(panel); pack(); setvisible(true);
BorderLayout BorderLayout 은 5 개의영역으로구분하고각각의영역에컴포넌트를배치 PAGE_START ( 또는 NORTH) PAGE_END ( 또는 SOUTH) LINE_START ( 또는 WEST) LINE_END END ( 또는 EAST) CENTER import java.awt.*; import javax.swing.*; BorderLayout 예제 class MyFrame extends JFrame { public MyFrame() { settitle("borderlayouttest"); t") setdefaultcloseoperation(jframe.exit_on_close); // 프레임은디폴트로 BorderLayout 이므로사실은불필요 setlayout(new BorderLayout()); // 버튼을추가한다. add(new JButton("Center"), t ") BorderLayout.CENTER); tcenter) add(new JButton("Line Start"), BorderLayout.LINE_START); add(new JButton("Line End"), BorderLayout.LINE_END); add(new JButton("Page Start"), BorderLayout.PAGE_START); _ add(new JButton("Page End"), BorderLayout.PAGE_END); pack(); setvisible(true);
GridLayout GridLayout 은컴포넌트들을격자모양으로배치한다. import java.awt.*; import javax.swing.*; GridLayout 예제 class MyFrame extends JFrame { public MyFrame() { settitle("gridlayouttest"); idl t") setdefaultcloseoperation(jframe.exit_on_close); setlayout(new GridLayout(0, 3)); // 3 개의열과필요한만큼의행 add(new JButton("Button1")); add(new JButton("Button2")); add(new JButton("Button3")); add(new JButton("B4")); add(new JButton("Long Button5")); pack(); setvisible(true);
BoxLayout BoxLayout은컴포넌트를다른컴포넌트위에쌓거나컴포넌트를하나의행에배치할수있다. BoxLayout은 FlowLayout의하나의버전으로간주할수있으나좀더기능이많다. import java.awt.*; import java.awt.event.*; import javax.swing.*; BoxLayout 예제 class MyFrame extends JFrame { public MyFrame() { settitle("boxlayouttest"); setdefaultcloseoperation(jframe.exit_on_close); JPanel panel = new JPanel(); // Y 축방향으로컴포넌트를쌓는다. panel.setlayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); makebutton(panel, "Button1"); makebutton(panel, "Button2"); makebutton(panel, "Button3"); makebutton(panel, "B4"); makebutton(panel, "Long Button5"); add(panel); pack(); setvisible(true);
GridLayout 예제 private void makebutton(jpanel panel, String text) { JButton button = new JButton(text); button.setalignmentx(component.center_alignment); ALIGNMENT); panel.add(button); CardLayout CardLayout 클래스는여러개의카드가쌓여있는형태로컴포넌트를배치
import java.awt.*; import java.awt.event.*; import javax.swing.*; CardLayout 예제 class MyFrame extends JFrame implements ActionListener { JPanel panel; Cards cards; public MyFrame() { settitle("cardlayouttest"); setsize(400, 200); setdefaultcloseoperation(jframe.exit_on_close); panel = new JPanel(new GridLayout(0, 5, 10, 0)); addbutton("<<", " panel); addbutton("<", panel); addbutton(">", panel); addbutton(">>", panel); addbutton(" 종료 ", panel); add(panel, "North"); cards = new Cards(); add(cards, "Center"); setvisible(true); CardLayout 예제 void addbutton(string str, Container target) { JButton button = new JButton(str); button.addactionlistener(this); target.add(button); private class Cards extends JPanel { CardLayout layout; public Cards() { layout = new CardLayout(); setlayout(layout); for (int i = 1; i <= 10; i++) { add(new JButton(" 현재카드의번호는 " + i + " 입니다 "), "Center"); ")
CardLayout 예제 public void actionperformed(actionevent e) { if (e.getactioncommand().equals(" 종료 ")) { System.exit(0); else if (e.getactioncommand().equals("<<")) { cards.layout.first(cards); else if (e.getactioncommand().equals("<")) { cards.layout.previous(cards); d else if (e.getactioncommand().equals(">")) { cards.layout.next(cards); else if (e.getactioncommand().equals(">>")) () ( )) { cards.layout.last(cards); public class CardTest { public static void main(string args[]) { MyFrame f=new MyFrame(); GridLayout 예제
어떤배치관리자를선택할것인가? 컴포넌트를가능한크게나타내고싶은경우 GridLayout 이나 BorderLayout 을사용 몇개의컴포넌트를자연스러운크기로한줄로나타내고싶은경우 FlowLayout을사용하던지 BoxLayout을사용한다. 몇개의컴포넌트를행과열로동일한크기로나타내고싶은경우 GridLayout을사용하여야한다. 몇개의컴포넌트를행과열로나타내는데각컴포넌트의크기가다르거나간격, 정렬방식을다르게내고싶은경우 BoxLayout 을사용하면된다. 절대위치로배치하기 1. 배치관리자를 null로설정한다. setlayout(null); 2. add() 메소드를사용하여컴포넌트를컨테이너에추가한다. Button b = Button("Absolute Position Button"); add(b); 3. setbounds() 메소드를사용하여절대위치와크기를지정한다. b.setbounds(x, y, w, h); 4. 컴포넌트의 repaint() 메소드를호출한다. b.repaint();
import java.awt.*; import java.awt.event.*; import javax.swing.*; class MyFrame extends JFrame { JButton b1; private JButton b2, b3; CardLayout 예제 public MyFrame() { settitle("absolute Position Test"); setdefaultcloseoperation(jframe.exit_on_close); setsize(300, 200); JPanel p = new JPanel(); p.setlayout(null); t( b1 = new JButton("Button #1"); p.add(b1); b2 = new JButton("Button #2"); p.add(b2); b3 = new JButton("Button #3"); p.add(b3); CardLayout 예제 b1.setbounds(20, 5, 95, 30); b2.setbounds(55, 45, 105, 70); b3.setbounds(180, 15, 105, 90); add(p); setvisible(true); public class AbsoluteTest { public static void main(string args[]) { MyFrame f=new MyFrame();
Q & A