Java:客户端/服务器问题

Java:客户端/服务器问题,java,button,textfield,windowbuilder,Java,Button,Textfield,Windowbuilder,实际问题是: 开发一个计算员工薪酬的客户机/服务器应用程序。 有些员工是永久性的,有些是临时性的 永久雇员有固定的年薪,有资格享受病假和年假 临时雇员按实际工作时间按小时计酬 雇员每两周领一次工资 应用程序的客户端是收集有关员工的信息,并将其发送到服务器,服务器将计算总工资、已缴税款和净工资 此信息将发送回要显示的客户端 客户端界面GUI,允许用户在永久或临时之间进行选择。如果是永久性的,则要求用户输入员工姓名、身份证号码、年薪以及休假下拉列表,默认为“无”,但可以选择“年假”或“病假”。如果选

实际问题是: 开发一个计算员工薪酬的客户机/服务器应用程序。 有些员工是永久性的,有些是临时性的

永久雇员有固定的年薪,有资格享受病假和年假

临时雇员按实际工作时间按小时计酬

雇员每两周领一次工资

应用程序的客户端是收集有关员工的信息,并将其发送到服务器,服务器将计算总工资、已缴税款和净工资

此信息将发送回要显示的客户端

客户端界面GUI,允许用户在永久或临时之间进行选择。如果是永久性的,则要求用户输入员工姓名、身份证号码、年薪以及休假下拉列表,默认为“无”,但可以选择“年假”或“病假”。如果选择其中一个,则会询问用户需要多少天

如果是临时工,则输入员工姓名、身份证号码、工资率和工作时间。这两类员工的计税公式相同。这些数字是每两周一次

第一个700美元是免税的,任何收入超过700美元到1500美元的都要按每美元19美分的税率纳税,任何超过1500美元的都要按每美元38美分的税率纳税。应用程序包括数据验证。这应该在数据发送到服务器之前完成。所有数字输入应大于0,临时工的工作小时数必须为5小时或以上且小于72小时。应向用户显示一条消息,然后用户应能够重新输入数据

我尽了最大努力,有人或任何人能让这件事变得简单吗?因为我花了好几个小时、好几个小时、好几个小时、好几个小时,它有问题:

服务器:

 package com.test;    
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.awt.*;
    import javax.swing.*;

    public class PizzaPayServer extends JFrame
    {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        //Text area for displaying contents
        private JTextArea jta = new JTextArea();

        public PizzaPayServer()
        {
            //Place text area on the frame
            getContentPane().setLayout(new BorderLayout());
            getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER);

            setTitle("PizzaPayServer");
            setSize(500, 300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true); // It is necessary to show the frame here!
        }

        public static void main(String[] args) 
        {
            ServerSocket serverSocket;
            Socket socket;
            DataInputStream inputFromClient;
            DataOutputStream outputToClient;

            String  empname;


            int empid;
            double hours;
            double payrate;
            double salary;
            double grossPay;
            double netPay;
            double tax;

            PizzaPayServer theServer = new PizzaPayServer();

            try
            {
                // Create a server socket
                serverSocket = new ServerSocket(8010);
                theServer.jta.append("Server started at " + new Date() + '\n');

                //Listen for a connection request
                socket = serverSocket.accept();

                //Create data input and output streams
                inputFromClient = new DataInputStream(socket.getInputStream());
                outputToClient = new DataOutputStream(socket.getOutputStream());



                while (true)
                {

                    hours=0;
                    payrate=0;
                    //Receive employee name from client
                    empname = inputFromClient.readUTF();
                    //Receive employee id from client
                    empid = inputFromClient.readInt();
                    //Receive payrate from client

                    //Receive hours from client
                    tax=0;
                    netPay=0;
                    salary=0;

                    String[] parts = empname.split(",");
                    String part1 = parts[0]; 
                    String part2 = parts[1]; 

                    if (part2.equals("c")){
                        payrate = inputFromClient.readDouble();
                        hours = inputFromClient.readDouble();
                    //Compute pay
                    grossPay = payrate * hours;
                    if(grossPay<=700)
                        tax=0;
                    else
                        if(grossPay<=1500)
                            tax=(grossPay-700) *0.19;
                            else
                                tax=((grossPay-1500) *0.38) + (800 *0.19);
                    netPay= grossPay-tax;
                    //Send pay back to client
                    outputToClient.writeDouble(netPay);
                    outputToClient.writeUTF(empname);

                    outputToClient.writeInt(empid);



                    theServer.jta.append("Employee Name recieved from client: " + part1 + '\n');
                    theServer.jta.append("Employee ID recieved from client: " + empid + '\n');
                    theServer.jta.append("Payrate recieved from client: " + payrate + '\n');
                    theServer.jta.append("Hours recieved from client: " + hours + '\n');
                    theServer.jta.append("Pay Calculated and Sent: " + netPay + '\n');
                    }

                    //perminPayClient Method
                    if (part2.equals("p")){
                        payrate = inputFromClient.readDouble();

                        //Compute pay

                        salary=payrate;

                        if(salary<=700)
                            tax=0;
                        else
                            if(salary<=1500)
                                tax=(salary-700) *0.19;
                                else
                                    tax=((salary-1500) *0.38) + (800 *0.19);
                        netPay= salary/26 -tax;

                        //Send pay back to client
                        outputToClient.writeDouble(salary);
                        outputToClient.writeUTF(empname);

                        outputToClient.writeInt(empid);



                        theServer.jta.append("Employee Name recieved from client: " + part1 + '\n');
                        theServer.jta.append("Employee ID recieved from client: " + empid + '\n');
                        theServer.jta.append("Salary recieved from client: " + salary + '\n');
                        theServer.jta.append("Pay Calculated and Sent: " + netPay + '\n');

                        }
                    }



            }


            catch(IOException ex)
            {
                System.err.println("Catch While");
            }

            System.exit(0);

                }}
休闲:

