📄 talkclientcs.java
字号:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TalkClientcs
{
JFrame jf;
JPanel jp1;
JPanel jp2;
JPanel jp10;
JPanel jp11;
JPanel jp12;
JPanel jp13;
JPanel jp21;
JPanel jp22;
JPanel jp211;
Checkbox a,b,c,d;
CheckboxGroup cbg;
JTextField jp22jtf,jp11jtf;
TextArea jp21ta1,jp21ta2;
Button jp211b1,jp211b2,jp211b3,jp11b,jp13b,jp2b;
JLabel jp1jl,jp2jl1,jp2jl2,jp2jl3;
Choice ch;
private PrintWriter output;
private BufferedReader input;
//private ServerSocket ss;
private Socket Client;
private String message="";
String[] srl,srlst;
boolean changed=false;
int t=0;
String daan,shijian;
int df;
StringBuffer ra=new StringBuffer("");
StringBuffer ya=new StringBuffer("");
int delay =3600;
CardLayout card;
Timer tx;
//private int counter = 1;
public TalkClientcs()
{
jf=new JFrame("英语标准化考试系统");
jp1=new JPanel();
jp2=new JPanel();
jf.setSize(800,600);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(new BorderLayout());
createjp1();
jf.add(jp1,BorderLayout.WEST);
createjp2();
jf.add(jp2,BorderLayout.CENTER);
jf.setVisible(true);
}
void createjp1(){
jp1.setSize(700,520);
jp1.setLayout(new GridLayout(5,1));
createjp10();
jp1.add(jp10);
createjp11();
jp1.add(jp11);
createjp12();
jp1.add(jp12);
jp1jl=new JLabel("选择一个文件以后,单击开始考试按纽",SwingConstants.CENTER);
jp1jl.setVisible(false);
jp1.add(jp1jl);
createjp13();
jp13.setVisible(false);
jp1.add(jp13);
}
void createjp10(){
jp10=new JPanel();
jp10.setLayout(new FlowLayout(FlowLayout.CENTER));
jp10.add(new JLabel("输入服务器的IP或者域名,如192.168.0.218",SwingConstants.CENTER));
}
void createjp11(){
jp11=new JPanel();
jp11.setLayout(new FlowLayout(FlowLayout.CENTER));
jp11jtf=new JTextField(15);
jp11.add(jp11jtf);
jp11b=new Button("呼叫");
jp11b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try{
if(!jp11jtf.getText().equals(""))
{Client=new Socket(jp11jtf.getText(),5000);
if(Client.isConnected())
{
jp2jl2.setText("您呼叫:"+jp11jtf.getText()+"成功");
card.show(jp12,"c2");
jp1jl.setVisible(true);
jp13.setVisible(true);
}
}
}
catch(Exception e1)
{
e1.printStackTrace();
}
}
});
jp11.add(jp11b);
}
void createjp12(){
jp12=new JPanel();
card=new CardLayout();
jp12.setLayout(card);
jp2jl1=new JLabel("提示是否连接成功的标签",SwingConstants.CENTER);
jp2jl2=new JLabel("呼叫成功",SwingConstants.CENTER);
jp2jl3=new JLabel("呼叫不成功",SwingConstants.CENTER);
jp12.add(jp2jl1,"c1");
jp12.add(jp2jl2,"c2");
jp12.add(jp2jl3,"c3");
card.show(jp12,"c1");
}
void createjp13(){
jp13=new JPanel();
jp13b=new Button("将试题文件添加到下拉列表中");
jp13.add(jp13b);
jp13b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try{
getStreams();
output.println("Client>>要求发送试卷名");
output.flush();
message=input.readLine();
srl=message.split(",");
for(int i=0;i<srl.length;i++){
ch.addItem(srl[i]);
}
}
catch(Exception e2)
{
e2.printStackTrace();
}
}
});
ch=new Choice();
ch.addItem("");
// ch.addItem("1.txt");
// ch.addItem("2.txt");
// ch.addItem("3.txt");
// ch.addItem("java.txt");//remove(Component comp)
jp13.add(ch);
}
void createjp2(){
jp2=new JPanel();
jp2.setLayout(new BorderLayout());
jp2b=new Button("开始考试");
jp2b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jp22.setVisible(true);
if(delay==0) {jp22jtf.setText(Timep(delay));tx.stop();}
jp22jtf.setText(Timep(delay)); delay--;
if(delay==0){
try{
jp211b2.setEnabled(false);
jp211b1.setEnabled(false);
closeConnection();
}
catch(Exception e0)
{
e0.printStackTrace();
}
}
}
};
tx=new Timer(1000, taskPerformer);
if(delay>0){tx.start();}
}
});
jp2b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try{
jp21ta2.setText("您选择了试题:"+ch.getSelectedItem()+"\n");
jp21.setVisible(true);
output.println("Client>>要求发送试卷内容");
output.flush();
output.println(ch.getSelectedItem());
output.flush();
message=input.readLine();
daan=message;
message=input.readLine();
shijian=message;
delay=Integer.parseInt(shijian)*60;
message=input.readLine();
srlst=message.split("##");
System.out.println("问题的length="+srlst.length);
//发完以后立刻关闭socket
output.close();
input.close();
Client.close();
for(int m=0;m<srlst.length;m++)
{System.out.println(srlst[m]);}
}
catch(Exception e3)
{
e3.printStackTrace();
}
}
});
jp2.add(jp2b,BorderLayout.NORTH);
createjp21();
jp2.add(jp21,BorderLayout.CENTER);
jp21.setVisible(false);
createjp22();
jp2.add(jp22,BorderLayout.SOUTH);
jp22.setVisible(false);
}
void createjp21(){
jp21=new JPanel();
jp21.setLayout(new BorderLayout(3,3));
jp21ta1=new TextArea("单击查看得分按钮,可以在这里看到得分和正确答案");
jp21ta1.setForeground(Color.RED);
jp21ta2=new TextArea("你选择了java.txt");
jp21.add(jp21ta1,BorderLayout.NORTH);
jp21.add(jp21ta2,BorderLayout.CENTER);
createjp211();
jp21.add(jp211,BorderLayout.SOUTH);
}
void createjp211(){
jp211=new JPanel();
cbg=new CheckboxGroup();
a=new Checkbox("A",cbg,true);
b=new Checkbox("B",cbg,false);
c=new Checkbox("C",cbg,false);
d=new Checkbox("D",cbg,false);
jp211.add(a);
jp211.add(b);
jp211.add(c);
jp211.add(d);
jp211b1=new Button("提交该题答案");
jp211b1.setEnabled(false);
jp211b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String code=String.valueOf(daan.charAt(t));
String you=cbg.getSelectedCheckbox().getLabel();
if(code.equalsIgnoreCase(you)){df++;}
ra.append(code);ya.append(you);
t++;
jp211b1.setEnabled(false);
jp211b2.setEnabled(true);
}
});
jp211b2=new Button("读取下一题");
jp211b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try{
if(t==srlst.length) {
t=0;
closeConnection();
jp211b2.setEnabled(false);
jp211b1.setEnabled(false);
}
else{
jp21ta2.setText(srlst[t]);
jp211b2.setEnabled(false);
jp211b1.setEnabled(true);
}
}
catch(Exception e4)
{
e4.printStackTrace();
}
}
});
jp211b3=new Button("查看得分");
jp211b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
jp21ta1.setText("分数:"+df+"\n"+"正确答案:"+ra.toString()+"\n"+"你的答案:"+ya.toString()+"\n");
}
});
jp211.add(jp211b1);
jp211.add(jp211b2);
jp211.add(jp211b3);
}
void createjp22(){
jp22=new JPanel();
jp22.setLayout(new FlowLayout(FlowLayout.CENTER));
jp22jtf=new JTextField("显示考试用时(倒计时)",20);
jp22jtf.setForeground(Color.RED);
jp22.add(jp22jtf);
}
String Timep(int miao){
StringBuffer guodu=new StringBuffer("考试剩余时间:");
int xiaoshi=miao/3600;
int xiaoshihou=miao%3600;
int fenzhong=xiaoshihou/60;
int fenzhonghou=xiaoshihou%60;
guodu.append(String.valueOf(xiaoshi));
guodu.append("小时");
guodu.append(String.valueOf(fenzhong));
guodu.append("分钟");
guodu.append(String.valueOf(fenzhonghou));
guodu.append("秒");
return guodu.toString();
}
private void getStreams() throws IOException
{
output = new PrintWriter(Client.getOutputStream());
input = new BufferedReader(new InputStreamReader(Client.getInputStream()));
}
private void processConnection() throws IOException
{
do{
try{
//要做的首先是显示jp1的其他信息,然后再从服务器端读入文件名列表
jp2jl2.setText("您呼叫:"+jp11jtf.getText()+"成功");
card.show(jp12,"c2");
jp1jl.setVisible(true);
//这里先显示,等按下去以后再在后面做读
jp13.setVisible(true);
}
catch(Exception e)
{
e.printStackTrace();
}
}
while(!message.equals("CLIENT>> bye"));
}
private void closeConnection() throws IOException
{
ra.replace(0,ra.length(),daan);
jp21ta2.setText("考试结束");
//output.close();
// input.close();
//Client.close();
}
public static void main(String args[]) throws Exception
{
TalkClientcs tccs = new TalkClientcs();
tccs.jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -