-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava217_gui.java
More file actions
76 lines (63 loc) · 1.73 KB
/
Copy pathJava217_gui.java
File metadata and controls
76 lines (63 loc) · 1.73 KB
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
package java0915_gui;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
// Button -> click -> ActionEvent -> ActionListener
// TextField -> Enter -> ActionEvent -> ActionListener
class TextInput extends Frame implements ActionListener {
TextField inputTxt;
Button clickBtn;
TextArea multiTra;
Panel pn;
public TextInput() {
inputTxt = new TextField(20);
clickBtn = new Button("input");
pn = new Panel();
pn.add(inputTxt);
pn.add(clickBtn);
multiTra = new TextArea(10, 10);
// TextArea에서 편집 불가능
multiTra.setEditable(false);
this.add(pn, BorderLayout.NORTH);
this.add(multiTra, BorderLayout.CENTER);
// Button에 ActionListener 연결
clickBtn.addActionListener(this);
// TextField에 ActionListener 연결
inputTxt.addActionListener(this);
setSize(300, 200);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
// TextField에 입력된 문자열을 리턴한다.
String stn = inputTxt.getText();
if (stn.length() == 0) {
inputTxt.requestFocus();
return;
}
// TextArea에 TextField에 입력된 문자열을 추가한다.
multiTra.append(stn + "\r\n");
// TextField를 초기화한다.
inputTxt.setText("");
// TextField로 포커스를 이동한다.
inputTxt.requestFocus();
}
}
public class Java217_gui {
public static void main(String[] args) {
new TextInput();
}
}