package com.test;

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

    public class PizzaPayClient extends JFrame implements ActionListener 
    {
        /**
         * 
         */
        //Variables

        private static final long serialVersionUID = 1L;
        //Text fields for receiving employee name, employee id, payrate, hours
        private  JTextField jtfEname = new JTextField();
        private JTextField jtfEid = new JTextField();
        private JTextField jtfPayRate = new JTextField();
        //Buttons for onClick Listeners
        private JButton calculate = new JButton("Calculate Pay");
        private JButton clear = new JButton("Clear");
        private JButton exit = new JButton("Exit");

        // IO Streams
        private DataOutputStream outputToServer;
        private DataInputStream inputFromServer;
        private Socket sock;
        private LayoutManager FlowLayout;
        private final JTextField jtfHours = new JTextField();
        private final JTextArea jta = new JTextArea();
        private final JLabel lblNewLabel = new JLabel("New label");

        public PizzaPayClient()
        {
            // Panel p to hold the label and text field
            jtfHours.setHorizontalAlignment(SwingConstants.LEFT);
            jtfHours.setBounds(134, 84, 140, 20);
            jtfHours.setColumns(10);
            JPanel p = new JPanel();
            p.setBackground(new Color(255, 255, 102));
            p.setBounds(0, 0, 501, 453);
            p.setLayout(FlowLayout);
            p.setLayout(null);
            p.setLayout(null);
            p.setLayout(null);
            p.setLayout(null);
            p.setLayout(null);
            JLabel lblEmpName = new JLabel("Employee Name:");
            lblEmpName.setFont(new Font("Tahoma", Font.BOLD, 12));
            lblEmpName.setBounds(9, 9, 103, 14);
            p.add(lblEmpName);
            exit.setFont(new Font("Tahoma", Font.BOLD, 12));
            exit.setBounds(376, 126, 108, 23);
            p.add(exit);
            exit.addActionListener(this);
            JLabel lblEmpId = new JLabel("Employee ID:");
            lblEmpId.setFont(new Font("Tahoma", Font.BOLD, 12));
            lblEmpId.setBounds(10, 37, 102, 14);
            p.add(lblEmpId);
            jtfEid.setBounds(134, 34, 140, 20);
            p.add(jtfEid);
            JLabel lblPayRate = new JLabel("Enter PayRate:");
            lblPayRate.setFont(new Font("Tahoma", Font.BOLD, 12));
            lblPayRate.setBounds(9, 62, 103, 14);
            p.add(lblPayRate);
            jtfPayRate.setBounds(133, 59, 141, 20);
            p.add(jtfPayRate);
            jtfPayRate.setHorizontalAlignment(SwingConstants.LEFT);
            jtfPayRate.addActionListener(this);
            jtfEname.setBounds(134, 6, 140, 20);
            p.add(jtfEname);
            jtfEname.setHorizontalAlignment(SwingConstants.LEFT);

            // Register Listener/s
            jtfEname.addActionListener(this);
            getContentPane().setLayout(null);
            JLabel lblHours = new JLabel("Enter Hours:");
            lblHours.setFont(new Font("Tahoma", Font.BOLD, 12));
            lblHours.setBounds(9, 87, 103, 14);
            p.add(lblHours);
            calculate.setFont(new Font("Tahoma", Font.BOLD, 12));
            calculate.setBounds(73, 126, 180, 23);
            p.add(calculate);
            clear.setFont(new Font("Tahoma", Font.BOLD, 12));
            clear.setBounds(263, 126, 103, 23);
            p.add(clear);
            jtfEid.setHorizontalAlignment(SwingConstants.LEFT);
            getContentPane().add(p);

            p.add(jtfHours);
            jta.setLineWrap(true);
            jta.setRows(20);
            jta.setBounds(10, 160, 480, 282);

            p.add(jta);
            lblNewLabel.setIcon(new ImageIcon("C:\\Users\\Little Beast\\workspace\\PizzaPaySystem\\pizzaplace2.jpg"));
            lblNewLabel.setBounds(298, 9, 192, 106);

            p.add(lblNewLabel);
            jtfEid.addActionListener(this);
            calculate.addActionListener(this);
            clear.addActionListener(this);
            setTitle("Casual Pay Entry");
            setSize(517, 491);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true); // It is necessary to show the frame here!

            try
            {
                //Create a socket to connect to the server
                sock = new Socket("localhost", 8010);

                //Create an input stream to receive data from the server
                inputFromServer = new DataInputStream(sock.getInputStream());

                //Create an output stream to send data to the server
                outputToServer = new DataOutputStream(sock.getOutputStream());
            }
            catch (IOException ex)
            {
                jta.append(ex.toString() + '\n');
            }
        }

        /**
         * @param args
         */
        public static void main(String[] args) 
        {
            new PizzaPayClient();
        }

        public void actionPerformed(ActionEvent e) {

            //String actionCommmand = e.getActionCommand();
            if (e.getSource().equals(calculate)) {
                try {
                    //Get the employee name, employee id, payrate, hours from the text fields

                    String empname = jtfEname.getText();
                    //If no name is entered display pop up dialog box.
                    if(empname.length()==0)
                        JOptionPane.showMessageDialog(null, "Please Enter Your Name");


                    int empid = 0; 
                    try {
                        empid = Integer.parseInt(jtfEid.getText().trim());
                      //If no employee Id is entered display pop up dialog box
                    } catch(NumberFormatException nfe) {
                        JOptionPane.showMessageDialog(null, "Please Enter Your Id");
                    }

                    double payrate = 0;
                    try {
                        payrate = Double.parseDouble(jtfPayRate.getText().trim());
                      //If no payrate is entered display pop up dialog box
                    } catch (NumberFormatException nfe) {
                        JOptionPane.showMessageDialog(null, "Please Enter Your Pay Rate");
                    }

                    double hours = 0;
                    try {
                        hours = Double.parseDouble(jtfHours.getText().trim());
                    //If hours are less than 5 or over 72 display pop up dialog box
                    if(hours <5)
                        JOptionPane.showMessageDialog(null, "Must be 5 hours or greater");
                    if(hours >72)
                        JOptionPane.showMessageDialog(null, "Hours must be less than 72");
                    //If no hours are entered display pop up dialog box
                    } catch (NumberFormatException nfe) {
                        JOptionPane.showMessageDialog(null, "Please Enter Your Hours");
                    }

                    String oldName=empname;

                    //send the employee name, employee id, payrate, hours to the server
                    outputToServer.writeUTF(empname+",c");
                    outputToServer.writeInt(empid);
                    outputToServer.writeDouble(hours);
                    outputToServer.writeDouble(payrate);
                    outputToServer.flush();

                    //Get pay from the server
                    double pay = inputFromServer.readDouble();
                    empname = inputFromServer.readUTF();


                    //Display to the text area
                    jta.append("Name " + oldName + '\n');
                    jta.append("ID " + empid + '\n');
                    jta.append("PayRate is " + payrate + '\n');
                    jta.append("Hours is " + hours + '\n');
                    jta.append("Total Pay "
                            + pay + '\n');
                    jtfEname.setText("");
                    jtfEid.setText("");
                    jtfPayRate.setText("");
                    jtfHours.setText("");


                }
                catch (IOException ex) {
                    System.err.println(ex);
                }
            }
            //Clear the fields - reset
            else if (e.getSource() == clear)    
            {


                jta.setText("");
            }

            else if (e.getSource() == exit)
            {
                try
                {
                    System.exit(0);
                    sock.close();
                }
                catch (Exception ex)
                {
                    System.out.println("Client: exception " + ex);
                }

                }
            }

    }

