import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ClientFrame extends JFrame implements Runnable,ActionListener
{
JButton connection,computer;
JTextField inputA,inputB,inputC;
JTextArea showResult;
Socket socket = null;
DataInputStream in = null;
DataOutputStream out = null;
Thread thread;
public ClientFrame()
{
// ---
socket = new Socket(); //待连接的套接字
// ---
connection = new JButton("连接服务器");
connection.addActionListener(this);
// ---
computer = new JButton("求方程的根");
computer.setEnabled(false); //没有和服务器连接之前,该按钮不可用
computer.addActionListener(this);
// ---
inputA = new JTextField("0",12);
inputB = new JTextField("0",12);
inputC = new JTextField("0",12);
// ---
Box boxV1 = Box.createVerticalBox();
boxV1.add(new JLabel("输入2次项系数"));
boxV1.add(new JLabel("输入1次项系数"));
boxV1.add(new JLabel("输入常数项"));
Box boxV2 = Box.createVerticalBox();
boxV2.add(inputA);
boxV2.add(inputB);
boxV2.add(inputC);
Box baseBox = Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(boxV2);
// ---
showResult = new JTextArea(8,18);
// ---
Container con = getContentPane();
con.setLayout(new FlowLayout());
con.add(connection);
con.add(baseBox);
con.add(computer);
con.add(new JScrollPane(showResult));
// ---
setBounds(100,100,360,310);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ---
thread = new Thread(this);
}
public void run()
{
while(true)
{
try
{
double root1 = in.readDouble(); //堵塞状态,除非读取到信息
double root2 = in.readDouble();
showResult.append("\n两个根:\n"+root1+"\n"+root2);
showResult.setCaretPosition((showResult.getText()).length());
}
catch(IOException e)
{
showResult.setText("与服务器已断开");
computer.setEnabled(false);
break;
}
}
}
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,4332);
// ---
socket.connect(socketAddress);
// ---
in=new DataInputStream(socket.getInputStream());
// ---
out=new DataOutputStream(socket.getOutputStream());
// ---
computer.setEnabled(true);
//thread.start();
}
}
catch (IOException ee)
{
System.out.println(ee);
socket=new Socket();
}
}
if(e.getSource()==computer)
{
try
{
double a = Double.parseDouble(inputA.getText()),
b = Double.parseDouble(inputB.getText()),
c = Double.parseDouble(inputC.getText());
double disk=b*b-4*a*c;
if(disk>=0)
{
out.writeDouble(a);
out.writeDouble(b);
out.writeDouble(c);
// !!!
double root1 = in.readDouble(); //堵塞状态,除非读取到信息
double root2 = in.readDouble();
showResult.append("\n两个根:\n"+root1+"\n"+root2);
showResult.setCaretPosition((showResult.getText()).length());
}
else
{
inputA.setText("此2次方程无实根");
}
}
catch(Exception ee)
{
inputA.setText("请输入数字字符");
}
}
}
public static void main(String args[])
{
ClientFrame win = new ClientFrame();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Java-Code-Chap11.zip_forth8x9_storyjk4_网络编程
共53个文件
class:26个
java:12个
prefs:5个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 61 浏览量
2022-09-23
20:58:25
上传
评论
收藏 54KB ZIP 举报
温馨提示
java界面设计的相关例子,非常丰富全面
资源推荐
资源详情
资源评论
收起资源包目录
Java-Code-Chap11.zip (53个子文件)
Example11_8
bin
BroadCast.class 2KB
Receive.class 3KB
.settings
org.eclipse.jdt.core.prefs 598B
src
BroadCast.java 1KB
BroadCast.class 1KB
Receive.java 2KB
Receive.class 3KB
.project 387B
.classpath 301B
Example11_1_2_3_4
bin
WinOne.class 3KB
Example11_3.class 403B
Example11_4.class 992B
Example11_2.class 403B
WinTwo.class 3KB
Example11_1.class 403B
NetWin.class 3KB
WinTwo$1.class 1KB
.settings
org.eclipse.jdt.core.prefs 598B
src
Example11_4.java 705B
Example11_3.java 3KB
Example11_2.java 2KB
Example11_1.java 2KB
.project 393B
.classpath 301B
Example11_7
bin
A.class 3KB
B.class 3KB
.settings
org.eclipse.jdt.core.prefs 598B
src
A.java 2KB
A.class 3KB
B.class 3KB
B.java 2KB
.project 387B
.classpath 301B
Example11_6
bin
ServerThread.class 2KB
ClientFrame.class 5KB
MutiServer.class 1KB
.settings
org.eclipse.jdt.core.prefs 598B
src
ServerThread.class 1KB
ClientFrame.java 5KB
MutiServer.java 2KB
ClientFrame.class 4KB
MutiServer.class 1KB
.project 387B
.classpath 301B
Example11_5
bin
Server.class 2KB
Client.class 2KB
.settings
org.eclipse.jdt.core.prefs 598B
src
Server.java 1KB
Server.class 2KB
Client.java 1014B
Client.class 1KB
.project 387B
.classpath 301B
共 53 条
- 1
资源评论
我虽横行却不霸道
- 粉丝: 75
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功