日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java GUI,贷款服务器

發布時間:2024/8/24 编程问答 32 如意码农
生活随笔 收集整理的這篇文章主要介紹了 java GUI,贷款服务器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本習題來自《java語言程序設計--進階篇》第30章,網絡編程的習題。

題目描述:為一個客戶端編寫一個服務器。客戶端向服務器發送貸款信息(年利率、貸款年限和貸款總額)。服務器計算月償還額和總償還額,并把它們發回給客戶端。將客戶端程序命名為:Exercise30_1Client,將服務器程序命名為Exercise30_1Server。

package exercise.chapter30;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

/*
 * 貸款客戶端
 * 日期:2015.5.17
 */

public class Exercise30_1Client extends JFrame {
	private JPanel center = new JPanel();
	private JPanel south = new JPanel();

	private JTextArea jta = new JTextArea(8, 10);
	private JButton jbt = new JButton("Submit");

	//創建標簽和文本域
	private JLabel jlb1 = new JLabel("稅率");
	private JTextField rate = new JTextField();
	private JLabel jlb2 = new JLabel("貸款年數");
	private JTextField year = new JTextField();
	private JLabel jlb3 = new JLabel("貸款總數");
	private JTextField amount = new JTextField();

	public Exercise30_1Client() {
		//設計GUI布局
		center.setLayout(new GridLayout(3, 2));
		south.setLayout(new BorderLayout());

		center.add(jlb1);
		center.add(rate);
		center.add(jlb2);
		center.add(year);
		center.add(jlb3);
		center.add(amount);

		jta.setEditable(false);
		jta.setFont(new Font("", Font.BOLD, 15));
		south.add(new JScrollPane(jta), BorderLayout.CENTER);
		south.add(jbt, BorderLayout.EAST);

		this.setLayout(new BorderLayout());
		this.add(center, BorderLayout.CENTER);
		this.add(south, BorderLayout.SOUTH);

		this.setTitle("Exercise30_1Client");
		this.setSize(400, 300);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//設計socket
		try {
			Socket socket = new Socket("localhost", 8000);
			jbt.addActionListener(new MyActionListener(socket));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new Exercise30_1Client();
	}

	private class MyActionListener implements ActionListener {
		Socket socket = null;

		public MyActionListener(Socket socket) {
			this.socket = socket;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				DataOutputStream output = new DataOutputStream(socket.getOutputStream());
				DataInputStream input = new DataInputStream(socket.getInputStream());
				double rateStr = Double.parseDouble(rate.getText().trim());
				double yearStr = Double.parseDouble(year.getText().trim());
				double amountStr = Double.parseDouble(amount.getText().trim());
				String message = rateStr + "@" + yearStr + "@" + amountStr;

				output.write(message.getBytes());
				output.flush();

				byte[] buf = new byte[1024];
				int len = 0;

				//文本域清零
				rate.setText("");
				year.setText("");
				amount.setText("");

				double amount = input.readDouble();
				//向jta中追加數據
				jta.append("稅率: " + rateStr + ", 貸款年數:" + yearStr + ", 貸款總額: " +
						amountStr + " ,償還總額:" + amount);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
/*
 *功能:貸款服務器
 *日期:2015.5.17
 */

public class Exercise30_1Server extends JFrame {
	private JTextArea jta = new JTextArea();

	public Exercise30_1Server() {
		this.setTitle("Exercise30_1Server");
		this.setSize(400, 300);
		this.setResizable(false);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		jta.setFont(new Font("Font.ITALIC",Font.PLAIN, 15));
		jta.setEditable(false);
		this.setLayout(new BorderLayout());
		this.add(new JScrollPane(jta), BorderLayout.CENTER);

		jta.append("Server started at " + new Date() + '\n');

		try {
			ServerSocket ss = new ServerSocket(8000);
			Socket socket = ss.accept();

			//獲取InetAddress類
			InetAddress inetAddress = socket.getInetAddress();
			jta.append("Client's hostname is: " + inetAddress.getHostName());
			jta.append("\n");
			jta.append("Client's ip is: " + inetAddress.getHostAddress());
			jta.append("\n");

			DataInputStream input = new DataInputStream(socket.getInputStream());
			DataOutputStream output = new DataOutputStream(socket.getOutputStream());
			byte[] buf = new byte[1024];
			int len = 0;
			while((len = input.read(buf)) != -1) {
				double amount = getAmount(new String(buf, 0, len));
				output.writeDouble(amount);

				jta.append(new String(buf, 0, len));
				jta.append("\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static double getAmount(String str) {
		String[] str1 = str.split("@");

		double rate = Double.parseDouble(str1[0]);
		double years = Double.parseDouble(str1[1]);
		double amount = Double.parseDouble(str1[2]);

		return rate * years * amount + amount;
	}

	public static void main(String[] args) {
		JFrame frame = new Exercise30_1Server();
	}
}

總結

以上是生活随笔為你收集整理的java GUI,贷款服务器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。