JSplitPane을 이용하면 컴포넌트를 분할할 수 있습니다. 상하를 나누거나 좌우로 나눌 수 있으며 이는 객체 생성할때 지정하거나 메소드를 이용하여 작업할 수 있습니다.
<<사용방법>>
1. JSplitPane객체를 생성합니다.
API를 열고 javax.swing패키지를 선택하면 JSplitPane의 생성자를 확인할 수 있습니다.
1) 기본생성자를 이용하여 생성
- 기본생성자를 이용하여 생성한 후 작업하면 메소드를 이용하여 컴포넌트의 위치를 셋팅할
수 있다.
JSplitPane jsp = new JSplitPane();
2) 두번째 생성자(int의 방향에 관련된 매개변수를 받는 생성자)
- 방향에 관련된 상수를 매개변수로 지정하여 생성한다.
JSplitPane.HORIZONTAL_SPLIT : 좌우에 배치
JSplitPane.VERTICAL_SPLIT : 상하로 배치
JSplitPane jsp = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT )
2. JSplitPane객체를 생성한 후 각각의 영역에 컴포넌트를 배치하면 된다.
jsp.setLeftComponent(컴포넌트) : JSplitPane의 왼쪽영역에 배치
jsp.setRightComponent(컴포넌트) : JSplitPane의 오른쪽영역에 배치
jsp.setTopComponent(컴포넌트):JSplitPane의 위쪽 영역에 배치
jsp.setBottomComponent(컴포넌트):JSplitPane의 아래쪽 영역에 배치
3. 샘플코드
아래 코드를 실행하면 다음과 같은 결과가 나타납니다.
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JSplitPaneTest extends JFrame{
private JTextArea jta = new JTextArea();
private JSplitPane jsp = new JSplitPane();
private BorderLayout bl = new BorderLayout();
JLabel lblId = new JLabel("아이디:");
JLabel lblPass = new JLabel("패스워드:");
JLabel lblName = new JLabel("성 명:");
JTextField txtId = new JTextField(10);
JTextField txtPass = new JTextField(10);
JTextField txtName = new JTextField(10);
JButton btnInsert = new JButton("가입");
public JSplitPaneTest(String title) {
super(title);
this.init();
this.start();
this.setSize(400,200);
//JFrame이 보여질 위치를 Dimension클래스를 이용하여 스크린사이즈를
//계산한 후 2로 나누어 중앙에 보여지도록 setLocation메소드를 이용하여
//적용
Dimension screen =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = this.getSize();
//x좌표값을 계산하는 코드로 전체 스크린 width를 2로 나누고 JFrame의
//너비를 2로 나누어 뺀 값을 적용함
int xpos =
(int)(screen.getWidth() / 2 - frm.getWidth() / 2);
int ypos = (int)(screen.getHeight() / 2 - frm.getHeight() / 2);
this.setLocation(xpos, ypos);
//전체 프레임의 크기를 조절할 수 없도록 처리
this.setResizable(false);
this.setVisible(true);
}
private void init(){
Container con = this.getContentPane();
con.setLayout(bl);
JPanel p = new JPanel(new GridLayout(3, 2));
p.add(lblId);
p.add(txtId);
p.add(lblPass);
p.add(txtPass);
p.add(lblName);
p.add(txtName);
//패널에 setBorder메소드를 이용하여 선을 그려주고 있는 것을 볼 수 있다.
p.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("로그인"),
BorderFactory.createEmptyBorder(5,5,5,5)));
//JSplitPane의 왼쪽 부분에 로그인 패널을 붙인다.
jsp.setLeftComponent(p); //컴포넌트 왼쪽에 로그인패널 추가하기
jsp.setRightComponent(jta); //컴포넌트 오른쪽에 텍스트 에어리어 삽입
con.add("Center",jsp);
con.add(btnInsert,BorderLayout.SOUTH);
}
private void start(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
new JSplitPaneTest("Text");
}
}
'프로그래밍언어 > Java' 카테고리의 다른 글
JTable을 셀을 클릭했을때 이벤트 처리하기 (0) | 2019.06.25 |
---|---|
JTable의 진실 (0) | 2019.06.25 |
swing 이벤트 개요 (0) | 2019.06.14 |
자바플랫폼의 개요 및 특징 (0) | 2019.06.04 |
환경변수설정 (0) | 2019.06.04 |