JFrame 自动计算

JFrame Auto Calculation

我在 JAVA 使用 Netbeans

时遇到文本字段自动计算问题

我的问题是,如果我将在文本字段中输入数值以进行自动添加,然后在文本字段中输入数值到(票价和税收和通信%),我将在其中进行字段自动计算,那么我将如何在单击提交按钮之前,在文本字段 (Comm) 和 (Cost Price) 中获取这些数值的结果。

     try {   String sql = "insert into ticketing (Date,LPO,PassName,Route,AirlineCode,TicketNum,SellingPrice, Contact, Officer,Fare,Tax,comm%,comm,CostPrice,System,Remart)" + "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
        pst = conn.prepareStatement(sql);
        //pst.setInt(1, Integer.parseInt(id.getText()));


        pst.setString(1, Date.getText());

        pst.setString(2, LPO.getText());

        pst.setString(3, PassName.getText());

        pst.setString(4, Route.getText());

        pst.setString(5, AirCode.getText());

        pst.setString(6, TikNum.getText());

        pst.setString(7, SellPrice.getText());



        String Conta;
        Conta = Cont.getSelectedItem().toString();
        pst.setString (8,Conta);

        String Officer;
        Officer = Offic.getSelectedItem().toString();
        pst.setString (9,Officer);

        pst.setString(10, Fare.getText());


        pst.setString(11, Tax.getText());


        pst.setString(12, commper.getText());


        pst.setString(13, comm.getText());


        pst.setString(14, CostPrice.getText());


       String Sys;
        Sys = System.getSelectedItem().toString();
        pst.setString (15,Sys);


        pst.setString(16, Remark.getText());




        pst.executeUpdate();
        JOptionPane.showMessageDialog(null, "insertion successful");
        conn.close();

    }catch (SQLException e){
        JOptionPane.showMessageDialog(null, e);
    }

怎么办。

谢谢......

我不得不使用 DocumentFilter 来解决我只是分享我的代码,它可能会在将来帮助某些人,也可能会帮助一些人搜索知识

DocumentFilter df = new DocumentFilter() {
        @Override
        public void insertString(DocumentFilter.FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {

            if (isDigit(string)) {
                super.insertString(fb, i, string, as);
                calcAndSetTotal();
            }
        }
         @Override
        public void remove(DocumentFilter.FilterBypass fb, int i, int i1) throws BadLocationException {
            super.remove(fb, i, i1);
            calcAndSetTotal();
        }

        @Override
        public void replace(DocumentFilter.FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
            if (isDigit(string)) {
                super.replace(fb, i, i1, string, as);
                calcAndSetTotal();

            }
        }

        private boolean isDigit(String string) {
            for (int n = 0; n < string.length(); n++) {
                char c = string.charAt(n);//get a single character of the string
                //System.out.println(c);
                if (!Character.isDigit(c)) {//if its an alphabetic character or white space
                    return false;
                }
            }
            return true;
        }

        void calcAndSetTotal() {
            int sum = 0;
            int fr = 0;
            int pc = 0;
            int tax = 0;
            int total = 0;

            if (!Fare.getText().isEmpty()) {
                fr= Integer.parseInt(Fare.getText());//we must add this
            }
            if (!Tax.getText().isEmpty()) {
                tax= Integer.parseInt(Tax.getText());//we must add this
            }
            if (!commper.getText().isEmpty()) {
                pc= Integer.parseInt(commper.getText());//we must subtract this
            }
            sum =(int) (fr *(pc*0.01));

            total = (int) (fr + tax - sum);
            comm.setText(String.valueOf(sum));
            CostPrice.setText(String.valueOf(total));
        }
    };