package com.sg.answer.panel; import com.sg.answer.core.SgEntity; import com.sg.answer.core.UserInfoEntity; import com.sg.answer.sg.Constant; import com.sg.answer.sg.FrameManager; import com.sg.answer.entity.JobTypeEntity; import com.sg.answer.utils.ObjectUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import javax.swing.*; import java.awt.*; import java.awt.datatransfer.StringSelection; import java.awt.event.*; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.atomic.AtomicBoolean; import static com.sg.answer.sg.API.reset; /** * @author 王玉鹏 * @version 1.0 * @className PlaceComponents * @description TODO * @date 2020/3/19 13:03 */ @Slf4j public class PlaceComponents { private AtomicBoolean status = new AtomicBoolean(true); protected AtomicBoolean pause = new AtomicBoolean(false); private static final Object lock = new Object(); public JPanel placeComponents() { JPanel panel = new JPanel(); panel.setLayout(null); // 创建 当前关卡JLabel JLabel gkLabel = new JLabel("当前关卡:"); gkLabel.setBounds(10,10,80,25); panel.add(gkLabel); SgEntity sgEntity = new SgEntity(); sgEntity.getGkText().setForeground(Color.RED); sgEntity.getGkText().setBounds(100,10,165,25); panel.add(sgEntity.getGkText()); // 创建 JLabel JLabel userLabel = new JLabel("用户:"); /* 这个方法定义了组件的位置。 * setBounds(x, y, width, height) * x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。 */ userLabel.setBounds(10,40,80,25); panel.add(userLabel); sgEntity.getUserText().setBounds(100,40,165,25); panel.add( sgEntity.getUserText()); // 输入密码的文本域 JLabel passwordLabel = new JLabel("密码:"); passwordLabel.setBounds(10,70,80,25); panel.add(passwordLabel); sgEntity.getPasswordText().setBounds(100,70,165,25); panel.add(sgEntity.getPasswordText()); // 工种的文本域 JLabel gzLabel = new JLabel("工种:"); gzLabel.setBounds(10,100,80,25); panel.add(gzLabel); JComboBox comboBox=new JComboBox(); for (JobTypeEntity jobTypeEntity : Constant.jobTypeList) { comboBox.addItem(jobTypeEntity); } /* for (String JobTypeName:Constant.jobTypeId.keySet()) { comboBox.addItem(JobTypeName); }*/ sgEntity.setComboBox(comboBox); comboBox.setBounds(100,100,165,25); panel.add(comboBox); //难度 JLabel typeLabel = new JLabel("难度选择:"); typeLabel.setBounds(10,130,80,25); panel.add(typeLabel); JComboBox typeBox=new JComboBox(); for (String name : Constant.level.keySet()) { typeBox.addItem(name); } typeBox.setBounds(100,130,165,25); sgEntity.setTypeBox(typeBox); panel.add(typeBox); //统计 JLabel xjLabel = new JLabel("当前刷分小计:"); xjLabel.setBounds(320,10,100,25); panel.add(xjLabel); sgEntity.getXjText().setBounds(410,10,50,25); sgEntity.getXjText().setForeground(Color.green); panel.add(sgEntity.getXjText()); //目标显示 JLabel mbLabel = new JLabel("目标:"); mbLabel.setBounds(460,10,100,25); panel.add(mbLabel); sgEntity.getMbText().setBounds(500,10,50,25); sgEntity.getMbText().setForeground(Color.red); panel.add(sgEntity.getMbText()); //倒计时 sgEntity.getTimeLabel().setForeground(Color.MAGENTA); sgEntity.getTimeLabel().setBounds(530,10,150,20); panel.add(sgEntity.getTimeLabel()); // 自定义刷分 //目标显示 JLabel zdyLabel = new JLabel("自定义刷分:"); zdyLabel.setBounds(10,160,100,25); panel.add(zdyLabel); sgEntity.getCoustomText().setBounds(100,160,100,25); panel.add( sgEntity.getCoustomText()); JButton coustomButton = new JButton("刷分"); coustomButton.setBounds(210, 160, 55, 25); panel.add(coustomButton); // 创建登录按钮 JButton loginButton = new JButton("登陆答题"); loginButton.setBounds(10, 190, 110, 25); panel.add(loginButton); // 创建停止按钮 JButton stopButton = new JButton("结束刷题"); stopButton.setBounds(160, 190, 110, 25); panel.add(stopButton); //日志 sgEntity.getTextArea().setBounds(10, 260, 770, 150); // 设置自动换行 sgEntity.getTextArea().setLineWrap(true); // 添加到内容面板 sgEntity.getTextArea().setText("日志"); sgEntity.getTextArea().setForeground(Color.green); sgEntity.getTextArea().setBackground(Color.black); sgEntity.getTextArea().setFocusable(false); panel.add( sgEntity.getTextArea()); //日志滚动面板 JScrollPane scrollpane=new JScrollPane(); scrollpane.setBounds(10, 260, 700, 180); scrollpane.setViewportView( sgEntity.getTextArea()); panel.add(scrollpane); // tip JLabel bqLabel = new JLabel("本软件由彭宇制作,如有疑问请加微信:pengyu20100613联系本人"); bqLabel.setForeground(Color.RED); bqLabel.setBounds(10,420,400,20); panel.add(bqLabel); /*******************************个人信息面板*********************************/ UserInfoEntity userInfoEntity = new UserInfoEntity(); // 姓名 JLabel userName = new JLabel("姓名:"); userName.setBounds(320,40,80,25); panel.add(userName); userInfoEntity.setUserNameData( new JLabel()); userInfoEntity.getUserNameData().setBounds(360,40,80,25); userInfoEntity.getUserNameData().setForeground(Color.blue); userInfoEntity.getUserNameData().addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e){ if (e.getButton() == MouseEvent.BUTTON3){ StringSelection selection = new StringSelection(userInfoEntity.getUserNameData().getText()); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection); JOptionPane.showMessageDialog(panel, "姓名已复制到剪贴板了" ," tip", JOptionPane.WARNING_MESSAGE); } } }); panel.add( userInfoEntity.getUserNameData()); // 工号 JLabel jobNo = new JLabel("工号:"); jobNo.setBounds(320,75,80,25); panel.add(jobNo); userInfoEntity.getJobNoData().setBounds(360,75,80,25); userInfoEntity.getJobNoData().setForeground(Color.blue); panel.add(userInfoEntity.getJobNoData()); // 工种 JLabel jobType = new JLabel("工种:"); jobType.setBounds(320,105,80,25); panel.add(jobType); userInfoEntity.getJobTypeData().setBounds(360,105,80,25); userInfoEntity.getJobTypeData().setForeground(Color.blue); panel.add(userInfoEntity.getJobTypeData()); // 积分 JLabel integral = new JLabel("总积分:"); integral.setBounds(30,220,80,25); integral.setFont(new Font (Font.DIALOG, Font.BOLD, 15)); integral.setForeground(Color.BLUE); panel.add(integral); userInfoEntity.getIntegralDate().setBounds(100,220,80,25); userInfoEntity.getIntegralDate().setFont(new Font (Font.DIALOG, Font.BOLD, 15)); userInfoEntity.getIntegralDate().setForeground(Color.red); panel.add(userInfoEntity.getIntegralDate()); // 金币 JLabel goldCoin = new JLabel("金币:"); goldCoin.setBounds(320,135,80,25); panel.add(goldCoin); userInfoEntity.getGoldCoinData().setBounds(360,135,80,25); userInfoEntity.getGoldCoinData().setForeground(Color.blue); panel.add(userInfoEntity.getGoldCoinData()); // 钻石 JLabel diamond = new JLabel("钻石:"); diamond.setBounds(320,165,80,25); panel.add(diamond); // 暂停 JButton parseButton = new JButton("暂停答题"); parseButton.setBounds(350, 190, 80, 25); panel.add(parseButton); userInfoEntity.getDiamondData().setBounds(360,165,80,25); userInfoEntity.getDiamondData().setForeground(Color.blue); panel.add( userInfoEntity.getDiamondData()); JLabel initLabel = new JLabel("本次答题初始分数:"); initLabel.setBounds(200,220,140,25); initLabel.setFont(new Font (Font.DIALOG, Font.BOLD, 15)); initLabel.setForeground(Color.BLUE); panel.add(initLabel); JLabel statrScore = new JLabel("0"); statrScore.setBounds(355,220,80,25); statrScore.setFont(new Font (Font.DIALOG, Font.BOLD, 15)); statrScore.setForeground(Color.red); panel.add(statrScore); // 头像 try { URL url= new URL("http://pic1.win4000.com/pic/9/75/1a9b924550.jpg"); ImageIcon imageIcon = new ImageIcon(url); imageIcon.setImage(imageIcon.getImage().getScaledInstance(195,290,Image.SCALE_DEFAULT)); sgEntity.setImage(new JLabel(imageIcon)); sgEntity.getImage().setBounds(480,30,200,300); panel.add(sgEntity.getImage()); } catch (MalformedURLException e) { e.printStackTrace(); } //自定义监听时间 coustomButton.addActionListener(e -> { if (StringUtils.isEmpty(sgEntity.getCoustomText().getText()) || !ObjectUtils.isNumeric(sgEntity.getCoustomText().getText())) { JOptionPane.showMessageDialog(panel, "请输入正确得分数", " stop", JOptionPane.ERROR_MESSAGE); return; } sgEntity.setCoustomScore(Double.parseDouble(sgEntity.getCoustomText().getText())); sgEntity.getMbText().setText(sgEntity.getCoustomText().getText()); }); //结束答题时间 stopButton.addActionListener(e -> { status.set(false); JOptionPane.showMessageDialog(panel, "本次答题已结束,如需要重写开始请继续" ," stop", JOptionPane.ERROR_MESSAGE); reset(loginButton,sgEntity,"已结束本次答题"); }); //登录时间 loginButton.addActionListener((ActionEvent e) -> { status.set(true); try { new FrameManager().start(loginButton,sgEntity,userInfoEntity,statrScore,status,pause,lock); } catch (Exception e1) { e1.printStackTrace(); } }); parseButton.addActionListener(e ->{ loginButton.setEnabled(false); parseButton.setText(parseButton.getText().equals("暂停答题")?"继续答题":"暂停答题"); pause.set(!pause.get()); if (!pause.get()){ synchronized (lock){ sgEntity.apppendLogs(",已恢复当前答题, "); log.info("以恢复当前等待线程,开始继续答题"); lock.notify(); } } }); return panel; } }