-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava218_gui.java
More file actions
85 lines (69 loc) · 1.86 KB
/
Copy pathJava218_gui.java
File metadata and controls
85 lines (69 loc) · 1.86 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
77
78
79
80
81
82
83
84
85
package java0915_gui;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/*
* AWT Swing
* Frame -> JFame
* TextField -> JTextField
* Button -> JButton
*/
//JButton -> click -> ActionEvent -> ActionListener
//JTextField -> Enter -> ActionEvent -> ActionListener
class TextInput2 extends JFrame implements ActionListener {
JTextField inputTxt;
JButton clickBtn;
JTextArea multiTra;
JPanel pn;
public TextInput2() {
inputTxt = new JTextField(10);
clickBtn = new JButton("input");
pn = new JPanel();
pn.add(inputTxt);
pn.add(clickBtn);
multiTra = new JTextArea(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 Java218_gui {
public static void main(String[] args) {
new TextInput2();
}
}