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;
}
}
}
}