Facebook
From KTB, 2 Months ago, written in Java.
Embed
Download Paste or View Raw
Hits: 183
  1. package vd1;
  2.  
  3. import java.awt.Color;
  4. import java.awt.FlowLayout;
  5. import java.awt.Font;
  6. import javax.swing.BorderFactory;
  7. import javax.swing.BoxLayout;
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.JPanel;
  13. import javax.swing.JTextArea;
  14. import javax.swing.JTextField;
  15. import javax.swing.border.TitledBorder;
  16.  
  17. public class Giai_PTB2 extends JFrame {
  18.     public Giai_PTB2() {
  19.         setSize(440, 420);
  20.         setTitle("Giải phương trình bậc 2");
  21.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  22.         setResizable(false);
  23.        
  24.         setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
  25.        
  26.         JLabel titleLabel = new JLabel("GIẢI PHƯƠNG TRÌNH BẬC 2");
  27.         titleLabel.setAlignmentX(CENTER_ALIGNMENT);
  28.         titleLabel.setFont(new Font("Sans Serif", Font.BOLD, 24));
  29.         titleLabel.setForeground(Color.BLUE);
  30.         add(titleLabel);
  31.        
  32.         JPanel inputPanel = new JPanel();
  33.         inputPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLUE), "NHẬP HỆ A, B, C", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Sans Serif", Font.PLAIN, 16), Color.RED));
  34.         inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
  35.        
  36.         JPanel heSoAPanel = new JPanel(new FlowLayout());
  37.         JLabel heSoALabel = new JLabel("HỆ SỐ A:");
  38.         JTextField heSoATextField = new JTextField(30);
  39.         heSoAPanel.add(heSoALabel);
  40.         heSoAPanel.add(heSoATextField);
  41.         inputPanel.add(heSoAPanel);
  42.        
  43.         JPanel heSoBPanel = new JPanel(new FlowLayout());
  44.         JLabel heSoBLabel = new JLabel("HỆ SỐ B:");
  45.         JTextField heSoBTextField = new JTextField(30);
  46.         heSoBPanel.add(heSoBLabel);
  47.         heSoBPanel.add(heSoBTextField);
  48.         inputPanel.add(heSoBPanel);
  49.        
  50.         JPanel heSoCPanel = new JPanel(new FlowLayout());
  51.         JLabel heSoCLabel = new JLabel("HỆ SỐ C:");
  52.         JTextField heSoCTextField = new JTextField(30);
  53.         heSoCPanel.add(heSoCLabel);
  54.         heSoCPanel.add(heSoCTextField);
  55.         inputPanel.add(heSoCPanel);
  56.        
  57.         JPanel ketQuaPanel = new JPanel(new FlowLayout());
  58.         JLabel ketQuaLabel = new JLabel("KẾT QUẢ:");
  59.         JTextArea ketQuaTextArea = new JTextArea(4, 30);
  60.         ketQuaTextArea.setEditable(false);
  61.         ketQuaPanel.add(ketQuaLabel);
  62.         ketQuaPanel.add(ketQuaTextArea);
  63.         inputPanel.add(ketQuaPanel);
  64.        
  65.         add(inputPanel);
  66.        
  67.         JPanel actionPanel = new JPanel();
  68.         actionPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED), "CHỌN THAO TÁC THỰC HIỆN", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Sans Serif", Font.PLAIN, 16), Color.BLUE));
  69.        
  70.         JButton giaiButton = new JButton("GIẢI");
  71.         giaiButton.addActionListener((e) -> {
  72.             double a = 0, b = 0, c = 0;
  73.            
  74.             try {
  75.                 a = Double.parseDouble(heSoATextField.getText());
  76.                 b = Double.parseDouble(heSoBTextField.getText());
  77.                 c = Double.parseDouble(heSoCTextField.getText());
  78.             } catch (NumberFormatException ex) {
  79.                 JOptionPane.showMessageDialog(null, "Một trong các hệ số không hợp lệ!");
  80.             }
  81.            
  82.             if (a == 0) {
  83.                 if (b == 0) {
  84.                     if (c == 0) {
  85.                         ketQuaTextArea.setText("Phương trình có vô số nghiệm");
  86.                     }
  87.                     else {
  88.                         ketQuaTextArea.setText("Phương trình vô nghiệm");
  89.                     }
  90.                 }
  91.                 else {
  92.                     double x = -c / b;
  93.                     ketQuaTextArea.setText("Phương trình có một nghiệm: x = " + x);
  94.                 }
  95.             }
  96.             else {
  97.                 double delta = b * b - 4 * a * c;
  98.  
  99.                 if (delta > 0) {
  100.                     double x1 = (-b + Math.sqrt(delta)) / (2 * a);
  101.                     double x2 = (-b - Math.sqrt(delta)) / (2 * a);
  102.                     ketQuaTextArea.setText("Phương trình có hai nghiệm phân biệt: " + "x1 = " + x1 + ", x2 = " + x2);
  103.                 }
  104.                 else if (delta == 0) {
  105.                     double x = -b / (2 * a);
  106.                     ketQuaTextArea.setText("Phương trình có nghiệm kép: x = " + x);
  107.                 }
  108.                 else {
  109.                     ketQuaTextArea.setText("Phương trình vô nghiệm.");
  110.                 }
  111.             }
  112.         });
  113.        
  114.         JButton xoaTrangButton = new JButton("XOÁ TRẮNG");
  115.         xoaTrangButton.addActionListener((e) -> {
  116.             heSoATextField.setText("");
  117.             heSoBTextField.setText("");
  118.             heSoCTextField.setText("");
  119.             ketQuaTextArea.setText("");
  120.         });
  121.        
  122.         JButton thoatButton = new JButton("THOÁT");
  123.         thoatButton.addActionListener((e) -> {
  124.             System.exit(0);
  125.         });
  126.        
  127.         actionPanel.add(giaiButton);
  128.         actionPanel.add(xoaTrangButton);
  129.         actionPanel.add(thoatButton);
  130.        
  131.         add(actionPanel);
  132.        
  133.         setVisible(true);
  134.     }
  135.    
  136.     public static void main(String[] args) {
  137.         new Giai_PTB2();
  138.     }
  139. }
  140.  
  141.