Phương trình bậc 2

JavaSwing giải phương trình bậc 2
 avatar
user_8008155
java
2 years ago
5.1 kB
0
Indexable
Never
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TheEquationUI extends JFrame {
    JTextField txtNumA,txtNumB;
    JTextField calculation;
    JButton BtnSolve, BtnCancel, BtnHelp;

    public TheEquationUI(String title){
        super(title);
        addControl();
        addEvent();
    }

    public void addControl(){
        Container con = getContentPane();
        JPanel pnMain = new JPanel();
        pnMain.setLayout(new BoxLayout(pnMain,BoxLayout.Y_AXIS));
        con.add(pnMain);

        JPanel pnTitle = new JPanel();
        pnTitle.setLayout(new FlowLayout());
        JLabel lb = new JLabel("Giải phương trình bậc 1");
        pnTitle.add(lb);
        pnMain.add(pnTitle);
        lb.setForeground(Color.PINK);
        Font fontTitle = new Font("arial",Font.BOLD,20);
        lb.setFont(fontTitle);

        JPanel pnNumA = new JPanel();
        pnNumA.setLayout(new FlowLayout());
        JLabel lbNumA = new JLabel("Number A");
        txtNumA = new JTextField(20);
        pnNumA.add(lbNumA);
        pnNumA.add(txtNumA);
        pnMain.add(pnNumA);

        JPanel pnNumB = new JPanel();
        pnNumB.setLayout(new FlowLayout());
        JLabel lbNumB = new JLabel("Number B");
        txtNumB = new JTextField(20);
        pnNumB.add(lbNumB);
        pnNumB.add(txtNumB);
        pnMain.add(pnNumB);

        JPanel pnBtn = new JPanel();
        pnBtn.setLayout(new FlowLayout());
        BtnSolve = new JButton("Solve");
        BtnCancel = new JButton(" Cancel");
        BtnCancel.setIcon(new ImageIcon("img/exit.png"));
        BtnHelp = new JButton("Help");
        BtnHelp.setIcon(new ImageIcon("img/exclamation.png"));
        pnBtn.add(BtnSolve);
        pnBtn.add(BtnCancel);
        pnBtn.add(BtnHelp);
        pnMain.add(pnBtn);

        BtnSolve.setPreferredSize(BtnCancel.getPreferredSize());
        BtnHelp.setPreferredSize(BtnCancel.getPreferredSize());

        JPanel pnCalculation = new JPanel();
        pnCalculation.setLayout(new FlowLayout());
        JLabel lbCalculation =new JLabel("Calculation");
        calculation = new JTextField(20);
        pnCalculation.add(lbCalculation);
        pnCalculation.add(calculation);
        pnMain.add(pnCalculation);


    }
    public void Equation(){
        String numberA = txtNumA.getText();
        String numberB = txtNumB.getText();
        double a = Double.parseDouble(numberA);
        double b = Double.parseDouble(numberB);


        if (a == 0 && b == 0){
            calculation.setText("Equation with infinite solutions");
        } else if (a == 0 && b!=0) {
            calculation.setText("The equation has no solution");
        }else {
            double x = -b / a;
            calculation.setText("X =" + x);
        }
    }

    public boolean isNumeric(){
        String numberA = txtNumA.getText();
        String numberB = txtNumB.getText();
        try {
            Double.parseDouble(numberA);
            Double.parseDouble(numberB);
            return true;
        } catch(NumberFormatException e){
            return false;
        }
    }

    public void addEvent(){
        BtnCancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        BtnSolve.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String numberA = txtNumA.getText();
                String numberB = txtNumB.getText();
                if ((numberA.trim().length() == 0)&& (numberB.trim().length() == 0)){
                    JOptionPane.showMessageDialog(null, "Opp, please check NumberA And NumberB.",
                            "Inane error", JOptionPane.ERROR_MESSAGE);
                    return;
                }else if ((numberA.trim().length() == 0) ) {
                    JOptionPane.showMessageDialog(null, "Opp, please check Number A.",
                            "Inane error", JOptionPane.ERROR_MESSAGE);
                    return;
                } else if (numberB.trim().length() == 0) {
                    JOptionPane.showMessageDialog(null, "Opp, please check NumberB.");
                    return;
                }
                if (isNumeric()==false){
                  JOptionPane.showMessageDialog(null,"Oop, Enter only Number",
                          "Inane error",JOptionPane.ERROR_MESSAGE);
                }

                Equation();
            }
        });
        BtnHelp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,
                        "Enter number A anh number B anh press Calculation");
            }
        });
    }

    public void showWindow(){
        this.setSize(400,350);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
}