Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package ptbac2;
- import javax.swing.JOptionPane;
- /**
- *
- * @author leo
- */
- public class Bac2 extends javax.swing.JFrame {
- /**
- * Creates new form Bac2
- */
- public Bac2() {
- initComponents();
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jLabel3 = new javax.swing.JLabel();
- jLabel4 = new javax.swing.JLabel();
- tfA = new javax.swing.JTextField();
- tfB = new javax.swing.JTextField();
- tfC = new javax.swing.JTextField();
- lbResult = new javax.swing.JLabel();
- btCalc = new javax.swing.JButton();
- btClear = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
- jLabel1.setText("ax^2 + bx + c = 0");
- jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
- jLabel2.setText("a=");
- jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
- jLabel3.setText("b=");
- jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
- jLabel4.setText("c=");
- lbResult.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
- lbResult.setText("Result");
- btCalc.setText("Calc");
- btCalc.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btCalcActionPerformed(evt);
- }
- });
- btClear.setText("Clear");
- btClear.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btClearActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(47, 47, 47)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel2)
- .addComponent(jLabel3)
- .addComponent(jLabel4))
- .addGap(34, 34, 34)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(lbResult)
- .addComponent(jLabel1)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
- .addGroup(layout.createSequentialGroup()
- .addComponent(btCalc)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
- .addComponent(btClear))
- .addComponent(tfA, javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(tfB, javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(tfC, javax.swing.GroupLayout.Alignment.LEADING)))
- .addContainerGap(65, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(27, 27, 27)
- .addComponent(jLabel1)
- .addGap(26, 26, 26)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel2)
- .addComponent(tfA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(22, 22, 22)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel3)
- .addComponent(tfB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(21, 21, 21)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel4)
- .addComponent(tfC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(34, 34, 34)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(btCalc)
- .addComponent(btClear))
- .addGap(36, 36, 36)
- .addComponent(lbResult)
- .addContainerGap(43, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void btCalcActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- try {
- float a = Float.valueOf(tfA.getText());
- float b = Float.valueOf(tfB.getText());
- float c = Float.valueOf(tfC.getText());
- float delta = b * b - 4 * a * c;
- if (delta > 0) {
- float x1 = (float) ((-b + Math.sqrt(delta)) / (2 * a));
- float x2 = (float) ((-b - Math.sqrt(delta)) / (2 * a));
- String strResult = "x1 = " + String.valueOf(x1) + " ; x2 = " + String.valueOf(x2);
- lbResult.setText(strResult);
- }
- if (delta == 0) {
- float x = -b / (2 * a);
- lbResult.setText(String.valueOf(x));
- }
- if (delta < 0) {
- lbResult.setText("Vo nghiem");
- }
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, "Error!");
- lbResult.setText("Error!");
- }
- }
- private void btClearActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- tfA.setText("");
- tfB.setText("");
- tfC.setText("");
- lbResult.setText("");
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(Bac2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(Bac2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(Bac2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(Bac2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new Bac2().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton btCalc;
- private javax.swing.JButton btClear;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JLabel jLabel4;
- private javax.swing.JLabel lbResult;
- private javax.swing.JTextField tfA;
- private javax.swing.JTextField tfB;
- private javax.swing.JTextField tfC;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment