?? pollclient.java
字號:
AnswerRadioButtonD.setText("D: " + q.getAnswerD()); AnswerRadioButtonE.setText("E: " + q.getAnswerE()); AnswerRadioButtonF.setText("F: " + q.getAnswerF()); controlsSetEnabled(true); this.toFront(); this.requestFocus(); } private int getAnswer() { if (AnswerRadioButtonA.isSelected()) {return 1; } if (AnswerRadioButtonB.isSelected()) {return 2; } if (AnswerRadioButtonC.isSelected()) {return 3; } if (AnswerRadioButtonD.isSelected()) {return 4; } if (AnswerRadioButtonE.isSelected()) {return 5; } if (AnswerRadioButtonF.isSelected()) {return 6; } return 0; } public void controlsSetEnabled(boolean b) { if (!b) QuestionNameLabel.setText("Waiting for the next question..."); QuestionTextArea.setEnabled(b); AnswerRadioButtonA.setEnabled(b); AnswerRadioButtonB.setEnabled(b); AnswerRadioButtonC.setEnabled(b); AnswerRadioButtonD.setEnabled(b); AnswerRadioButtonE.setEnabled(b); AnswerRadioButtonF.setEnabled(b); if (b) { AnswerRadioButtonC.setVisible(b); AnswerRadioButtonD.setVisible(b); AnswerRadioButtonE.setVisible(b); AnswerRadioButtonF.setVisible(b); } AnswersLabel.setEnabled(b); SendButton.setEnabled(b); if (currentQuestion == null) return; if (currentQuestion.getNumOfAnswers() <= 2) { AnswerRadioButtonC.setEnabled(false); AnswerRadioButtonC.setVisible(false); AnswerRadioButtonC.setText(""); } if (currentQuestion.getNumOfAnswers() <= 3) { AnswerRadioButtonD.setEnabled(false); AnswerRadioButtonD.setVisible(false); AnswerRadioButtonD.setText(""); } if (currentQuestion.getNumOfAnswers() <= 4) { AnswerRadioButtonE.setEnabled(false); AnswerRadioButtonE.setVisible(false); AnswerRadioButtonE.setText(""); } if (currentQuestion.getNumOfAnswers() <= 5) { AnswerRadioButtonF.setEnabled(false); AnswerRadioButtonF.setVisible(false); AnswerRadioButtonF.setText(""); } } public void connect() { if (myNet != null) return; try {myNet = new Network(AddressTextField.getText(), Network.PORT_NUMBER); } catch (IOException e) { myNet = null; return; } ConnectButton.setEnabled(false); StatusLabel.setIcon(new ImageIcon(getClass().getResource("connected-icon.gif"))); StatusLabel.setText("Connected to " + AddressTextField.getText()); jButton1.setBounds(369, 313, 30, 28); ConnectButton.setVisible(false); AddressTextField.setVisible(false); jLabel1.setVisible(false); ConnectButton.setEnabled(false); AddressTextField.setEnabled(false); jLabel1.setEnabled(false); QuestionNameLabel.setVisible(true); TimeoutLabel.setVisible(true); QuestionTextArea.setVisible(true); this.setBounds(this.getX(), this.getY(), 408, 367); this.setVisible(false); this.setVisible(true); start(); } private void disconnect() { if (myNet != null) myNet.close(); myNet = null; ConnectButton.setEnabled(true); t = null; StatusLabel.setIcon(new ImageIcon(getClass().getResource("disconnected-icon.gif"))); TimeoutLabel.setText("Time left: "); ConnectButton.setBounds(65, 60, 90, 30); AddressTextField.setBounds(40, 30, 140, 20); jLabel1.setBounds(30, 10, 90, 14); jButton1.setBounds(220-38, 130-55, 30, 28); ConnectButton.setVisible(true); AddressTextField.setVisible(true); jLabel1.setVisible(true); ConnectButton.setEnabled(true); AddressTextField.setEnabled(true); jLabel1.setEnabled(true); QuestionNameLabel.setVisible(false); TimeoutLabel.setVisible(false); QuestionTextArea.setVisible(false); this.setBounds(this.getX(), this.getY(), 220, 130); this.setVisible(false); this.setVisible(true); } public void start() { t = new Thread(this); t.start(); } private int waitForQuestion() { int status = 0; int timeouts = 0; currentQuestion = null; while (status != Network.SERVER_SENDING_QUESTION) { if (myNet == null) return Network.SERVER_DISCONNECTED; status = myNet.checkForQuestionStatus(); if (status == Network.SERVER_DISCONNECTED) { disconnect(); return Network.SERVER_DISCONNECTED; } if (status == Network.CONNECTION_TIMED_OUT) timeouts++; if (status == Network.CHECK_BYTE) timeouts = 0; if (timeouts > 25) { disconnect(); return Network.SERVER_DISCONNECTED; } try {t.sleep (50);} catch (InterruptedException e) { } } currentQuestion = myNet.recieveQuestion(); if (currentQuestion != null) showQuestion(currentQuestion); else { disconnect(); return Network.SERVER_DISCONNECTED; } return Network.QUESTION_CLOSED; } private void waitForAnswer() { int status = 0; int timeouts = 0; while (status != Network.QUESTION_CLOSED) { status = myNet.checkForQuestionStatus(); if (status == Network.QUESTION_CLOSED) { controlsSetEnabled(false); break; } if (status == Network.SERVER_DISCONNECTED) { disconnect(); controlsSetEnabled(false); return; } if (status == Network.CONNECTION_TIMED_OUT) timeouts++; if (status == Network.CHECK_BYTE) timeouts = 0; if (timeouts > 25) { disconnect(); return; } if (answer > 0) { myNet.sendAnswer(answer); answer = 0; } try {t.sleep (50);} catch (InterruptedException e) { } } } public void run() { int status = 0; while (true) { controlsSetEnabled(false); status = waitForQuestion(); if (status == Network.SERVER_DISCONNECTED) break; AnswerRadioButtonA.setSelected(true); timeElapsed = 0; TimeoutTimer.start(); TimeoutLabel.setText("Time left: " + Integer.toString(currentQuestion.getTimeout())); waitForAnswer(); TimeoutTimer.stop(); } } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextField AddressTextField; private javax.swing.JRadioButton AnswerRadioButtonA; private javax.swing.JRadioButton AnswerRadioButtonB; private javax.swing.JRadioButton AnswerRadioButtonC; private javax.swing.JRadioButton AnswerRadioButtonD; private javax.swing.JRadioButton AnswerRadioButtonE; private javax.swing.JRadioButton AnswerRadioButtonF; private javax.swing.JLabel AnswersLabel; private javax.swing.JButton ConnectButton; private javax.swing.JLabel QuestionNameLabel; private javax.swing.JTextArea QuestionTextArea; private javax.swing.ButtonGroup RadioButtonGroup; private javax.swing.JButton SendButton; private javax.swing.JLabel StatusLabel; private javax.swing.JLabel TimeoutLabel; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JSeparator jSeparator1; // End of variables declaration//GEN-END:variables protected Network myNet; private Thread t; private javax.swing.Timer TimeoutTimer; private int answer = 0; private Question currentQuestion = null; private int timeElapsed = 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -