编写一个网络计算程序。要求:
(1)客户端界面上有两个文本框,允许用户输入两个操作数,有4个复选框,分别是加、减、乘、除,一个按钮表示发送;
(2)服务器端监听数据,接受的信息包括操作数、操作符,根据操作数的类型(int、float、double)不同,选择不同的方法进行计算;
(3)将计算结果发回客户端,客户端在文本框内显示计算结果;
可以帮我把界面做的能优化一些吗?我要求把结果也放到一个文本框里。然后当输入第一个和第二个操作数的时候,文本框前要注明是输入的是“第一个:”和“第二个:”。
能把界面的颜色做得稍微有所出入一些好吗?谢谢了版主。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class JClientSocket extends JFrame implements ActionListener{
private JTextField jtfNum1,jtfNum2;
private JComboBox jcbFlags;
private JButton jbCompute;
private JLabel labelResult;
private JMenuBar jmenuBar;
private JMenu jmenuCtrl;
private JMenuItem jmenuConnect;
private BufferedReader in;
private PrintWriter out;
private Socket socket;
private int PORT=8888;
public JClientSocket(){
super("客户端");
init();
this.setSize(350,150);
Dimension scrSize=this.getToolkit().getScreenSize();
Dimension frmSize=this.getSize();
int left=(scrSize.width-frmSize.width)/2;
int top=(scrSize.height-frmSize.height)/2;
this.setLocation(left,top);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
void init(){
jmenuBar=new JMenuBar();
jmenuCtrl=new JMenu("操作");
jmenuConnect=new JMenuItem("连接");
jmenuConnect.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.META_MASK));
jmenuConnect.addActionListener(this);
jmenuCtrl.add(jmenuConnect);
jmenuBar.add(jmenuCtrl);
this.setJMenuBar(jmenuBar);
Container container=this.getContentPane();
jtfNum1=new JTextField(10);
Vector vector=new Vector();
vector.addElement("+");
vector.addElement("-");
vector.addElement("*");
vector.addElement("/");
jcbFlags=new JComboBox(vector);
jtfNum2=new JTextField(10);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,3));
panel.add(jtfNum1);
panel.add(jcbFlags);
panel.add(jtfNum2);
container.add(panel,BorderLayout.NORTH);
labelResult=new JLabel("结果是:");
jbCompute=new JButton("计算");
jbCompute.setEnabled(false);
jbCompute.addActionListener(this);
container.add(labelResult,BorderLayout.CENTER);
JPanel panel2=new JPanel();
panel2.add(jbCompute);
container.add(panel2,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jmenuConnect){
connectSocket();
}else if(e.getSource()==jbCompute){
getInfo();
}
}
void connectSocket(){
try{
socket=new Socket(InetAddress.getByName("localhost"),PORT);
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
jbCompute.setEnabled(true);
}catch(Exception exc){
JOptionPane.showMessageDialog(null,exc.getMessage());
}
}
void getInfo(){
try{
double num1,num2;
String result;
int flags;
num1=Double.parseDouble(jtfNum1.getText());
num2=Double.parseDouble(jtfNum2.getText());
flags=jcbFlags.getSelectedIndex();
out.println(num1);
out.println(num2);
out.println(flags);
result=in.readLine();
labelResult.setText("结果是:"+result);
}catch(Exception exc){
JOptionPane.showMessageDialog(null,exc.getMessage());
}
}
public static void main(String[] args){
new JClientSocket();
}
}