Guest User

Untitled

a guest
Jan 6th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.62 KB | None | 0 0
  1. import org.json.JSONArray;
  2. import org.json.JSONObject;
  3.  
  4. import javax.swing.*;
  5. import javax.swing.plaf.IconUIResource;
  6. import java.awt.*;
  7. import java.awt.event.*;
  8.  
  9. /**
  10. * Created by 史书恒 on 2016/11/1.
  11. */
  12. public class AdminWindow extends JFrame {
  13. JFrame father;
  14.  
  15. UsersInfo[] usersInfos, hasRentInfos;
  16. CarsInfo[] carsInfos;
  17. DoubleClickList uclick, cclick, hclick;
  18.  
  19. AdminWindow(JFrame father) {
  20. this.father = father;
  21. father.setVisible(false);
  22. init();
  23. }
  24. void init() { //初始化,创建窗口以及相关组件
  25. Box baseBox, usersBox, carsBox, rentBox, groupBox, buttonBox;
  26. JButton deleteButton, addButton, exitButton, refreshButton;
  27. JList<UsersInfo> usinfo, hasRentUser;
  28. JList<CarsInfo> carinfo;
  29. JScrollPane userscroller, carscroller, rentscroller;
  30. DefaultListModel usersListModel, carsListMode, hasRentListMode;
  31.  
  32. baseBox = Box.createVerticalBox();
  33. usersBox = Box.createVerticalBox();
  34. carsBox = Box.createVerticalBox();
  35. rentBox = Box.createVerticalBox();
  36. groupBox = Box.createHorizontalBox();
  37. buttonBox = Box.createHorizontalBox();
  38. refreshButton = new JButton("刷新");
  39. deleteButton = new JButton("删除");
  40. addButton = new JButton("添加");
  41. exitButton = new JButton("退出");
  42. usersListModel = new DefaultListModel();
  43. carsListMode = new DefaultListModel();
  44. hasRentListMode = new DefaultListModel();
  45. refreshButton.addActionListener(new ActionListener() {
  46. @Override
  47. public void actionPerformed(ActionEvent e) {
  48. /*
  49. usersListModel.removeAllElements();
  50. carsListMode.removeAllElements();
  51. dealJSON(usersListModel, carsListMode, hasRentListMode);
  52. uclick = new DoubleClickList(usersInfos);
  53. cclick = new DoubleClickList(carsInfos);
  54. */
  55. new AdminWindow(father);
  56. dispose();
  57. }
  58. });
  59. exitButton.addActionListener(new ActionListener() {
  60. @Override
  61. public void actionPerformed(ActionEvent e) {
  62. new Login();
  63. dispose();
  64. }
  65. });
  66.  
  67. dealJSON(usersListModel, carsListMode, hasRentListMode);
  68.  
  69. usinfo = new JList<>(usersListModel);
  70. usinfo.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  71. carinfo = new JList<>(carsListMode);
  72. carinfo.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  73. hasRentUser = new JList<>(hasRentListMode);
  74. hasRentUser.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  75. userscroller = new JScrollPane(usinfo);
  76. carscroller = new JScrollPane(carinfo);
  77. rentscroller = new JScrollPane(hasRentUser);
  78. uclick = new DoubleClickList(usersInfos);
  79. usinfo.addMouseListener(uclick);
  80. cclick = new DoubleClickList(carsInfos);
  81. carinfo.addMouseListener(cclick);
  82. hclick = new DoubleClickList(hasRentInfos);
  83. hasRentUser.addMouseListener(hclick);
  84.  
  85. usersBox.add(new JLabel("用户信息"));
  86. usersBox.add(Box.createVerticalStrut(5));
  87. usersBox.add(userscroller);
  88. carsBox.add(new JLabel("车辆信息"));
  89. carsBox.add(Box.createVerticalStrut(5));
  90. carsBox.add(carscroller);
  91. rentBox.add(new JLabel("租车信息"));
  92. rentBox.add(Box.createVerticalStrut(5));
  93. rentBox.add(rentscroller);
  94. groupBox.add(Box.createHorizontalStrut(10));
  95. groupBox.add(usersBox);
  96. groupBox.add(Box.createHorizontalStrut(5));
  97. groupBox.add(carsBox);
  98. groupBox.add(Box.createHorizontalStrut(5));
  99. groupBox.add(rentBox);
  100. groupBox.add(Box.createHorizontalStrut(10));
  101. groupBox.setPreferredSize(new Dimension(700, 500));
  102. // buttonBox.add(Box.createVerticalStrut(5));
  103. // buttonBox.add(Box.createHorizontalStrut(10));
  104. // buttonBox.add(deleteButton);
  105. // buttonBox.add(Box.createHorizontalStrut(10));
  106. // buttonBox.add(addButton);
  107. buttonBox.add(Box.createHorizontalStrut(20));
  108. buttonBox.add(refreshButton);
  109. buttonBox.add(Box.createHorizontalStrut(20));
  110. buttonBox.add(exitButton);
  111. buttonBox.add(Box.createHorizontalStrut(20));
  112. baseBox.add(Box.createVerticalStrut(20));
  113. baseBox.add(groupBox);
  114. baseBox.add(Box.createVerticalStrut(10));
  115. baseBox.add(buttonBox);
  116. baseBox.add(Box.createVerticalStrut(10));
  117. baseBox.setPreferredSize(new Dimension(700, 500));
  118. add(baseBox);
  119.  
  120. setSize(800, 450);
  121. setTitle("管理员窗口");
  122. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  123. setLocationRelativeTo(null);
  124. setVisible(true);
  125. }
  126.  
  127. void dealJSON(DefaultListModel usersListModel, DefaultListModel carsListMode, DefaultListModel hasRentListMode) { //处理从服务器接受的JSON数据
  128. String jsonuser = Server.getString(Server.HOST + "/php?admin=true&getinfomode=5");
  129. String jsoncar = Server.getString(Server.HOST + "/php?admin=true&getinfomode=6");
  130. try {
  131. JSONObject root = new JSONObject(jsonuser);
  132. JSONObject data = root.getJSONObject("data");
  133. JSONArray users = data.getJSONArray("userinfo");
  134. usersInfos = null;
  135. usersInfos = new UsersInfo[users.length()];
  136. hasRentInfos = null;
  137. hasRentInfos = new UsersInfo[users.length()];
  138. hasRentListMode.removeAllElements();
  139. int hi = 0;
  140. for (int i = 0; i < users.length(); i++) {
  141. JSONObject user = users.getJSONObject(i);
  142. usersInfos[i] = new UsersInfo();
  143. usersInfos[i].account = user.getString("account");
  144. usersInfos[i].admin = user.getString("admin");
  145. usersInfos[i].day = user.getString("day");
  146. usersInfos[i].name = user.getString("name");
  147. usersInfos[i].number = user.getString("number");
  148. usersInfos[i].password = user.getString("password");
  149. usersInfos[i].startdate = user.getString("startdate");
  150. usersInfos[i].total = user.getString("total");
  151. usersInfos[i].cash = user.getString("cash");
  152. usersListModel.addElement(usersInfos[i].account);
  153. if (!usersInfos[i].number.equals("0") && !usersInfos[i].total.equals("0")) {
  154. hasRentInfos[hi] = usersInfos[i];
  155. hasRentListMode.addElement(usersInfos[i].account + " 租用了 " + usersInfos[i].number);
  156. hi++;
  157. }
  158. }
  159. } catch (Exception ee) {
  160. ee.printStackTrace();
  161. }
  162.  
  163. try {
  164. JSONObject root = new JSONObject(jsoncar);
  165. JSONObject data = root.getJSONObject("data");
  166. JSONArray cars = data.getJSONArray("carinfo");
  167. carsInfos = null;
  168. carsInfos = new CarsInfo[cars.length()];
  169. for (int i = 0; i < cars.length(); i++) {
  170. JSONObject car = cars.getJSONObject(i);
  171. carsInfos[i] = new CarsInfo();
  172. carsInfos[i].able = car.getString("able");
  173. carsInfos[i].brand = car.getString("brand");
  174. carsInfos[i].cargo = car.getString("cargo");
  175. carsInfos[i].people = car.getString("people");
  176. carsInfos[i].name = car.getString("name");
  177. if (car.getString("number").equals("0"))
  178. carsInfos[i].number = "此项为系统预留项 请勿更改";
  179. else
  180. carsInfos[i].number = car.getString("number");
  181. carsInfos[i].price = car.getString("price");
  182. carsInfos[i].style = car.getString("style");
  183. carsListMode.addElement(carsInfos[i].number);
  184. }
  185. }catch (Exception eee) {
  186. eee.printStackTrace();
  187. }
  188. }
  189. }
  190.  
  191. class DoubleClickList extends MouseAdapter { //鼠标时间监听器
  192. UsersInfo[] usersInfos; //从服务器已获取的用户信息
  193. CarsInfo[] carsInfos; //从服务器已获取的车辆信息
  194. int select; //根据select进行判断进入对应的动作
  195.  
  196. DoubleClickList(UsersInfo[] usersInfos) { //当传入UserInfo[]时select=1
  197. this.usersInfos = usersInfos;
  198. this.select = 1;
  199. }
  200.  
  201. DoubleClickList(CarsInfo[] carsInfos) { //当传入CarInfo[]时select=2
  202. this.carsInfos = carsInfos;
  203. this.select = 2;
  204. }
  205.  
  206. @Override
  207. public void mouseClicked(MouseEvent e) {
  208. JList list = (JList)e.getSource();
  209. JPopupMenu popupMenu = new JPopupMenu();
  210. if (select == 1) { //添加用户
  211. popupMenu.removeAll(); //右击菜单选项清空
  212. if (e.getClickCount() == 1 && list.getSelectedIndex() >= 0) { //单击用户列表项目时创建右击菜单(创建不显示)
  213. JMenuItem addAdmin = new JMenuItem("添加新的管理员账户");
  214. JMenuItem addUser = new JMenuItem("添加新的普通账户");
  215. JMenuItem delete = new JMenuItem("删除当前选定账户");
  216. AddItemListener addAdminLisetener = new AddItemListener(1, list);
  217. addAdmin.addActionListener(addAdminLisetener);
  218. AddItemListener addUserListener = new AddItemListener(2, list);
  219. addUser.addActionListener(addUserListener);
  220. DeleteItemListener deleteItemListener = new DeleteItemListener(select, usersInfos[list.getSelectedIndex()].account, list, usersInfos, carsInfos);
  221. delete.addActionListener(deleteItemListener);
  222. popupMenu.add(addUser);
  223. popupMenu.add(addAdmin);
  224. popupMenu.add(delete);
  225. }
  226. if (e.getClickCount() == 2) { //双击用户列表项目时,创建详细信息窗口
  227. int index = list.getSelectedIndex();
  228. String admin = "";
  229. String number = "";
  230. String total = "";
  231. String start = "";
  232. String day = "";
  233. String buttonText = "";
  234. if (usersInfos[index].admin.equals("1"))
  235. admin = "管理员用户";
  236. else
  237. admin = "非管理员用户";
  238. if (usersInfos[index].total.equals("0")) {
  239. number = "无";
  240. total = "无";
  241. start = "无";
  242. day = "无";
  243. buttonText = "租车";
  244. }
  245. else {
  246. number = usersInfos[index].number;
  247. total = usersInfos[index].total;
  248. start = usersInfos[index].startdate;
  249. day = usersInfos[index].day;
  250. buttonText = "还车";
  251. }
  252. JDialog dialog = new JDialog();
  253. dialog.setTitle("详细: " + list.getSelectedValue());
  254. dialog.setSize(300, 180);
  255. JLabel acc = new JLabel("账户:" + usersInfos[index].account);
  256. JLabel name = new JLabel("姓名:" + usersInfos[index].name);
  257. JLabel ad = new JLabel("账户类型:" + admin);
  258. JLabel no = new JLabel("已租车辆:" + number);
  259. JLabel startdate = new JLabel("租车日期:" + start);
  260. JLabel sumday = new JLabel("租车总时长/天:" + total);
  261. JLabel d = new JLabel("已租车时间/天:" + day);
  262. JButton positive = new JButton(buttonText);
  263. Box layout = Box.createVerticalBox();
  264. layout.add(acc);
  265. layout.add(name);
  266. layout.add(ad);
  267. layout.add(no);
  268. layout.add(startdate);
  269. layout.add(sumday);
  270. layout.add(d);
  271. // layout.add(positive, Box.CENTER_ALIGNMENT);
  272. dialog.add(layout);
  273. dialog.setLocationRelativeTo(null);
  274. dialog.setAlwaysOnTop(true);
  275. dialog.setVisible(true);
  276. }
  277. } else if (select == 2) { //选择车辆列表
  278. popupMenu.removeAll();
  279. if (e.getClickCount() == 1 && list.getSelectedIndex() >= 0) { //单击车辆列表项目时创建右击菜单,不显示
  280. JMenuItem addCarIten = new JMenuItem("添加新的车辆");
  281. JMenuItem delete = new JMenuItem("删除当前选定车辆");
  282. addCarIten.addActionListener(new AddItemListener(3, list));
  283. delete.addActionListener(new DeleteItemListener(select, carsInfos[list.getSelectedIndex()].number, list, usersInfos, carsInfos));
  284. popupMenu.add(addCarIten);
  285. popupMenu.add(delete);
  286. }
  287. if (e.getClickCount() == 2 && list.getSelectedIndex() >= 0) { //双击车辆列表项目时显示车辆信息更新窗口
  288. int index = list.getSelectedIndex();
  289. RegisterCar showCar = new RegisterCar(null, 1);//传入参数1即选择更新模式
  290. showCar.number_editor.setText(carsInfos[index].number);
  291. showCar.style_editor.setText(carsInfos[index].style);
  292. showCar.able_editor.setText(carsInfos[index].able);
  293. showCar.brand_editor.setText(carsInfos[index].brand);
  294. showCar.name_editor.setText(carsInfos[index].name);
  295. showCar.cargo_editor.setText(carsInfos[index].cargo);
  296. showCar.people_editor.setText(carsInfos[index].people);
  297. showCar.price_editor.setText(carsInfos[index].price);
  298. // showCar.setAlwaysOnTop(true);
  299. }
  300. }
  301. if (e.getButton() == 3) {//判断是否是右击,显示右击菜单
  302. popupMenu.show(e.getComponent(), e.getX(), e.getY());
  303. }
  304. }
  305. }
  306.  
  307. class DeleteItemListener implements ActionListener { //右击菜单中添加选项(创建用户、管理员、车辆信息)的监听器
  308. String account; //要删除的用户账户或车辆车牌
  309. JList list; //传入点击的列表
  310. int select; //根据传入的select进行判断进入对应的动作
  311. UsersInfo[] usersInfos;
  312. CarsInfo[] carsInfos;
  313.  
  314. DeleteItemListener(int userOrCars, String str, JList list, UsersInfo[] usersInfos, CarsInfo[] carsInfos){
  315. this.account = str;
  316. this.list = list;
  317. this.select = userOrCars;
  318. this.usersInfos = usersInfos;
  319. this.carsInfos = carsInfos;
  320. }
  321. @Override
  322. public void actionPerformed(ActionEvent e) {
  323. String result = "";
  324. if (select == 1) { //删除用户
  325. Server.getString(Server.HOST + "/php/?deal=2&account=" + account + "&number=" + usersInfos[select+1].number);
  326. result = Server.getString(Server.HOST + "/php/?admin=true&deletedata=1&n_account=" + account);
  327. } else if (select == 2) { //删除车辆
  328. result = Server.getString(Server.HOST + "/php/?admin=true&deletedata=2&n_number=" + account);
  329. }
  330. if (result.equals("success")) {
  331. ((DefaultListModel)list.getModel()).removeElementAt(list.getSelectedIndex());
  332. System.out.println("删除信息成功!");
  333. }
  334. else
  335. System.out.println("删除信息失败!");
  336. }
  337. }
  338.  
  339. class AddItemListener implements ActionListener { //右击菜单中添加选项(创建用户、管理员、车辆信息)的触发器
  340. int select; //根据传入的select进行判断进入对应的动作
  341. JList list; //传入点击的列表
  342. AddItemListener (int select, JList list) {
  343. this.select = select;
  344. this.list = list;
  345. }
  346.  
  347. @Override
  348. public void actionPerformed(ActionEvent e) {
  349. if (select == 1) {
  350. RegisterUser registerAdmin = new RegisterUser(null, 1);//创建管理员账户
  351. registerAdmin.setLocationRelativeTo(null);
  352. // registerAdmin.setAlwaysOnTop(true);
  353. }
  354. if (select == 2) {
  355. RegisterUser register = new RegisterUser(null, 0);//创建普通用户
  356. register.setLocationRelativeTo(null);
  357. // register.setAlwaysOnTop(true);
  358. }
  359. if (select == 3) {
  360. RegisterCar registerCar = new RegisterCar(null, 0);//添加车辆对话框
  361. registerCar.setLocationRelativeTo(null);
  362. // registerCar.setAlwaysOnTop(true);
  363. }
  364. }
  365. }
Add Comment
Please, Sign In to add comment