Guest User

Calc

a guest
Apr 2nd, 2015
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.76 KB | None | 0 0
  1. /**
  2.  * /
  3.  
  4. /**
  5.  * @author mirchevsvilen
  6.  *
  7.  */
  8. import java.text.DecimalFormat;import java.io.*;import javax.swing.JOptionPane;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.awt.font.*;
  9. public class Calc extends Frame {
  10.   Double x1 = 0d;Double x2 = 0d;Double d = 0d;
  11.   private JButton btnRechne = new JButton("Calculate");
  12.   private JTextField tbxa = new JTextField("a");
  13.   private JTextField tbxb = new JTextField("b");
  14.   private JTextField tbxc = new JTextField("c");
  15.   private JTextField tbxdiscriminant  = new JTextField("D");
  16.   private JTextField tbxx1  = new JTextField("x1");
  17.   private JTextField tbxx2  = new JTextField("x2");
  18.   private JLabel lbla = new JLabel("a:");
  19.   private JLabel lblb = new JLabel("b:");
  20.   private JLabel lblc = new JLabel("c:");
  21.   private JLabel lblkvadratnouravnenie = new JLabel("<html><h1><b>(ax<sup>2</sup>+bx-c = 0)</b></h1></html>");
  22.   private JLabel lblx1 = new JLabel("x1:");
  23.   private JLabel lblx2 = new JLabel("x2:");
  24.   private JLabel lbldiscriminant = new JLabel("D:");
  25.   private JButton btnReset = new JButton("Reset");
  26.   private JFrame frm = new JFrame("");
  27.   private JMenuBar menubar = new JMenuBar();
  28.   private JMenu menu1  = new JMenu("File");
  29.   private Panel cp = new Panel(null);
  30.   private JMenuItem exit = new JMenuItem("Exit") ;
  31.   private Font font = new Font("Verdana", Font.BOLD, 12);
  32.   private ButtonListener  meinBl = new  ButtonListener ();
  33.   //Constructor
  34.   public Calc (){
  35.     super("Calculator");
  36.     menubar.add(menu1);
  37.     menu1.add(exit);
  38.     exit.addActionListener(meinBl);
  39.     frm.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  40.     setLocationRelativeTo(null);
  41.     frm.add(cp);
  42.     frm.setJMenuBar(menubar);
  43.     frm.setSize(500,500);
  44.     frm.setResizable(false);
  45.     frm.setVisible(true);
  46.     cp.setBackground(Color.WHITE);
  47.     //******Buttons***************
  48.     //***************
  49.     btnReset.setBounds(250,300,100,50);
  50.     btnReset.setFont(font);
  51.     btnReset.setBackground(Color.RED);
  52.     btnReset.setForeground(Color.WHITE);
  53.     btnReset.addActionListener(new ActionListener (){
  54.       public void actionPerformed(ActionEvent evt){
  55.         btnReset_ActionPerformed(evt);
  56.       }});
  57.     cp.add(btnReset);
  58.     btnRechne.setBounds(50, 300, 100, 50);
  59.     btnRechne.setFont(font);
  60.     btnRechne.setBackground(Color.YELLOW);
  61.     btnRechne.addActionListener(new ActionListener (){
  62.       public void actionPerformed(ActionEvent evt){
  63.         btnRechne_ActionPerformed(evt);
  64.       } } );
  65.     cp.add(btnRechne);
  66.     //******TextBox***************
  67.     //***************
  68.     tbxa.setBounds(20,80,110,30);
  69.     tbxa.setBackground(Color.WHITE);
  70.     cp.add(tbxa);
  71.     tbxb.setBounds(180,80,110,30);
  72.     tbxb.setBackground(Color.WHITE);
  73.     cp.add(tbxb);
  74.     tbxc.setBounds(320,80,110,30);
  75.     tbxc.setBackground(Color.WHITE);
  76.     cp.add(tbxc);
  77.     tbxdiscriminant.setBounds(50,180,100,30);
  78.     tbxdiscriminant.setBackground(Color.WHITE);
  79.     cp.add(tbxdiscriminant);
  80.     tbxx1.setBounds(190,150,200,30);
  81.     cp.add(tbxx1);
  82.     tbxx2.setBounds(190,200,200,30);
  83.     cp.add(tbxx2);
  84.     //****************Label***************
  85.     //***************
  86.     lblkvadratnouravnenie.setBounds(150,2,300,50);
  87.     cp.add(lblkvadratnouravnenie);
  88.     lbla.setBounds(5,80,20,20);
  89.     cp.add(lbla);
  90.     lblb.setBounds(160,80,20,20);
  91.     cp.add(lblb);
  92.     lblc.setBounds(300,80,20,20);
  93.     cp.add(lblc);
  94.     lblx1.setBounds(160,150,100,30);
  95.     cp.add(lblx1);
  96.     lblx2.setBounds(160,200,100,30);
  97.     cp.add(lblx2);
  98.     lbldiscriminant.setBounds(30,180,100,30);
  99.     cp.add(lbldiscriminant);
  100.     //**********************************************
  101.   }//End of Constructor
  102.   public void  btnRechne_ActionPerformed(ActionEvent evt){
  103.     DecimalFormat df = new DecimalFormat("#.##");
  104.     String textc = this.tbxc.getText();
  105.     Double  zahlc = Double.valueOf((textc));
  106.     String textb = this.tbxb.getText();
  107.     Double  zahlb = Double.valueOf((textb));;
  108.     String texta = this.tbxa.getText();
  109.     Double zahla = Double.valueOf((texta));
  110.     if (zahla != 0) {
  111.       try {
  112.         d = (zahlb*zahlb)-(4*zahla*zahlc);
  113.         textc = d.toString();
  114.         this.tbxdiscriminant.setText(textc);
  115.         if (d > 0) {
  116.           x1 = (-zahlb + Math.sqrt(Math.pow(zahlb, 2) - 4*zahla*zahlc)) / (2*zahla);
  117.           textc = x1.toString();
  118.           this.tbxx1.setText(textc);
  119.           x2 = (-zahlb - Math.sqrt(Math.pow(zahlb, 2) - 4*zahla*zahlc)) / (2*zahla);
  120.           textc = x2.toString();
  121.           this.tbxx2.setText(textc);
  122.         } // end of if
  123.         if (d < 0) {
  124.           this.tbxx1.setText("Error");
  125.           this.tbxx2.setText("Error");
  126.         } // end of if
  127.         if (d == 0) {
  128.           x1 = -zahlb/(2*zahla) + Math.round(x1);
  129.           textc = x1.toString();
  130.           this.tbxx1.setText(textc);
  131.           this.tbxx2.setText("");
  132.         } // end of if
  133.       }
  134.       catch (NumberFormatException e) {
  135.       }
  136.       // end of try
  137.     } else if (zahla == 0) {
  138.       try {
  139.         this.tbxx1.setText("Error");
  140.         this.tbxx2.setText("Error");
  141.       }
  142.       catch (NumberFormatException e) {
  143.         System.out.println("Error");
  144.       }
  145.       // end of try
  146.     } // end of if-else
  147.   }
  148.   public void    btnReset_ActionPerformed(ActionEvent evt){
  149.     this.tbxa.setText("");
  150.     this.tbxb.setText("");  
  151.     this.tbxc.setText("");
  152.     this.tbxx1.setText("");
  153.     this.tbxx2.setText("");
  154.     this.tbxdiscriminant.setText("");
  155.   }
  156.   public class ButtonListener implements ActionListener {
  157.     public void actionPerformed(ActionEvent ae) {
  158.       if (ae.getSource() == exit) {
  159.         System.exit(0);
  160.       } // end of if
  161.     }
  162.     }
  163.   public static void main(String[] args) {
  164.     new Calc();
  165.     }//End of main
  166. } //End of Class
Advertisement
Add Comment
Please, Sign In to add comment