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

📄 client.java

📁 一个复数的网络运算设计,用JAVA写的,我的毕业设计
💻 JAVA
字号:
package CalculateProgram;

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
class Client extends JFrame implements ActionListener{
	Socket sock;
	JTextField txtNum1 = new JTextField(10);
	JTextField txtNum2 = new JTextField(10);
	JTextField txtResult = new JTextField(10);
	JLabel lblEqual = new JLabel("=");
	JRadioButton rbtn1 = new JRadioButton("加",true);
	JRadioButton rbtn2 = new JRadioButton("减",false);
	JRadioButton rbtn3 = new JRadioButton("乘",false);
	JRadioButton rbtn4 = new JRadioButton("除",false);
	JButton b1 = new JButton("计算");
	JButton b2 = new JButton("连接服务器");
	DataOutputStream out;
	DataInputStream in;
    public Client() {
    	JPanel center = new JPanel();
    	center.setLayout(new FlowLayout(FlowLayout.LEFT));
    	center.add(txtNum1);
    	center.add(rbtn1);
    	center.add(rbtn2);
    	center.add(rbtn3);
    	center.add(rbtn4);
    	center.add(txtNum2);
    	center.add(lblEqual);
    	center.add(txtResult);
    	ButtonGroup bgp = new ButtonGroup();
    	bgp.add(rbtn1);
    	bgp.add(rbtn2);
    	bgp.add(rbtn3);
    	bgp.add(rbtn4);
    	this.getContentPane().add(center,"Center");
    	JPanel bottom = new JPanel();
    	bottom.add(b1);
    	JPanel top = new JPanel();
    	top.add(b2);
    	this.getContentPane().add(bottom,"South");
    	this.getContentPane().add(top,"North");
    	b1.addActionListener(this);
    	b2.addActionListener(this);
    	setTitle("客户端");
    	setSize(600,200);
    	setVisible(true);
    	addWindowListener(new WindowAdapter(){
    		 public void windowClosing(WindowEvent e){
    		 	try{
    		 		out.writeUTF("bye");
    		 	}
    		 	catch(Exception ee){
    		 		dispose();
    		 		System.exit(0);
    		 	}
    		 }		
    	});
    }
    public void actionPerformed(ActionEvent e){
    	if(e.getSource()==b1){
    		String digit = "^[1-9]+[0-9]+[.0-9]+$";
    		if(!txtNum1.getText().matches(digit) || !txtNum2.getText().matches(digit)){
    			JOptionPane.showMessageDialog(null, "数字格式不正确");
    			return;
    		}
    		String expression;
    		expression = txtNum1.getText().trim()+","+txtNum2.getText().trim()+",";
    		if(rbtn1.isSelected()){
    			expression += "+";
    		}else if(rbtn2.isSelected()){
    			expression += "-";
    		}else if(rbtn3.isSelected()){
    			expression += "*";
    		}else{
    			expression += "/";
    		}
    		try{
    			out.writeUTF(expression);
    		}catch(Exception ee){}
    	}else{
    		try{
    			sock = new Socket("127.0.0.1",6003);
    			OutputStream os = sock.getOutputStream();
    			out = new DataOutputStream(os);
    			InputStream is = sock.getInputStream();
    			in = new DataInputStream(is);
    			Communion th = new Communion(this);
    			th.start();
    		}catch(IOException ee){
    			JOptionPane.showMessageDialog(null,"连接服务器失败!");
    			return;
    		}
    	}
    }
    public static void main(String[] args) {
    	Client mainFrame = new Client();
    }
}

class Communion extends Thread{
	Client client;
	Communion(Client client){
		this.client = client;
	}
	public void run(){
		String msg = null;
		String result;
		while(true){
			try{
				msg = client.in.readUTF();
				if(msg.equals("bye")){
					JOptionPane.showMessageDialog(null,"服务器已经停止");
					break;
			    }else{
			    	client.txtResult.setText(msg);
			    }
			}catch(Exception e){
				break;
			}
		}
		try{
			client.out.close();
			client.in.close();
			client.sock.close();
		}
		catch(Exception e){}
	}
}

⌨️ 快捷键说明

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