Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * /
- /**
- * @author mirchevsvilen
- *
- */
- 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.*;
- public class Calc extends Frame {
- Double x1 = 0d;Double x2 = 0d;Double d = 0d;
- private JButton btnRechne = new JButton("Calculate");
- private JTextField tbxa = new JTextField("a");
- private JTextField tbxb = new JTextField("b");
- private JTextField tbxc = new JTextField("c");
- private JTextField tbxdiscriminant = new JTextField("D");
- private JTextField tbxx1 = new JTextField("x1");
- private JTextField tbxx2 = new JTextField("x2");
- private JLabel lbla = new JLabel("a:");
- private JLabel lblb = new JLabel("b:");
- private JLabel lblc = new JLabel("c:");
- private JLabel lblkvadratnouravnenie = new JLabel("<html><h1><b>(ax<sup>2</sup>+bx-c = 0)</b></h1></html>");
- private JLabel lblx1 = new JLabel("x1:");
- private JLabel lblx2 = new JLabel("x2:");
- private JLabel lbldiscriminant = new JLabel("D:");
- private JButton btnReset = new JButton("Reset");
- private JFrame frm = new JFrame("");
- private JMenuBar menubar = new JMenuBar();
- private JMenu menu1 = new JMenu("File");
- private Panel cp = new Panel(null);
- private JMenuItem exit = new JMenuItem("Exit") ;
- private Font font = new Font("Verdana", Font.BOLD, 12);
- private ButtonListener meinBl = new ButtonListener ();
- //Constructor
- public Calc (){
- super("Calculator");
- menubar.add(menu1);
- menu1.add(exit);
- exit.addActionListener(meinBl);
- frm.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
- setLocationRelativeTo(null);
- frm.add(cp);
- frm.setJMenuBar(menubar);
- frm.setSize(500,500);
- frm.setResizable(false);
- frm.setVisible(true);
- cp.setBackground(Color.WHITE);
- //******Buttons***************
- //***************
- btnReset.setBounds(250,300,100,50);
- btnReset.setFont(font);
- btnReset.setBackground(Color.RED);
- btnReset.setForeground(Color.WHITE);
- btnReset.addActionListener(new ActionListener (){
- public void actionPerformed(ActionEvent evt){
- btnReset_ActionPerformed(evt);
- }});
- cp.add(btnReset);
- btnRechne.setBounds(50, 300, 100, 50);
- btnRechne.setFont(font);
- btnRechne.setBackground(Color.YELLOW);
- btnRechne.addActionListener(new ActionListener (){
- public void actionPerformed(ActionEvent evt){
- btnRechne_ActionPerformed(evt);
- } } );
- cp.add(btnRechne);
- //******TextBox***************
- //***************
- tbxa.setBounds(20,80,110,30);
- tbxa.setBackground(Color.WHITE);
- cp.add(tbxa);
- tbxb.setBounds(180,80,110,30);
- tbxb.setBackground(Color.WHITE);
- cp.add(tbxb);
- tbxc.setBounds(320,80,110,30);
- tbxc.setBackground(Color.WHITE);
- cp.add(tbxc);
- tbxdiscriminant.setBounds(50,180,100,30);
- tbxdiscriminant.setBackground(Color.WHITE);
- cp.add(tbxdiscriminant);
- tbxx1.setBounds(190,150,200,30);
- cp.add(tbxx1);
- tbxx2.setBounds(190,200,200,30);
- cp.add(tbxx2);
- //****************Label***************
- //***************
- lblkvadratnouravnenie.setBounds(150,2,300,50);
- cp.add(lblkvadratnouravnenie);
- lbla.setBounds(5,80,20,20);
- cp.add(lbla);
- lblb.setBounds(160,80,20,20);
- cp.add(lblb);
- lblc.setBounds(300,80,20,20);
- cp.add(lblc);
- lblx1.setBounds(160,150,100,30);
- cp.add(lblx1);
- lblx2.setBounds(160,200,100,30);
- cp.add(lblx2);
- lbldiscriminant.setBounds(30,180,100,30);
- cp.add(lbldiscriminant);
- //**********************************************
- }//End of Constructor
- public void btnRechne_ActionPerformed(ActionEvent evt){
- DecimalFormat df = new DecimalFormat("#.##");
- String textc = this.tbxc.getText();
- Double zahlc = Double.valueOf((textc));
- String textb = this.tbxb.getText();
- Double zahlb = Double.valueOf((textb));;
- String texta = this.tbxa.getText();
- Double zahla = Double.valueOf((texta));
- if (zahla != 0) {
- try {
- d = (zahlb*zahlb)-(4*zahla*zahlc);
- textc = d.toString();
- this.tbxdiscriminant.setText(textc);
- if (d > 0) {
- x1 = (-zahlb + Math.sqrt(Math.pow(zahlb, 2) - 4*zahla*zahlc)) / (2*zahla);
- textc = x1.toString();
- this.tbxx1.setText(textc);
- x2 = (-zahlb - Math.sqrt(Math.pow(zahlb, 2) - 4*zahla*zahlc)) / (2*zahla);
- textc = x2.toString();
- this.tbxx2.setText(textc);
- } // end of if
- if (d < 0) {
- this.tbxx1.setText("Error");
- this.tbxx2.setText("Error");
- } // end of if
- if (d == 0) {
- x1 = -zahlb/(2*zahla) + Math.round(x1);
- textc = x1.toString();
- this.tbxx1.setText(textc);
- this.tbxx2.setText("");
- } // end of if
- }
- catch (NumberFormatException e) {
- }
- // end of try
- } else if (zahla == 0) {
- try {
- this.tbxx1.setText("Error");
- this.tbxx2.setText("Error");
- }
- catch (NumberFormatException e) {
- System.out.println("Error");
- }
- // end of try
- } // end of if-else
- }
- public void btnReset_ActionPerformed(ActionEvent evt){
- this.tbxa.setText("");
- this.tbxb.setText("");
- this.tbxc.setText("");
- this.tbxx1.setText("");
- this.tbxx2.setText("");
- this.tbxdiscriminant.setText("");
- }
- public class ButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent ae) {
- if (ae.getSource() == exit) {
- System.exit(0);
- } // end of if
- }
- }
- public static void main(String[] args) {
- new Calc();
- }//End of main
- } //End of Class
Advertisement
Add Comment
Please, Sign In to add comment