?? testpanel.java~28~
字號:
package testsystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class TestPanel extends JPanel implements ActionListener,Runnable
{
private Socket connectToServer;
private DataInputStream inFromServer;
private DataOutputStream outToServer;
private Thread thread;
private Timer testTimer;
String inStr="";
private JButton startButton;
private JLabel timeLabel;
private JTextArea questionArea;
private Checkbox radioButton[]=new Checkbox[4];
private CheckboxGroup buttonGroup=new CheckboxGroup();
private JButton answerButton;
private JButton questionButton;
private JButton scoreButton;
String s[]={"A","B","C","D"};
public TestPanel(InetAddress addr,int port)
{
initPanelGUI();
try {
connectToServer=new Socket(addr,port);
inFromServer=new DataInputStream(connectToServer.getInputStream());
outToServer=new DataOutputStream(connectToServer.getOutputStream());
System.out.println(connectToServer.getInetAddress());
} catch (IOException ex) {
System.out.println("TestPanel連接錯誤");
startButton.setEnabled(false);
}
thread=new Thread(this);
thread.start();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==startButton)
{
startButtonPerformed();
}
/* if(e.getSource()==testTimer)
{
testTimerPerformed();
}*/
if(e.getSource()==questionButton)
{
questionButtonPerformed();
}
if(e.getSource()==answerButton)
{
answerButtonPerformed();
}
if(e.getSource()==scoreButton)
{
scoreButtonPerformed();
}
}
public void run()
{
while(true)
{
try {
inStr=inFromServer.readUTF();
if(inStr.startsWith("考試時間"))
{
System.out.println(inStr.indexOf('@')+1);
inStr=inStr.substring(inStr.indexOf('@')+1);
// testTime=Integer.parseInt(inStr);
timeLabel.setText(inStr);
System.out.println(inStr);
}
if(inStr.startsWith("下一題"))
{
for(int i=0;i<radioButton.length;i++)
{
radioButton[i]=new Checkbox(s[i],buttonGroup,false);
}
inStr=inStr.substring(inStr.indexOf("@")+1);
questionArea.setText(inStr);
if(inStr.startsWith("試題結束"))
{
testTimer.stop();
questionButton.setEnabled(false);
answerButton.setEnabled(false);
scoreButton.setEnabled(true);
}
}
if(inStr.startsWith("成績"))
{
JOptionPane.showMessageDialog(null,inStr,"成績顯示",JOptionPane.INFORMATION_MESSAGE);
socketClosing();
}
} catch (Exception ex) {
socketClosing();
questionArea.setText("服務器連接終止");
break;
}
}
}
private void initPanelGUI()
{
setLayout(new BorderLayout());
JPanel northPanel=new JPanel();
northPanel.setLayout(new GridLayout(2,1));
startButton=new JButton("開始考試");
startButton.addActionListener(this);
timeLabel=new JLabel("考試剩余時間");
northPanel.add(startButton);
northPanel.add(timeLabel);
add(northPanel,BorderLayout.NORTH);
questionArea=new JTextArea(30,10);
questionArea.setLineWrap(true);
questionArea.setWrapStyleWord(true);
questionArea.setFont(new Font("幼圓",Font.PLAIN,16));
int vScroll=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int hScroll=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;
add(new JScrollPane(questionArea,vScroll,hScroll),BorderLayout.CENTER);
JPanel southPanel=new JPanel();
JPanel radioPanel=new JPanel();
for(int i=0;i<radioButton.length;i++)
{
radioButton[i]=new Checkbox(s[i],buttonGroup,false);
radioPanel.add(radioButton[i]);
}
answerButton=new JButton("提交答案");
answerButton.setEnabled(false);
answerButton.addActionListener(this);
questionButton=new JButton("下一題");
questionButton.setEnabled(false);
questionButton.addActionListener(this);
scoreButton=new JButton("成績");
scoreButton.setEnabled(false);
scoreButton.addActionListener(this);
southPanel.add(radioPanel);
southPanel.add(answerButton);
southPanel.add(questionButton);
southPanel.add(scoreButton);
add(southPanel,BorderLayout.SOUTH);
}
private void startButtonPerformed()
{
startButton.setEnabled(true);
questionButton.setEnabled(false);
answerButton.setEnabled(true);
try {
outToServer.writeUTF("開始考試");
} catch (Exception ex) {
System.out.println("向服務器寫\"開始考試\"失敗");
}
}
private void questionButtonPerformed()
{
questionButton.setEnabled(false);
answerButton.setEnabled(true);
try {
outToServer.writeUTF("下一題");
} catch (Exception ex) {
System.out.println("向服務器寫\"下一題\"失敗");
}
}
private void answerButtonPerformed()
{
String answer="";
questionButton.setEnabled(true);
answerButton.setEnabled(false);
for(int i=0;i<radioButton.length;i++)
{
if(radioButton[i].getState())
{
answer=radioButton[i].getLabel();
break;
}
}
try {
outToServer.writeUTF("提交答案@"+answer);
} catch (Exception e) {
System.out.println("向服務器\"提交答案\"失敗");
}
}
private void scoreButtonPerformed()
{
try {
scoreButton.setEnabled(false);
outToServer.writeUTF("成績");
} catch (Exception ex) {
System.out.println("要求服務器發送\" 成績\"失敗");
}
}
private void socketClosing()
{
try {
inFromServer.close();
outToServer.close();
connectToServer.close();
} catch (Exception ex) {
System.out.println("關閉socket發生異常!");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -