자바 JFC 구성 및 일반 클래스 활용 Toggle, Radio Button

자바 JFC 구성 및 일반 클래스 활용 Toggle, Radio Button


Toggle버튼
토글이란 옛날 선풍기의 버튼을 생각하면 되는데 하나의 버튼을 눌렀을 때 다른버튼들은
모두 누른상태가 해지되어 버리는 형태를 말한다 물론 이것이 실행되기 위해서는
어떤 버튼들을 그룹으로 묶을 것인가 하는 것이 중요하다

ex )

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
53
54
55
56
57
import java.awt.*;
import javax.swing.*;

class Round22_Ex01_Sub extends JFrame {// JFrame을 상속받는다
private Container con;
private GridLayout gl = new GridLayout(2, 2, 5, 5);
private JToggleButton tb = new JToggleButton("1", true);
private JToggleButton tb1 = new JToggleButton("2", false);
private JToggleButton tb2 = new JToggleButton("3", tfalse);
private JToggleButton tb3 = new JToggleButton("4", false);
// Toggle의 그룹이다 true는 초기에 눌려저있는값 false는 빈값이다 .
private ButtonGroup bg = new ButtonGroup();

// 위의 토글을 하나의 그룹으로 묶는다
public Round22_Ex01_Sub() {
super("제목");
this.init();
this.start();
im = new ImageIcon("title.gif");
this.setIconImage(im.getImage());
this.setSize(300, 200);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension di = tk.getScreenSize();
Dimension di1 = this.getSize();
int xpos = ((int) di.getWidth() / 2 - (int) di1.getWidth() / 2);
int ypos = ((int) di.getHeight() / 2 - (int) di1.getHeight() / 2);
this.setLocation(xpos, ypos);
this.setVisible(true);
}

public void init() {
con = this.getContentPane();// 다중 Panel에서의 기본 작업영역 획득
con.setLayout(gl);
bg.add(tb);
bg.add(tb1);
bg.add(tb2);
bg.add(tb3);
// 토글을 하나의 그룹으로 묶는다
con.add(tb);
con.add(tb1);
con.add(tb2);
con.add(tb3);
// 폼 구성 영역 (초기화 영역)
}

public void start() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Frame의 x버튼을 눌렀을 때의 Event
// 이벤트나 기타 액션의 영역
}
}

public class Round22_Ex01 {
public static void main(String[] ar) {
Round22_Ex01_Sub es = new Round22_Ex01_Sub();
}
}

Radio Button
위의 토글과 마찬가지로 RadioButton에도 그대로 통용되는 것이다
RadioButton은 AWT의 CheckboxGroup으로 묶여 있는 Checkbox라고 생각하면 된다.

ex )

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
53
54
55
56
57
58
59
import java.awt.*;
import javax.swing.*;

class Round22_Ex01_Sub extends JFrame {// JFrame을 상속받는다
private Container con;
private GridLayout gl = new GridLayout(2, 2, 5, 5);
private JRadioButton cb1 = new JRadioButton("1", true);
private JRadioButton cb2 = new JRadioButton("2");
private JRadioButton cb3 = new JRadioButton("3");
private JRadioButton cb4 = new JRadioButton("4", true);
// RadioButton 의 그룹이다 true는 초기에 눌려저있는값 이다 .
private ButtonGroup bg = new ButtonGroup();
// 위의 버튼을 하나의 그룹으로 묶는다
private ButtonGroup bg1 = new ButtonGroup();

// 위의 버튼을 하나의 그룹으로 묶는다
public Round22_Ex01_Sub() {
super("제목");
this.init();
this.start();
im = new ImageIcon("title.gif");
this.setIconImage(im.getImage());
this.setSize(300, 200);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension di = tk.getScreenSize();
Dimension di1 = this.getSize();
int xpos = ((int) di.getWidth() / 2 - (int) di1.getWidth() / 2);
int ypos = ((int) di.getHeight() / 2 - (int) di1.getHeight() / 2);
this.setLocation(xpos, ypos);
this.setVisible(true);
}

public void init() {
con = this.getContentPane();// 다중 Panel에서의 기본 작업영역 획득
con.setLayout(gl);
bg.add(cb1);
bg.add(cb2);
bg1.add(cb3);
bg1.add(cb4);
// 버튼 을 두개의 그룹으로 묶는다 bg, bg1
con.add(cb1);
con.add(cb2);
con.add(cb3);
con.add(cb4);
// 폼 구성 영역 (초기화 영역)
}

public void start() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Frame의 x버튼을 눌렀을 때의 Event
// 이벤트나 기타 액션의 영역
}
}

public class Round22_Ex01 {
public static void main(String[] ar) {
Round22_Ex01_Sub es = new Round22_Ex01_Sub();
}
}

-다르게 관리하기 위해 두개의 ButtonGroup을 나누었다

Share