/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package GiaiPTBac1; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * * @author Admin */ public class View extends JFrame { private JTextField tfHeSoA, tfHeSoB, tfKetQua; private JButton btnGiai, btnXoa, btnThoat; public View() { this.initComponents(); this.ActionListener(); } private void initComponents() { this.setTitle("Giải Phương Trình Bậc 1"); this.setSize(500, 400); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); Container con = this.getContentPane(); con.setLayout(new BoxLayout(con, BoxLayout.Y_AXIS)); JPanel[] listPanel = new JPanel[5]; FlowLayout flowL = new FlowLayout(FlowLayout.CENTER); for (int i = 0; i < listPanel.length; i++) { listPanel[i] = new JPanel(); } // Panel[0] - Tiêu Đề listPanel[0].setLayout(flowL); JLabel lbTieuDe = new JLabel("Giải Phương Trình Bậc 1"); listPanel[0].add(lbTieuDe); // Panel[1] - Hệ Số A listPanel[1].setLayout(flowL); JLabel lbHeSoA = new JLabel("Hệ Số A: "); tfHeSoA = new JTextField(20); listPanel[1].add(lbHeSoA); listPanel[1].add(tfHeSoA); // Panel[2] - Hệ Số B listPanel[2].setLayout(flowL); JLabel lbHeSoB = new JLabel("Hệ Số B: "); tfHeSoB = new JTextField(20); listPanel[2].add(lbHeSoB); listPanel[2].add(tfHeSoB); // Panel[3] - Nút listPanel[3].setLayout(flowL); btnXoa = new JButton("Xóa"); btnGiai = new JButton("Giải"); btnThoat = new JButton("Thoát"); listPanel[3].add(btnGiai); listPanel[3].add(btnXoa); listPanel[3].add(btnThoat); // Panel[4] - Kết Quả listPanel[4].setLayout(flowL); JLabel lbKetQua = new JLabel("Kết Quả: "); tfKetQua = new JTextField(20); tfKetQua.setEnabled(false); listPanel[4].add(lbKetQua); listPanel[4].add(tfKetQua); // Add Panel to Container for (JPanel panel : listPanel) { con.add(panel); } } private void ActionListener() { btnGiai.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (!View.isNumeric(tfHeSoA.getText()) || !View.isNumeric(tfHeSoB.getText())) { tfKetQua.setText("Vui lòng nhập lại A hoặc B"); } else { Double a = Double.parseDouble(tfHeSoA.getText()); Double b = Double.parseDouble(tfHeSoB.getText()); if (a == 0 && b == 0) { tfKetQua.setText("Phương Trình Vô Số Nghiệm"); } else if (a == 0 && b != 0) { tfKetQua.setText("Phương Trình Vô Nghiệm"); } else { tfKetQua.setText("Phương Trình Có Nghiệm x=" + (-b / a)); } } } }); btnXoa.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { tfHeSoA.setText(""); tfHeSoB.setText(""); tfKetQua.setText(""); tfHeSoA.requestFocus(); } }); btnThoat.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int n = JOptionPane.showConfirmDialog(null, "Bạn muốn thoát không?", "Thông Báo", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); if(n == JOptionPane.YES_OPTION) System.exit(0); else JOptionPane.showMessageDialog(null, "Vậy ớ hở"); } }); } public static boolean isNumeric(String strNum) { if (strNum == null) { return false; } try { double d = Double.parseDouble(strNum); } catch (NumberFormatException nfe) { return false; } return true; } public static void main(String[] args) { new View().setVisible(true); } }