Java - GUI를 활용한 Server와 Client 활용

2020. 7. 29. 22:48Java

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package chat;
 
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
 
public class SimpleChatServer extends JFrame {
    private JTextField textField;
    JTextArea textArea ;
    public SimpleChatServer(){
        showFrame();
    }
    
    public void showFrame() {
        setTitle("1:1 채팅 서버");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(400400500300);
        getContentPane().setLayout(null);
        
        textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setBackground(Color.LIGHT_GRAY);
        textArea.setBounds(021484208);
        getContentPane().add(textArea);
        
        textField = new JTextField();
        textField.setBounds(023039031);
        getContentPane().add(textField);
        textField.setColumns(10);
        
        JButton btnInput = new JButton("입력");
        btnInput.setBackground(Color.YELLOW);
        btnInput.setForeground(Color.BLUE);
        btnInput.setBounds(3872309731);
        getContentPane().add(btnInput);
        
        JPanel panel = new JPanel();
        panel.setBounds(0048422);
        getContentPane().add(panel);
        
        JLabel lblStatus = new JLabel("클라이언트 연결 상태 : ");
        lblStatus.setForeground(Color.RED);
        panel.add(lblStatus);
        
        textField.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }
        });
        
        setVisible(true);
        
    }
    
    public void sendMessage() {
        String text=textField.getText();
        textArea.append(text+"\n");
        textField.setText("");
        textField.requestFocus();
    }
    
    public static void main(String[] args) {
        new SimpleChatServer();
    }
}
 
package chat;
 
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JPanel;
 
public class SimpleChatClient extends JFrame{
    private JTextField textField;
    JTextArea textArea;
    public SimpleChatClient() {
        showFrame();
    }
    
    public void showFrame() {
        setTitle("1:1 채팅 클라이언트");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(1200400500300);
        getContentPane().setLayout(null);
        
        textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setBackground(Color.LIGHT_GRAY);
        textArea.setBounds(024484205);
        getContentPane().add(textArea);
        
        textField = new JTextField();
        textField.setBounds(023039031);
        getContentPane().add(textField);
        textField.setColumns(10);
        
        JButton btnInput = new JButton("입력");
        btnInput.setForeground(Color.BLUE);
        btnInput.setBackground(Color.YELLOW);
        btnInput.setBounds(3872309731);
        getContentPane().add(btnInput);
        
        JPanel panel = new JPanel();
        panel.setBounds(0048422);
        getContentPane().add(panel);
        
        JLabel lblStatus = new JLabel("서버 연결 상태 : ");
        lblStatus.setForeground(Color.RED);
        panel.add(lblStatus);
        
        
        textField.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }
        });
        
        setVisible(true);
    }
    
    public void sendMessage() {
        String text=textField.getText();
        textArea.append(text+"\n");
        textField.setText("");
        textField.requestFocus();
    }
    
    
 
    public static void main(String[] args) {
        new SimpleChatClient();
    }
}
 
 
 
 
cs

 

GUI를 활용해서 1:1 채팅이 가능하도록 만들어 보았다. Absolute Layout를 사용했는데 다음엔 BorderLayout을 사용해 보아야겠다. 엔터키로 채팅을 적용시키고 싶으면 버튼에 ActionListener를 걸지 말고 TextField에 걸어야 가능하다.

간단한 것이니 잊어먹지 않게 기억해 둬야 겠다.