JAVA (网络编程---求圆的面积)

1.关于jbt.setEnabled(false);   jbt在这个地方是一个按钮组件
设置控件是否可用,就是你在用一些软件的时候有些菜单什么的是灰色的。
在javax.swing包中比如你写个管理软件,有好几种用户,有些用户你不想让他拥有某些权限,你就可以把那些功能菜单给禁用掉。

true 表示启用此 Action;为 false 表示禁用它

首先是声明一个ServerSocket ss; 对象,这里ss为null因为没有创建,然后判断
if(ss==null) 则ss = new ServerSocket(9999); 这样 ss就创建出来了,
startBtn.setEnabled(false);这样就使得startBtn这个按钮变灰不可点击了。

判断ss==null是因为,你的baibutton的监听事件只要du确认一次服务器就可以了,不用重复的,
setEnabled(false)是使你的button按钮处于不可点击状态,值为true的时候是可点的(也是默认的)

在这里插入图片描述
在这里插入图片描述

Server类

package circle;

import java.io.*;
import java.net.*;
import java.util.*;

public class Server {
	public static void main(String args[]) {
		ServerSocket server = null;
		ServerThread thread;
		Socket you = null;
		while (true) {
			try {
				server = new ServerSocket(4331);
			} catch (IOException e1) {
				System.out.println("正在监听"); // ServerSocket对象不能重复创建
			}
			try {
				System.out.println(" 等待客户呼叫");
				you = server.accept();
				System.out.println("客户的地址:" + you.getInetAddress());
			} catch (IOException e) {
				System.out.println("正在等待客户");
			}
			if (you != null) {
				new ServerThread(you).start(); // 为每个客户启动一个专门的线程
			}
		}
	}
}

class ServerThread extends Thread {
	Socket socket;
	DataOutputStream out = null;
	DataInputStream in = null;
	String s = null;

	ServerThread(Socket t) {
		socket = t;
		try {
			out = new DataOutputStream(socket.getOutputStream());
			in = new DataInputStream(socket.getInputStream());
		} catch (IOException e) {
		}
	}

	public void run() {
		while (true) {
			try {
				double r = in.readDouble();// 堵塞状态,除非读取到信息
				// double r=Double.parseDouble(s);
				double area = Math.PI * r * r;
				out.writeUTF("半径是:" + r + "的圆的面积:" + area);
			} catch (IOException e) {
				System.out.println("客户离开");
				return;
			}
		}
	}
}

Client类

package circle;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 
public class Client {
   public static void main(String args[]) {
     new WindowClient();
   }
}
class  WindowClient extends JFrame implements Runnable,ActionListener {
   JButton connection,send;
   JTextField inputText;
   JTextArea showResult;
   Socket socket=null;
   DataInputStream in=null;
   DataOutputStream out=null;
   Thread thread; 
   WindowClient() {
      socket=new Socket();
      connection=new JButton("连接服务器");
      send=new JButton("发送");
      send.setEnabled(false);
      inputText=new JTextField(6);
      showResult=new JTextArea();
      add(connection,BorderLayout.NORTH);
      JPanel pSouth=new JPanel();
      pSouth.add(new JLabel("输入圆的半径:"));
      pSouth.add(inputText);
      pSouth.add(send);
      add(new JScrollPane(showResult),BorderLayout.CENTER);
      add(pSouth,BorderLayout.SOUTH);
      connection.addActionListener(this);
      send.addActionListener(this);
      thread=new Thread(this); 
      setBounds(10,30,460,400);
      setVisible(true);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
   public void actionPerformed(ActionEvent e) {
      if(e.getSource()==connection) {
        try { //请求和服务器建立套接字连接:
           if(socket.isConnected()){}
           else{
              InetAddress  address=InetAddress.getByName("127.0.0.1");
              InetSocketAddress socketAddress=new InetSocketAddress(address,4331);
              socket.connect(socketAddress); 
              in =new DataInputStream(socket.getInputStream());
              out = new DataOutputStream(socket.getOutputStream());
              send.setEnabled(true);
              if(!(thread.isAlive()))
                 thread=new Thread(this);
              thread.start();
           }
        } 
        catch (IOException ee) {
           System.out.println(ee);
           socket=new Socket();
        }
      }
      if(e.getSource()==send) {
         String s=inputText.getText();
         double r=Double.parseDouble(s);
         try { out.writeDouble(r);
         }
         catch(IOException e1){} 
      }
   }
   public void run() {
      String s=null;
      double result=0;
      while(true) {
        try{ s=in.readUTF();
             showResult.append("\n"+s);
        }
        catch(IOException e) { 
             showResult.setText("与服务器已断开"+e);
             socket=new Socket();
             break;
        }   
      }
   }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值