PlaceComponents.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package com.sg.answer.panel;
  2. import com.sg.answer.core.SgEntity;
  3. import com.sg.answer.core.UserInfoEntity;
  4. import com.sg.answer.sg.Constant;
  5. import com.sg.answer.sg.FrameManager;
  6. import com.sg.answer.entity.JobTypeEntity;
  7. import com.sg.answer.utils.ObjectUtils;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.apache.commons.lang3.StringUtils;
  10. import javax.swing.*;
  11. import java.awt.*;
  12. import java.awt.datatransfer.StringSelection;
  13. import java.awt.event.*;
  14. import java.net.MalformedURLException;
  15. import java.net.URL;
  16. import java.util.concurrent.atomic.AtomicBoolean;
  17. import static com.sg.answer.sg.API.reset;
  18. /**
  19. * @author 王玉鹏
  20. * @version 1.0
  21. * @className PlaceComponents
  22. * @description TODO
  23. * @date 2020/3/19 13:03
  24. */
  25. @Slf4j
  26. public class PlaceComponents {
  27. private AtomicBoolean status = new AtomicBoolean(true);
  28. protected AtomicBoolean pause = new AtomicBoolean(false);
  29. private static final Object lock = new Object();
  30. public JPanel placeComponents() {
  31. JPanel panel = new JPanel();
  32. panel.setLayout(null);
  33. // 创建 当前关卡JLabel
  34. JLabel gkLabel = new JLabel("当前关卡:");
  35. gkLabel.setBounds(10,10,80,25);
  36. panel.add(gkLabel);
  37. SgEntity sgEntity = new SgEntity();
  38. sgEntity.getGkText().setForeground(Color.RED);
  39. sgEntity.getGkText().setBounds(100,10,165,25);
  40. panel.add(sgEntity.getGkText());
  41. // 创建 JLabel
  42. JLabel userLabel = new JLabel("用户:");
  43. /* 这个方法定义了组件的位置。
  44. * setBounds(x, y, width, height)
  45. * x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。
  46. */
  47. userLabel.setBounds(10,40,80,25);
  48. panel.add(userLabel);
  49. sgEntity.getUserText().setBounds(100,40,165,25);
  50. panel.add( sgEntity.getUserText());
  51. // 输入密码的文本域
  52. JLabel passwordLabel = new JLabel("密码:");
  53. passwordLabel.setBounds(10,70,80,25);
  54. panel.add(passwordLabel);
  55. sgEntity.getPasswordText().setBounds(100,70,165,25);
  56. panel.add(sgEntity.getPasswordText());
  57. // 工种的文本域
  58. JLabel gzLabel = new JLabel("工种:");
  59. gzLabel.setBounds(10,100,80,25);
  60. panel.add(gzLabel);
  61. JComboBox comboBox=new JComboBox();
  62. for (JobTypeEntity jobTypeEntity : Constant.jobTypeList) {
  63. comboBox.addItem(jobTypeEntity);
  64. }
  65. /* for (String JobTypeName:Constant.jobTypeId.keySet()) {
  66. comboBox.addItem(JobTypeName);
  67. }*/
  68. sgEntity.setComboBox(comboBox);
  69. comboBox.setBounds(100,100,165,25);
  70. panel.add(comboBox);
  71. //难度
  72. JLabel typeLabel = new JLabel("难度选择:");
  73. typeLabel.setBounds(10,130,80,25);
  74. panel.add(typeLabel);
  75. JComboBox typeBox=new JComboBox();
  76. for (String name : Constant.level.keySet()) {
  77. typeBox.addItem(name);
  78. }
  79. typeBox.setBounds(100,130,165,25);
  80. sgEntity.setTypeBox(typeBox);
  81. panel.add(typeBox);
  82. //统计
  83. JLabel xjLabel = new JLabel("当前刷分小计:");
  84. xjLabel.setBounds(320,10,100,25);
  85. panel.add(xjLabel);
  86. sgEntity.getXjText().setBounds(410,10,50,25);
  87. sgEntity.getXjText().setForeground(Color.green);
  88. panel.add(sgEntity.getXjText());
  89. //目标显示
  90. JLabel mbLabel = new JLabel("目标:");
  91. mbLabel.setBounds(460,10,100,25);
  92. panel.add(mbLabel);
  93. sgEntity.getMbText().setBounds(500,10,50,25);
  94. sgEntity.getMbText().setForeground(Color.red);
  95. panel.add(sgEntity.getMbText());
  96. //倒计时
  97. sgEntity.getTimeLabel().setForeground(Color.MAGENTA);
  98. sgEntity.getTimeLabel().setBounds(530,10,150,20);
  99. panel.add(sgEntity.getTimeLabel());
  100. // 自定义刷分
  101. //目标显示
  102. JLabel zdyLabel = new JLabel("自定义刷分:");
  103. zdyLabel.setBounds(10,160,100,25);
  104. panel.add(zdyLabel);
  105. sgEntity.getCoustomText().setBounds(100,160,100,25);
  106. panel.add( sgEntity.getCoustomText());
  107. JButton coustomButton = new JButton("刷分");
  108. coustomButton.setBounds(210, 160, 55, 25);
  109. panel.add(coustomButton);
  110. // 创建登录按钮
  111. JButton loginButton = new JButton("登陆答题");
  112. loginButton.setBounds(10, 190, 110, 25);
  113. panel.add(loginButton);
  114. // 创建停止按钮
  115. JButton stopButton = new JButton("结束刷题");
  116. stopButton.setBounds(160, 190, 110, 25);
  117. panel.add(stopButton);
  118. //日志
  119. sgEntity.getTextArea().setBounds(10, 260, 770, 150);
  120. // 设置自动换行
  121. sgEntity.getTextArea().setLineWrap(true);
  122. // 添加到内容面板
  123. sgEntity.getTextArea().setText("日志");
  124. sgEntity.getTextArea().setForeground(Color.green);
  125. sgEntity.getTextArea().setBackground(Color.black);
  126. sgEntity.getTextArea().setFocusable(false);
  127. panel.add( sgEntity.getTextArea());
  128. //日志滚动面板
  129. JScrollPane scrollpane=new JScrollPane();
  130. scrollpane.setBounds(10, 260, 700, 180);
  131. scrollpane.setViewportView( sgEntity.getTextArea());
  132. panel.add(scrollpane);
  133. // tip
  134. JLabel bqLabel = new JLabel("本软件由彭宇制作,如有疑问请加微信:pengyu20100613联系本人");
  135. bqLabel.setForeground(Color.RED);
  136. bqLabel.setBounds(10,420,400,20);
  137. panel.add(bqLabel);
  138. /*******************************个人信息面板*********************************/
  139. UserInfoEntity userInfoEntity = new UserInfoEntity();
  140. // 姓名
  141. JLabel userName = new JLabel("姓名:");
  142. userName.setBounds(320,40,80,25);
  143. panel.add(userName);
  144. userInfoEntity.setUserNameData( new JLabel());
  145. userInfoEntity.getUserNameData().setBounds(360,40,80,25);
  146. userInfoEntity.getUserNameData().setForeground(Color.blue);
  147. userInfoEntity.getUserNameData().addMouseListener(new MouseAdapter() {
  148. @Override
  149. public void mouseClicked(MouseEvent e){
  150. if (e.getButton() == MouseEvent.BUTTON3){
  151. StringSelection selection = new StringSelection(userInfoEntity.getUserNameData().getText());
  152. Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection);
  153. JOptionPane.showMessageDialog(panel, "姓名已复制到剪贴板了" ," tip", JOptionPane.WARNING_MESSAGE);
  154. }
  155. }
  156. });
  157. panel.add( userInfoEntity.getUserNameData());
  158. // 工号
  159. JLabel jobNo = new JLabel("工号:");
  160. jobNo.setBounds(320,75,80,25);
  161. panel.add(jobNo);
  162. userInfoEntity.getJobNoData().setBounds(360,75,80,25);
  163. userInfoEntity.getJobNoData().setForeground(Color.blue);
  164. panel.add(userInfoEntity.getJobNoData());
  165. // 工种
  166. JLabel jobType = new JLabel("工种:");
  167. jobType.setBounds(320,105,80,25);
  168. panel.add(jobType);
  169. userInfoEntity.getJobTypeData().setBounds(360,105,80,25);
  170. userInfoEntity.getJobTypeData().setForeground(Color.blue);
  171. panel.add(userInfoEntity.getJobTypeData());
  172. // 积分
  173. JLabel integral = new JLabel("总积分:");
  174. integral.setBounds(30,220,80,25);
  175. integral.setFont(new Font (Font.DIALOG, Font.BOLD, 15));
  176. integral.setForeground(Color.BLUE);
  177. panel.add(integral);
  178. userInfoEntity.getIntegralDate().setBounds(100,220,80,25);
  179. userInfoEntity.getIntegralDate().setFont(new Font (Font.DIALOG, Font.BOLD, 15));
  180. userInfoEntity.getIntegralDate().setForeground(Color.red);
  181. panel.add(userInfoEntity.getIntegralDate());
  182. // 金币
  183. JLabel goldCoin = new JLabel("金币:");
  184. goldCoin.setBounds(320,135,80,25);
  185. panel.add(goldCoin);
  186. userInfoEntity.getGoldCoinData().setBounds(360,135,80,25);
  187. userInfoEntity.getGoldCoinData().setForeground(Color.blue);
  188. panel.add(userInfoEntity.getGoldCoinData());
  189. // 钻石
  190. JLabel diamond = new JLabel("钻石:");
  191. diamond.setBounds(320,165,80,25);
  192. panel.add(diamond);
  193. // 暂停
  194. JButton parseButton = new JButton("暂停答题");
  195. parseButton.setBounds(350, 190, 80, 25);
  196. panel.add(parseButton);
  197. userInfoEntity.getDiamondData().setBounds(360,165,80,25);
  198. userInfoEntity.getDiamondData().setForeground(Color.blue);
  199. panel.add( userInfoEntity.getDiamondData());
  200. JLabel initLabel = new JLabel("本次答题初始分数:");
  201. initLabel.setBounds(200,220,140,25);
  202. initLabel.setFont(new Font (Font.DIALOG, Font.BOLD, 15));
  203. initLabel.setForeground(Color.BLUE);
  204. panel.add(initLabel);
  205. JLabel statrScore = new JLabel("0");
  206. statrScore.setBounds(355,220,80,25);
  207. statrScore.setFont(new Font (Font.DIALOG, Font.BOLD, 15));
  208. statrScore.setForeground(Color.red);
  209. panel.add(statrScore);
  210. // 头像
  211. try {
  212. URL url= new URL("http://pic1.win4000.com/pic/9/75/1a9b924550.jpg");
  213. ImageIcon imageIcon = new ImageIcon(url);
  214. imageIcon.setImage(imageIcon.getImage().getScaledInstance(195,290,Image.SCALE_DEFAULT));
  215. sgEntity.setImage(new JLabel(imageIcon));
  216. sgEntity.getImage().setBounds(480,30,200,300);
  217. panel.add(sgEntity.getImage());
  218. } catch (MalformedURLException e) {
  219. e.printStackTrace();
  220. }
  221. //自定义监听时间
  222. coustomButton.addActionListener(e -> {
  223. if (StringUtils.isEmpty(sgEntity.getCoustomText().getText()) || !ObjectUtils.isNumeric(sgEntity.getCoustomText().getText())) {
  224. JOptionPane.showMessageDialog(panel, "请输入正确得分数", " stop", JOptionPane.ERROR_MESSAGE);
  225. return;
  226. }
  227. sgEntity.setCoustomScore(Double.parseDouble(sgEntity.getCoustomText().getText()));
  228. sgEntity.getMbText().setText(sgEntity.getCoustomText().getText());
  229. });
  230. //结束答题时间
  231. stopButton.addActionListener(e -> {
  232. status.set(false);
  233. JOptionPane.showMessageDialog(panel, "本次答题已结束,如需要重写开始请继续" ," stop", JOptionPane.ERROR_MESSAGE);
  234. reset(loginButton,sgEntity,"已结束本次答题");
  235. });
  236. //登录时间
  237. loginButton.addActionListener((ActionEvent e) -> {
  238. status.set(true);
  239. try {
  240. new FrameManager().start(loginButton,sgEntity,userInfoEntity,statrScore,status,pause,lock);
  241. } catch (Exception e1) {
  242. e1.printStackTrace();
  243. }
  244. });
  245. parseButton.addActionListener(e ->{
  246. loginButton.setEnabled(false);
  247. parseButton.setText(parseButton.getText().equals("暂停答题")?"继续答题":"暂停答题");
  248. pause.set(!pause.get());
  249. if (!pause.get()){
  250. synchronized (lock){
  251. sgEntity.apppendLogs(",已恢复当前答题, ");
  252. log.info("以恢复当前等待线程,开始继续答题");
  253. lock.notify();
  254. }
  255. }
  256. });
  257. return panel;
  258. }
  259. }