2013년 12월 25일 수요일

SWT - 버튼(Button)




버튼(Button)



  Shell과 Label과 같이, GUI에서 Button의 외양은 생성할 때 사용하는 스타일 비트값에 의존합니다.



○ 푸쉬 버튼과 SWT.PUSH


  SWT.PUSH 스타일은 Button 클래스의 기본 스타일입니다. Button의 텍스트는 setText() 메서드로 정의하며, getText() 메서드로 Button의 텍스트(문자열)을 얻을 수 있습니다. Button의 생성자는 SWT.LEFT, SWT.CENTER, SWT.RIGHT 스타일 중 하나와 SWT.PUSH| (파이프) 연산자를 이용하여 결합시킬 수 있습니다. SWT.FLAT 스타일을 이용해 평평한 모양의 Button도 생성할 수 있습니다.



<예제 코드>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.swtjface.ch3;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
/**
 * 2010.05.20
 * Button Exaple 
 * @author cremazer
 */
public class ButtonTest {
 public static void main(String[] args) {
  //할당과 초기화
  Display display = new Display();
  Shell shell = new Shell(display);
  shell.setText("Button Example");
  shell.setSize(400, 250);
  
  
  //쉘에 추가하기
  Button bt = new Button(shell, SWT.PUSH);
  bt.setText("PUSH");
  bt.setBounds(40, 60, 100, 20);
//  bt.pack();
  
  //GUI 작동
//  shell.pack();
  shell.open();
  while(!shell.isDisposed()){
   if(!display.readAndDispatch()){
    display.sleep();    
   }
  }
  display.dispose();
 }
}



<결과>







○ 화살표 버튼과 SWt.ARROW


  화살표 버튼은 SWT.ARROWSWT.UP, SWT.DOWN, SWT.LEFT, SWT.RIGHT 상수를 결합하여 화살표 방향을 명시할 수 있습니다. SWT.FLAT 스타일을 사용하여 평평한 형태로 나타낼 수 있습니다. setImage() 메서드를 사용하여 표시하고자 하는 Image 객체를 덧붙일 수 있습니다.



<예제 코드>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.swtjface.ch3;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
/**
 * 2010.05.20
 * Button Exaple 
 * @author cremazer
 */
public class ButtonTest {
 public static void main(String[] args) {
  //할당과 초기화
  Display display = new Display();
  Shell shell = new Shell(display);
  shell.setText("Button Example");
  shell.setSize(400, 250);
  
  
  //쉘에 추가하기
  Button bt = new Button(shell, SWT.ARROW | SWT.RIGHT);
  bt.setBounds(0, 0, 50, 50);
  
  Button bt2 = new Button(shell, SWT.ARROW | SWT.LEFT);
  bt2.setBounds(50, 0, 50, 50);
  
  Button bt3 = new Button(shell, SWT.ARROW | SWT.UP);
  bt3.setBounds(100, 0, 50, 50);
  
  Button bt4 = new Button(shell, SWT.ARROW | SWT.DOWN);
  bt4.setBounds(150, 0, 50, 50);
  
  //GUI 작동
//  shell.pack();
  shell.open();
  while(!shell.isDisposed()){
   if(!display.readAndDispatch()){
    display.sleep();    
   }
  }
  display.dispose();
 }
}



<결과>







○ 토글 버튼과 SWT.TOGGLE


  토글 버튼은 버튼을 눌린 상태를 유지하는 기능을 제공합니다. 버튼을 생성할 때, SWT.TOGGLE 스타일을 사용하며, 특정처리를 수행하는 대신 애플리케이션의 특정 상태 정보를 유지하는데 사용합니다. setSelection(boolean) 메서드를 사용하여 버튼의 상태를 설정합니다. 눌린상태는 true 이고, 눌리지 않은 상태는 false값으로 설정합니다. 



<예제 코드>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.swtjface.ch3;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
/**
 * 2010.05.20
 * Button Exaple 
 * @author cremazer
 */
public class ButtonTest {
 public static void main(String[] args) {
  //할당과 초기화
  Display display = new Display();
  Shell shell = new Shell(display);
  shell.setText("Button Example");
  shell.setSize(400, 250);
  
  
  //쉘에 추가하기
  Button bt = new Button(shell, SWT.TOGGLE);
  bt.setText("true");
  bt.setSelection(true);  
  bt.setBounds(0, 0, 50, 50);
  
  Button bt2 = new Button(shell, SWT.TOGGLE);
  bt2.setText("fasle");
  bt2.setSelection(false);
  bt2.setBounds(50, 0, 50, 50);
  
  
  
  //GUI 작동
//  shell.pack();
  shell.open();
  while(!shell.isDisposed()){
   if(!display.readAndDispatch()){
    display.sleep();    
   }
  }
  display.dispose();
 }
}