我不确定到底是怎么回事,但我看到的一件事是你的状况:

if (e.getSource() == calculate) {
    ...
} else if (e.getSource() == clear) { //change it to else if since every click only comes from one source.
    ...
} else if (e.getSource() == exit)

计算
清除
退出
变量吗?如果没有,那么就不应该是这样。

如果没有看到代码的其余部分,很难判断,但是比较语句可能有问题?听起来很奇怪,但我遇到过这样的问题:如果使用==over.equals(),if语句中可能会出现一些奇怪的事情

尝试:


我听说==非常弱,只适合比较int值。在大多数其他情况下,您应该始终使用.equals()

对不起,我不能写评论回复,因为我对这个网站太陌生了。
它看起来像是在actionPerformed for calculate中检查验证,但仍然附加数据。我认为您可能需要围绕数据的附加来包装验证代码。从我看到的情况来看,您可以检查是否输入了用户输入,如果用户没有输入,则相应地提示用户,然后在所有检查之后,它只是附加了任何方式。

我理解得对吗?您有一个清除输入字段的清除按钮吗?但是当在designer视图中单击它时,它会显示上面的代码?当我双击designer视图中的clear按钮对执行的操作进行编码时,它会将我带到源代码的这一部分。我在底部靠近退出代码的地方手动添加了清除代码,它确实清除了字段。但是,当我重新输入姓名、id等并再次单击“计算”时,它崩溃了。这是否意味着您只有三个按钮的一个方法?是的,出于某种原因,当没有在文本字段中输入数据以计算jta(JtextArea)时,也是如此验证在消息对话框中出现,无需担心。但是,当您按下ok时,数据仍然存在,并且计算不会将textfields和jtextarea重置为blankyes,并且出于某种原因,我仍然尝试了此操作。这是输出屏幕第一次正常工作;名称chris ID 76 PayRate是10.0小时是10.0总工资100.0我单击“清除”将字段重置为空白,然后重新输入,这是第二次输出和程序崩溃:名称tom ID 88 PayRate是12.0小时是13.0总工资1.6180540078E-312,然后我再次按“清除”重新输入详细信息单击“计算”并崩溃请将堆栈跟踪添加到你的问题让我知道我们确实出了问题。它只是冻结了所有的堆栈跟踪:java.io.UTFDataFormatException:字节2附近的输入格式错误谢谢你我已经发布了我的完整代码,但是仍然有问题。基本上,我输入详细信息,点击计算,它工作正常,然后我点击清除,清除文本字段和文本区域,重新输入详细信息,点击计算,它给我一个奇怪的总工资数字。最后我再次单击“清除”,它会清除字段,我重新输入详细信息并单击“计算”,它将冻结/崩溃。我重新发布了问题和我的代码,如果有人能使这项工作出色,因为我一直遇到问题谢谢你,我会检查它。其他人建议你不能在套接字中附加多个,因此它会崩溃。如何进行包装验证?耶,我现在可以发表评论了!呵呵,一切都好。我读错了你的一些代码,你的try/catch应该能捕捉到任何缺失的输入。这太疯狂了,它一直在严重地融合我……我甚至尝试从头开始重新创建它。我已经尽了最大的努力,我知道一个接一个的代码,但是一起实现它有点让人困惑。我想也是因为缺乏插座知识。我做得还可以,虽然这是一个班级项目,我们获得的信息和知识有限,并被要求外包。我已经回复了实际问题,也许这会有所帮助,我所知道的一切都糟透了,我用我所知道的知识尝试了又尝试,并且走到了这一步,但是的,肯定有一个更简单的方法…如果有人能重现这个或使它变得简单,我将不胜感激
package com.test;

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

    public class PerminPayClient extends JFrame implements ActionListener 
    {
        /**
         * 
         */
        //Variables

        private static final long serialVersionUID = 1L;
        //Text fields for receiving employee name, employee id, payrate, hours
        private  JTextField jtfEname = new JTextField();
        private JTextField jtfEid = new JTextField();
        private JTextField jtfPayRate = new JTextField();
        //Buttons for onClick Listeners
        private JButton calculate = new JButton("Calculate Pay");
        private JButton clear = new JButton("Clear");
        private JButton exit = new JButton("Exit");

        // IO Streams
        private DataOutputStream outputToServer;
        private DataInputStream inputFromServer;
        private Socket sock;
        private LayoutManager FlowLayout;
        private final JTextArea jta = new JTextArea();
        private final JLabel lblNewLabel = new JLabel("New label");

        public PerminPayClient()
        {
            JPanel p = new JPanel();
            p.setBackground(new Color(255, 255, 102));
            p.setBounds(0, 0, 501, 453);
            p.setLayout(FlowLayout);
            p.setLayout(null);
            p.setLayout(null);
            p.setLayout(null);
            p.setLayout(null);
            p.setLayout(null);
            JLabel lblEmpName = new JLabel("Employee Name:");
            lblEmpName.setFont(new Font("Tahoma", Font.BOLD, 12));
            lblEmpName.setBounds(9, 9, 103, 14);
            p.add(lblEmpName);
            exit.setFont(new Font("Tahoma", Font.BOLD, 12));
            exit.setBounds(376, 126, 108, 23);
            p.add(exit);
            exit.addActionListener(this);
            JLabel lblEmpId = new JLabel("Employee ID:");
            lblEmpId.setFont(new Font("Tahoma", Font.BOLD, 12));
            lblEmpId.setBounds(10, 37, 102, 14);
            p.add(lblEmpId);
            jtfEid.setBounds(134, 34, 140, 20);
            p.add(jtfEid);
            JLabel lblPayRate = new JLabel("Enter Salary:");
            lblPayRate.setFont(new Font("Tahoma", Font.BOLD, 12));
            lblPayRate.setBounds(9, 62, 103, 14);
            p.add(lblPayRate);
            jtfPayRate.setBounds(133, 59, 141, 20);
            p.add(jtfPayRate);
            jtfPayRate.setHorizontalAlignment(SwingConstants.LEFT);
            jtfPayRate.addActionListener(this);
            jtfEname.setBounds(134, 6, 140, 20);
            p.add(jtfEname);
            jtfEname.setHorizontalAlignment(SwingConstants.LEFT);

            // Register Listener/s
            jtfEname.addActionListener(this);
            getContentPane().setLayout(null);
            calculate.setFont(new Font("Tahoma", Font.BOLD, 12));
            calculate.setBounds(73, 126, 180, 23);
            p.add(calculate);
            clear.setFont(new Font("Tahoma", Font.BOLD, 12));
            clear.setBounds(263, 126, 103, 23);
            p.add(clear);
            jtfEid.setHorizontalAlignment(SwingConstants.LEFT);
            getContentPane().add(p);
            jta.setLineWrap(true);
            jta.setRows(20);
            jta.setBounds(10, 160, 480, 282);

            p.add(jta);
            lblNewLabel.setIcon(new ImageIcon("C:\\Users\\Little Beast\\workspace\\PizzaPaySystem\\pizzaplace2.jpg"));
            lblNewLabel.setBounds(298, 9, 192, 106);

            p.add(lblNewLabel);
            jtfEid.addActionListener(this);
            calculate.addActionListener(this);
            clear.addActionListener(this);
            setTitle("Casual Pay Entry");
            setSize(517, 491);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true); // It is necessary to show the frame here!

            try
            {
                //Create a socket to connect to the server
                sock = new Socket("localhost", 8010);

                //Create an input stream to receive data from the server
                inputFromServer = new DataInputStream(sock.getInputStream());

                //Create an output stream to send data to the server
                outputToServer = new DataOutputStream(sock.getOutputStream());
            }
            catch (IOException ex)
            {
                jta.append(ex.toString() + '\n');
            }
        }

        /**
         * @param args
         */
        public static void main(String[] args) 
        {
            new PerminPayClient();
        }

        public void actionPerformed(ActionEvent e) {

            //String actionCommmand = e.getActionCommand();
            if (e.getSource().equals(calculate)) {
                try {
                    //Get the Employee Name, Employee Id, Pay Rate(Salary) from the text fields

                    String empname = jtfEname.getText();
                    //If no Employee Name is entered display pop up dialog box.
                    if(empname.length()==0)
                        JOptionPane.showMessageDialog(null, "Please Enter Your Name");


                    int empid = 0; 
                    try {
                        empid = Integer.parseInt(jtfEid.getText().trim());
                      //If no Employee Id is entered display pop up dialog box
                    } catch(NumberFormatException nfe) {
                        JOptionPane.showMessageDialog(null, "Please Enter Your Id");
                    }   


                    double payrate = 0;
                    try {
                        payrate = Double.parseDouble(jtfPayRate.getText().trim());
                    //If no pay rate is entered display pop up dialog box
                    } catch (NumberFormatException nfe) {
                        JOptionPane.showMessageDialog(null, "Please Enter Your Annual Salary");
                    }

                    //send the Employee Name, Employee Id, Pay Rate to the server
                    String oldName=empname;

                    outputToServer.writeUTF(empname+",p");
                    outputToServer.writeInt(empid);
                    outputToServer.writeDouble(payrate);
                    outputToServer.flush();

                    //Get pay from the server
                    double pay = inputFromServer.readDouble();
                    empname = inputFromServer.readUTF();


                    //Display to the text area
                    jta.append("Name " + oldName + '\n');
                    jta.append("ID " + empid + '\n');
                    jta.append("Salary is " + payrate + '\n');
                    jta.append("Total Pay "
                            + pay + '\n');

                }
                catch (IOException ex) {
                    System.err.println(ex);
                }
            }
            //Clear the fields - reset
            else if (e.getSource() == clear)    
            {
                jtfEname.setText("");
                jtfEid.setText("");
                jtfPayRate.setText("");

                jta.setText("");
            }

            //Button exit function to exit program
            else if (e.getSource() == exit)
            {
                try
                {
                    System.exit(0);
                    sock.close();
                }
                catch (Exception ex)
                {
                    System.out.println("Client: exception " + ex);
                }

                }
            }

    }
if (e.getSource() == calculate) {
    ...
} else if (e.getSource() == clear) { //change it to else if since every click only comes from one source.
    ...
} else if (e.getSource() == exit)
if (e.getSource().equals(calculate)) {