-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.java
More file actions
268 lines (220 loc) · 7.97 KB
/
Search.java
File metadata and controls
268 lines (220 loc) · 7.97 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package network_term;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.sql.*;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.Scanner;
public class Search extends JFrame implements ActionListener,ListSelectionListener{
private JTextField searchwindow;
private JButton search_button;
private Font result;
private JTextField input;
private JList resultList;
private JButton friend_add,setting_button;
private DefaultListModel modelresult;
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/user";
String id = "root";
String pwd = "1234";
Connection con = null;
String[] name=new String[100];
String [] userid=new String[100];
String [] password=new String[100];
String [] introduce=new String[100];
int [] onOff= new int[100];
int [] user_id=new int[100];
int count=0;
int resultcount=0;
Search(){
result=new Font ("나눔스퀘어",Font.PLAIN,15);
//데베 연결
try {
System.out.println("networkDB 연결중");
Class.forName(driver);
con = DriverManager.getConnection(url, id, pwd);
System.out.println("드라이버 로딩 성공");
} catch (Exception e) {
System.out.println("드라이버 로딩 실패 ");
try {
con.close();
} catch (SQLException e1) {
}
}
//사용자들 정보 불러오기
String sql = "SELECT * FROM member";
PreparedStatement pstmt = null;
ResultSet rs = null;
int i=0;
try {
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
count++;
name[i] = rs.getString("name");
userid[i] = rs.getString("id");
password[i] = rs.getString("password");
onOff[i] = rs.getInt("on/off");
introduce[i] = rs.getString("introduce");
user_id[i]= rs.getInt("user_id");
System.out.println("피드 데이터 성공");
System.out.println("> 유저이름: " + name[i]);
System.out.println("> 한줄소개: " + introduce[i]);
System.out.println("> 온라인/오프라인 여부: " + onOff[i]);
i++;
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
pstmt.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
setSize(450, 450);
setTitle("Search");
//getContentPane().setBackground(Color.WHITE);
setLayout(new FlowLayout());
setLocationRelativeTo(null);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
dispose();
}
});
//검색창 panel
JPanel window = new JPanel();
window.setBackground(Color.WHITE);
window.setPreferredSize(new Dimension(450, 450));
window.setLayout(null);
ImageIcon searchbt= new ImageIcon("C:\\Users\\82102\\eclipse-workspace\\network_term\\src\\network_img\\search22.png");
Image searchbt_png = searchbt.getImage(); //ImageIcon을 Image로 변환.
Image searchbt_png2 = searchbt_png.getScaledInstance(45, 45, java.awt.Image.SCALE_SMOOTH);
ImageIcon sc = new ImageIcon(searchbt_png2); //Image로 ImageIcon 생성
search_button=new JButton(sc);
//search_button.setPreferredSize(new Dimension(55, 55));
search_button.setBorderPainted(false);
search_button.setContentAreaFilled(false);
search_button.setFocusPainted(false);
search_button.addActionListener(this);
search_button.setBounds(40, 10, 50, 50);
input = new JTextField(); //검색창
input.setFont(result);
input.addActionListener(this);
window.add(input);
input.setBounds(110, 20, 270, 40);
search_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Search a3 = new Search();
a3.setVisible(true);
}
});
window.add(search_button);
searchwindow=new JTextField();
// 검색결과---------------------------------------------------
//리스트 생성
modelresult = new DefaultListModel();
resultList = new JList();
JScrollPane scrollOn = new JScrollPane(resultList);
resultList.setModel(modelresult);
resultList.setBounds(50,80,200,170);
resultList.setFont(result);
resultList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
resultList.addListSelectionListener(this);
window.add(resultList);
window.add(scrollOn);
//검색 나오면 친구등록 창 (addfriend) or 상세정보 (setting) 보는 부분-----------------------------------
//-------------------------------------------------------------------
ImageIcon friendbt= new ImageIcon("C:\\Users\\82102\\eclipse-workspace\\network_term\\src\\network_img\\addFriend2.png");
Image friendbt_png = friendbt.getImage(); //ImageIcon을 Image로 변환.
Image friendbt_png2 = friendbt_png.getScaledInstance(100, 40, java.awt.Image.SCALE_SMOOTH);
ImageIcon fb = new ImageIcon(friendbt_png2); //Image로 ImageIcon 생성
friend_add=new JButton(fb);
friend_add.setPreferredSize(new Dimension(55, 55));
friend_add.setBorderPainted(false);
friend_add.setContentAreaFilled(false);
friend_add.setFocusPainted(false);
friend_add.addActionListener(this);
friend_add.setBounds(110, 270, 90, 60);
friend_add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddFriend a1 = new AddFriend();
a1.setVisible(true);
}
});
ImageIcon settingbt= new ImageIcon("C:\\Users\\82102\\eclipse-workspace\\network_term\\src\\network_img\\info.png");
Image settingbt_png = settingbt.getImage(); //ImageIcon을 Image로 변환.
Image settingbt_png2 = settingbt_png.getScaledInstance(100, 40, java.awt.Image.SCALE_SMOOTH);
ImageIcon st = new ImageIcon(settingbt_png2); //Image로 ImageIcon 생성
setting_button=new JButton(st);
setting_button.setPreferredSize(new Dimension(55, 55));
setting_button.setBorderPainted(false);
setting_button.setContentAreaFilled(false);
setting_button.setFocusPainted(false);
setting_button.addActionListener(this);
setting_button.setBounds(220, 270, 100, 60);
setting_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Setting a2 = new Setting();
a2.setVisible(true);
}
});
window.add(friend_add);
window.add(setting_button);
add(window);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
//부분 검색 && 검색 결과 출력------------------------------------------------------
if (e.getSource() == input) {
String inputStr = input.getText();
// String [] resultStr = {};
//name으로 search
for(int i=0;i<count;i++) {
if(name[i].contains(inputStr)) {
System.out.println("검색 결과 : "+name[i]);
modelresult.insertElementAt(name[i]+" "+userid[i],resultcount);
resultcount++;
}
else {
continue;
}
}
//id로 search
for(int j=0;j<count;j++) {
if(userid[j].contains(inputStr)) {
System.out.println("검색 결과 : "+name[j]);
modelresult.insertElementAt(name[j]+" "+userid[j],resultcount);
resultcount++;
}
else {
continue;
}
}
}
}
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
Search JFrameSearch = new Search();
JFrameSearch.setVisible(true);
}
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
}
}