JFrame Автоматический расчет - PullRequest
0 голосов
/ 20 января 2019

У меня проблема с автоматическим вычислением текстового поля в JAVA с использованием Netbeans

Мой вопрос: я буду вводить числовые значения в текстовое поле для автоматического добавления, а затем вводить числовые значения в текстовое поле в (тариф и налоги comm%), где я буду получать к полям автоматический расчет, так как я получу результат этих числовых значений в текстовом поле (Comm), а также (себестоимость) до нажатия кнопки Отправить.

enter image description here

     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);
    }

Как это можно сделать.

Спасибо ...........

1 Ответ

0 голосов
/ 26 января 2019

Мне пришлось использовать 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));
        }
    };
...