<결과>

 푸른색으로 표시된 버튼이 현재 토글상태로 눌린상태이다.(true)



○ 체크 버튼과 SWT.CHECK


  체크버튼은 일반적으로 여러 개를 같이 사용합니다. 사용자는 체크 버튼을 선택해서 하나 이상의 선택 사항을 고를 수 있습니다. 체크 버튼은 배열로 만들 것을 권장합니다.



<예제 코드>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.swtjface.ch3;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
/**
 * 2010.05.20
 * Button Exaple 
 * @author cremazer
 */
public class ButtonTest {
 public static void main(String[] args) {
  //할당과 초기화
  Display display = new Display();
  Shell shell = new Shell(display);
  shell.setText("Button Example");
  shell.setSize(400, 250);
  
  
  //쉘에 추가하기
  Button[] checks = new Button[3];
  
  checks[0] = new Button(shell, SWT.CHECK);
  checks[0].setText("Selected");
  checks[0].setSelection(true);
  checks[0].setLocation(0, 40);
  checks[0].pack();
  
  checks[1] = new Button(shell, SWT.CHECK);
  checks[1].setText("Not Selected");
  checks[1].setLocation(100, 40);
  checks[1].pack();
  
  checks[2] = new Button(shell, SWT.CHECK);
  checks[2].setText("Also Selected");
  checks[2].setSelection(true);
  checks[2].setLocation(200, 40);
  checks[2].pack();
  
  
  
  //GUI 작동
//  shell.pack();
  shell.open();
  while(!shell.isDisposed()){
   if(!display.readAndDispatch()){
    display.sleep();    
   }
  }
  display.dispose();
 }
}


<결과>





○ 라디오 버튼과 SWT.RADIO


  라디오 버튼은 여러 버튼 중에서 오직 한개만 선택할 수 있습니다. 하나를 선택하면, 다른 항목들은 자동으로 선택이 해제됩니다. 버튼을 생성할 때는 SWT.RADIO 스타일을 이용합니다.



<에제 코드>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.swtjface.ch3;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
/**
 * 2010.05.20
 * Button Exaple 
 * @author cremazer
 */
public class ButtonTest {
 public static void main(String[] args) {
  //할당과 초기화
  Display display = new Display();
  Shell shell = new Shell(display);
  shell.setText("Button Example");
  shell.setSize(400, 250);
  
  
  //쉘에 추가하기
  Button[] radios = new Button[3];
  
  radios[0] = new Button(shell, SWT.RADIO);
  radios[0].setText("choice1");
  radios[0].setSelection(true);
  radios[0].setLocation(0, 10);
  radios[0].pack();
  
  radios[1] = new Button(shell, SWT.RADIO);
  radios[1].setText("choice2");
  radios[1].setLocation(0, 40);
  radios[1].pack();
  
  radios[2] = new Button(shell, SWT.RADIO);
  radios[2].setText("choice3");
  radios[2].setLocation(0, 70);
  radios[2].pack();
  
  
  
  //GUI 작동
//  shell.pack();
  shell.open();
  while(!shell.isDisposed()){
   if(!display.readAndDispatch()){
    display.sleep();    
   }
  }
  display.dispose();
 }
}


<결과>







RadioGroupFieldEditor를 이용한 라디오 버튼의 포함  


대부분 툴셋이 라디오 버튼의 그룹을 모으고 관리하기 위한 구성 요소를 제공합니다. 

생성자의 첫 번재 변수는 편집기에서 반환하는 값의 유형을 위한 이름을 제공합니다. 

두 번째와 세 번째 매개 변수는 그룹의 레이블과 열 개수를 명시합니다. 

네 번째는 옵션 이름을 결합되는 값과 함께 설정합니다. 

다섯 번째 매개 변수는 이 편집기를 Shell 객체에 추가합니다. 

마지막 변수는 라디오 버튼을 Group 객체에 연동할지에 대해 명시합니다. 



<예제 코드>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.swtjface.ch3; 
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
/**
 * 2010.05.20
 * Button Exaple 
 * @author cremazer
 */
public class ButtonTest {
 public static void main(String[] args) {
  //할당과 초기화
  Display display = new Display();
  Shell shell = new Shell(display);
  shell.setText("Button Example");
  shell.setSize(400, 250);
  
  
  //쉘에 추가하기
  RadioGroupFieldEditor rgfe = new RadioGroupFieldEditor("UserChoice"
                                                         "Choose an option", 1, 
    new String[][]{{"Choice1""ch1"},
          {"Choice2""ch2"},
          {"Choice3""ch3"}}, shell, true);
  
  //GUI 작동
//  shell.pack();
  shell.open();
  while(!shell.isDisposed()){
   if(!display.readAndDispatch()){
    display.sleep();    
   }
  }
  display.dispose();
 }
}


<결과>









참고 서적 : SWT/JFace in Action



댓글 없음:

댓글 쓰기