2013년 7월 3일 수요일

(130703) 8일차 PanelTest.java (JPanel 테스트)

 - Panel
 : component를 위한 공간을 제공한다.
 : 고유한 Layout을 갖는 sub-panel을 혀용한다.
 : 생성된 후 Window 나 Frame에 붙여져야만 보여질수 있다.
 : Default Layout managerFlowLayout(순서대로 일렬 부착)을 갖는다.
 : setLayout() 을 이용해 Layout을 설정할수 있다.
 : this.setLayout(new FlowLayout());


 - JframeTest2.java 소스
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JButton;

public class JframeTest2 extends JFrame {
JButton northbutton, southbutton, eastbutton, westbutton, centerbutton;

public JframeTest2(){
// 닫기 버튼을 누르면 메모리에서 해제
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// jframe 속성을 패널의 기본인 Flowlayout으로 변경
this.setLayout(new FlowLayout());
// 객체 생성
northbutton = new JButton("북쪽");
southbutton = new JButton("남쪽");
eastbutton = new JButton("동쪽");
westbutton = new JButton("서쪽");
centerbutton = new JButton("중앙");
// 컨테이너에 컴포넌트들을 부착
this.add("North", northbutton);
this.add("South", southbutton);
this.add("East", eastbutton);
this.add("West", westbutton);
this.add(centerbutton); // 위치를 작성하지 않으면 기본으로 Center에 들어감
this.setSize(400, 300);
this.setVisible(true);
}

public static void main(String[] args) {
new JframeTest2();
}
}


 - 결과




















 - PanelTest.java 소스
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;

public class PanelTest extends JFrame {

JButton northbutton, southbutton, eastbutton, westbutton, centerbutton;
JPanel panel; // 패널

public PanelTest(){

// 닫기 버튼을 누르면 메모리에서 해제
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel = new JPanel();

// 객체 생성
northbutton = new JButton("북쪽");
southbutton = new JButton("남쪽");
eastbutton = new JButton("동쪽");
westbutton = new JButton("서쪽");
centerbutton = new JButton("중앙");

// 패널에 버튼들을 추가
panel.add(northbutton);
panel.add(southbutton);
panel.add(eastbutton);
panel.add(westbutton);
panel.add(centerbutton);

this.add("South", panel); // 서쪽에 다 붙임

this.setSize(400, 300);
this.setVisible(true);
}

public static void main(String[] args) {
new PanelTest();
}
}


 - 결과