Repository navigation
Expand file tree
/
Copy pathChatClient.txt
More file actions
102 lines (76 loc) · 2.6 KB
/
Copy pathChatClient.txt
File metadata and controls
102 lines (76 loc) · 2.6 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package swing.component;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import swing.frame.ContentPaneFrame;
public class ChatClient extends JFrame implements ActionListener {
private JCheckBox chk1;
private JCheckBox chk2;
private JCheckBox chk3;
private JLabel lblResult;
public int sum = 0;
private JTextField tf;
private JTextArea ta;
private JButton btnSend;
public ChatClient(String title) {
setTitle(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 350);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
setPanelCenter(); // 메소드로 뽑아서 작업하는 것
setPanelBottom();
setResizable(false);
tf.setFocusable(true);
setVisible(true);
tf.requestFocus();
}
private void setPanelCenter() {
JPanel panCenter = new JPanel(); // panel은 flowlayout이 기본 설정
panCenter.setLayout(new BorderLayout());
panCenter.setBackground(Color.RED);
ta = new JTextArea(10, 20);
ta.setLineWrap(true); // 글자가 벽에 닿으면 다음줄로 넘어오는 작업
ta.setEditable(false); // 글자가 나온것을 수정하지 못하도록 하는 작업
JScrollPane sp = new JScrollPane(ta, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); // 스크롤을 설정하는 방법
panCenter.add(sp); // 스크롤 설정한것을 add해야 스크롤이 보인다
add(panCenter, BorderLayout.CENTER); //레이아웃을 센터로 보이게 설정
}
private void setPanelBottom() {
JPanel panBottom = new JPanel();
tf = new JTextField(12);
tf.addActionListener(this);
panBottom.add(tf);
btnSend = new JButton("보내기");
btnSend.setPreferredSize(new Dimension(100, 30)); // 버튼의 길이를 조절할 수 있다
btnSend.addActionListener(this);
panBottom.add(btnSend);
add(panBottom, BorderLayout.SOUTH);
}
public static void main(String[] args) {
new ChatClient("ChatClient");
}
@Override
public void actionPerformed(ActionEvent e) {
if(!tf.getText().equals("")) {
ta.append("[아무개] : " + tf.getText() + "\n");
}
tf.setText("");
}
}