Main화면에서 버튼을 누르면 화면1, 화면2로 전환되는 프레임을 만들기.
1. Main 설계
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
|
public class Main extends JFrame {
public Main() {
setTitle("화면 전환 예제");
JPanel container = new JPanel();
JButton login = new JButton("로그인");
JButton join = new JButton("회원가입");
container.add(login); container.add(join);
add(container);
setBounds(200, 200, 300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new LoginScreen();
dispose(); //현재의 JFrame을 종료시키는 메서드
}
});
join.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new JoinScreen();
dispose();
}
});
}
public static void main(String[] args) {
new Main();
}
}
|
cs |
dispose(); 라는 메서드를 새로 알게 되었다.
로그인을 누르면 이런 화면이 나오게 구성할 것이다.
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
public class LoginScreen extends JFrame {
public LoginScreen() {
setTitle("로그인 페이지");
JPanel title = new JPanel();
JLabel login = new JLabel("로그인 화면");
login.setForeground(new Color(5, 0 , 153));
login.setFont(new Font("맑은 고딕", Font.BOLD, 25));
title.add(login);
JPanel container1 = new JPanel(new GridLayout(3, 2)); //그리드 레이아웃으로 구성.
JPanel idPane1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JLabel label1 = new JLabel("아이디: ", JLabel.CENTER);
idPane1.add(label1);
JPanel idPane2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JTextField jtf1 = new JTextField(10);
idPane2.add(jtf1);
container1.add(idPane1); container1.add(idPane2);
JPanel pwdPane1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JLabel label2 = new JLabel("비밀번호: ", JLabel.CENTER);
pwdPane1.add(label2);
JPanel pwdPane2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPasswordField jtf2 = new JPasswordField(10);
pwdPane2.add(jtf2);
container1.add(pwdPane1); container1.add(pwdPane2);
JPanel loginPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton jLogin = new JButton("로그인");
JPanel joinPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JButton join = new JButton("회원가입");
loginPanel.add(jLogin); joinPanel.add(join);
container1.add(loginPanel); container1.add(joinPanel);
JPanel container2 = new JPanel();
container2.setLayout(new FlowLayout());
container2.add(container1);
setLayout(new BorderLayout());
add(title, BorderLayout.NORTH);
add(container2, BorderLayout.CENTER);
setBounds(200, 200, 300, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
jLogin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String myId = jtf1.getText();
String myPwd = new String(jtf2.getPassword());
JOptionPane.showMessageDialog(null, "아이디 : " + myId + ", 비밀번호 : " + myPwd);
}
});
join.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new JoinScreen();
dispose();
}
});
}
}
|
cs |
.getPassword() 메서드는 char로 반환되기 때문에 new String()을 통해 String 타입으로 바꿔줌.
회원가입 버튼을 누르면 이런 창으로 넘어간다.
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
public class JoinScreen extends JFrame {
public JoinScreen() {
setTitle("회원가입 화면");
JLabel title = new JLabel("회원가입", JLabel.CENTER);
title.setForeground(new Color(5, 0, 153));
title.setFont(new Font("맑은 고딕", Font.BOLD, 30));
JButton join = new JButton("회원가입");
JButton cancel = new JButton("취소");
JTextField id = new JTextField(10);
JPasswordField pwd = new JPasswordField(10); //JPasswordField의 존재 확인.
JTextField name = new JTextField(10);
JTextField phone = new JTextField(10);
JRadioButton client = new JRadioButton("고객");
JRadioButton manager = new JRadioButton("매니저");
JRadioButton etc = new JRadioButton("기타");
ButtonGroup bg = new ButtonGroup();
bg.add(client);bg.add(manager);bg.add(etc);
//radio 컨테이너
JPanel radioPanel = new JPanel();
radioPanel.add(client); radioPanel.add(manager); radioPanel.add(etc);
//form 컨테이너
JPanel idPanel = new JPanel();
idPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
idPanel.add(new JLabel("아이디: "));
idPanel.add(id);
JPanel pwdPanel = new JPanel();
pwdPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
pwdPanel.add(new JLabel("비밀번호: "));
pwdPanel.add(pwd);
JPanel namePanel = new JPanel();
namePanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
namePanel.add(new JLabel("이름: "));
namePanel.add(name);
JPanel phonePanel = new JPanel();
phonePanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
phonePanel.add(new JLabel("전화번호: "));
phonePanel.add(phone);
JPanel formPanel = new JPanel();
formPanel.setLayout(new GridLayout(4, 1));
formPanel.add(idPanel); formPanel.add(pwdPanel);
formPanel.add(namePanel); formPanel.add(phonePanel);
//radio + form panel
JPanel contentPanel = new JPanel();
contentPanel.setLayout(new FlowLayout());
contentPanel.add(radioPanel);
contentPanel.add(formPanel);
//button panel
JPanel buttonPanel = new JPanel();
buttonPanel.add(join); buttonPanel.add(cancel);
//프레임에 컨테이너 올리기
add(title, BorderLayout.NORTH);
add(contentPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
setBounds(200, 200, 300, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
//이벤트처리
join.addActionListener(new ActionListener() {
String choice = null;
@Override
public void actionPerformed(ActionEvent e) {
String myId = id.getText();
String myPwd = new String(pwd.getPassword());
String myName = name.getText();
String myPhone = phone.getText();
if(client.isSelected()) {
choice = client.getText();
}else if(manager.isSelected()){
choice = manager.getText();
}else if(etc.isSelected()) {
choice = etc.getText();
}
JOptionPane.showMessageDialog(null, "아이디: " + myId + ", 비밀번호 : "+ myPwd +
", 이름 : "+ myName + ", 연락처 : " + myPhone + ", 가입유형 : " + choice);
}
});
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Main();
dispose();
}
});
}
}
|
cs |
취소버튼에는 new Main();을 통해서 첫 클래스를 다시 실행시켜주면 첫 화면으로 돌아갈 수 있다.
'국기훈련과정 > JAVA 복습노트' 카테고리의 다른 글
02. JAVA 의 변수 (0) | 2021.09.01 |
---|---|
01. JAVA의 시작. (0) | 2021.09.01 |
13. GUI_Event 예제 (0) | 2021.08.27 |
13. GUI_Event (0) | 2021.08.27 |
13. GUI_03 (0) | 2021.08.26 |