⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientaccount.java

📁 运用socket编程
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
public class clientAccount extends WindowAdapter implements ActionListener
{
	static final int serverPort=9999;
    static final int NUM=4;
    CheckboxGroup symbol_group;
    Label lb1,lb2,lb3;
    TextField tf_count1,tf_count2,tf_result;
   	Button bt;//,bt_out;
    Frame f; 
    public static void main(String args[])	
    {
       clientAccount cp=new clientAccount();
       cp.init();
    }
    void mainClient(String temp[])
    {
    	try
        {
        	 Socket s=new Socket("localhost",serverPort);
        	 System.out.println("Socket:"+s);
             BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
             PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())),true);
             System.out.println("start......");
             int i=0;
             while(i<temp.length)
             {
             	out.println(temp[i]);
             	i++;
             }
             while(true)
             {
                String str=in.readLine();
             	if (str==null)
             		break;
                tf_result.setText(str);
             }
             s.close();
        }
        catch(IOException e)
        {
        	 System.err.println("print error:"+e.getMessage());
        }
    }
    void init()
    {
        String symbol[]={"+","-","*","/"};
    	f=new Frame("Client Counting Program!");
    	f.setLayout(new GridLayout(6,2));
    	tf_count1=new TextField(3);
    	tf_count2=new TextField(3);
    	lb1=new Label("count1:");
    	lb2=new Label("count2:");
    	f.add(lb1);
    	f.add(tf_count1);
    	f.add(lb2);
    	f.add(tf_count2);
        symbol_group=new CheckboxGroup();
        Checkbox radio[]=new Checkbox[NUM];
        for(int i=0;i<NUM;i++)
        {
        	radio[i]=new Checkbox(symbol[i],symbol_group,false);
        	f.add(radio[i]);
        	
        }
      
       bt=new Button("Submit");
       lb3=new Label("result:");
       tf_result=new TextField(3);
       f.add(lb3);
       f.add(tf_result);
       f.add(bt);
       //bt_out=new Button("Out");
      // f.add(bt_out);
       bt.addActionListener(this);
       f.addWindowListener(this);
       f.setSize(300,200);
       f.setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
      // Object o=e.getSource();
       //if(o==bt)
       
       	
	       String temp[]=new String[3];
	       Checkbox cbx=(Checkbox)symbol_group.getSelectedCheckbox(); 
	       temp[0]=cbx.getLabel();
	       temp[1]=tf_count1.getText();
	       temp[2]=tf_count2.getText();
	       if((temp[0]!=null) && (temp[1]!=null) && (temp[2]!=null))
	          {
	          	  mainClient(temp);
	          }
	           
	       else 
	       	tf_result.setText("Content can't null!" );
	   
	   
    }
    public void windowClosing(WindowEvent e)
    {
        System.exit(0);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -