Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package calculator;
- public class Calculator
- {
- public static void main(String[] args) throws Exception
- {
- MyCalculator ob = new MyCalculator();
- ob.setVisible(true);
- }
- }
- public class MyCalculator extends javax.swing.JFrame
- {
- double num1,num2,result;
- String n1,n2,nr;
- /**
- * Creates new form MyCalculator
- */
- public MyCalculator() throws Exception
- {
- 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() {
- num1TextField = new javax.swing.JTextField();
- num2TextField = new javax.swing.JTextField();
- resultTextField = new javax.swing.JTextField();
- AddButton = new javax.swing.JButton();
- SubstractButton = new javax.swing.JButton();
- MultiplyButton = new javax.swing.JButton();
- DivideButton = new javax.swing.JButton();
- ModulusButton = new javax.swing.JButton();
- PercentageButton = new javax.swing.JButton();
- LogarithmButtom = new javax.swing.JButton();
- KilometerToMileConvertButton = new javax.swing.JButton();
- CelsiusToFahrenheitConvertButton = new javax.swing.JButton();
- ClearButton = new javax.swing.JButton();
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- num1TextField.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- num1TextFieldActionPerformed(evt);
- }
- });
- num2TextField.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- num2TextFieldActionPerformed(evt);
- }
- });
- resultTextField.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- resultTextFieldActionPerformed(evt);
- }
- });
- AddButton.setText("ADD");
- AddButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- AddButtonActionPerformed(evt);
- }
- });
- SubstractButton.setText("SUBSTRACT");
- SubstractButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- SubstractButtonActionPerformed(evt);
- }
- });
- MultiplyButton.setText("MULTIPLY");
- MultiplyButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- MultiplyButtonActionPerformed(evt);
- }
- });
- DivideButton.setText("DIVIDE");
- DivideButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- DivideButtonActionPerformed(evt);
- }
- });
- ModulusButton.setText("MODULUS");
- ModulusButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- ModulusButtonActionPerformed(evt);
- }
- });
- PercentageButton.setText("PERCENTAGE");
- PercentageButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- PercentageButtonActionPerformed(evt);
- }
- });
- LogarithmButtom.setText("LOGARITHM");
- LogarithmButtom.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- LogarithmButtomActionPerformed(evt);
- }
- });
- KilometerToMileConvertButton.setText("KM --> MILE");
- KilometerToMileConvertButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- KilometerToMileConvertButtonActionPerformed(evt);
- }
- });
- CelsiusToFahrenheitConvertButton.setText("*C --> *F");
- CelsiusToFahrenheitConvertButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- CelsiusToFahrenheitConvertButtonActionPerformed(evt);
- }
- });
- ClearButton.setText("CLEAR");
- ClearButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- ClearButtonActionPerformed(evt);
- }
- });
- jLabel1.setText("Operation :");
- jLabel2.setText("Convert:");
- 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(22, 22, 22)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(ClearButton, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap())
- .addGroup(layout.createSequentialGroup()
- .addGap(0, 0, Short.MAX_VALUE)
- .addComponent(num1TextField, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(num2TextField, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(resultTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(60, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(ModulusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(KilometerToMileConvertButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(CelsiusToFahrenheitConvertButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(0, 0, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(SubstractButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(MultiplyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(DivideButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addComponent(PercentageButton)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(LogarithmButtom, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(36, 36, 36)
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGap(39, 39, 39)
- .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addGap(0, 0, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(20, 20, 20)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(num1TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE)
- .addComponent(num2TextField)
- .addComponent(resultTextField))
- .addGap(14, 14, 14)
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(AddButton)
- .addComponent(SubstractButton)
- .addComponent(MultiplyButton)
- .addComponent(DivideButton))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(ModulusButton)
- .addComponent(PercentageButton)
- .addComponent(LogarithmButtom))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jLabel2)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(KilometerToMileConvertButton)
- .addComponent(CelsiusToFahrenheitConvertButton))
- .addGap(18, 18, 18)
- .addComponent(ClearButton)
- .addContainerGap(27, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void resultTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void num2TextFieldActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void DivideButtonActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- num1=Integer.parseInt(num1TextField.getText());
- num2=Integer.parseInt(num2TextField.getText());
- try
- {
- result=num1/num2;
- }
- catch(Exception e)
- {
- System.out.println(""+e);
- e.printStackTrace();
- }
- resultTextField.setText(""+result);
- }
- private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- num1=Integer.parseInt(num1TextField.getText());
- num2=Integer.parseInt(num2TextField.getText());
- result=num1+num2;
- resultTextField.setText(""+result);
- }
- private void ModulusButtonActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- num1=Integer.parseInt(num1TextField.getText());
- num2=Integer.parseInt(num2TextField.getText());
- result=num1%num2;
- resultTextField.setText(""+result);
- }
- private void MultiplyButtonActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- num1=Integer.parseInt(num1TextField.getText());
- num2=Integer.parseInt(num2TextField.getText());
- result=num1*num2;
- resultTextField.setText(""+result);
- }
- private void SubstractButtonActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- num1=Integer.parseInt(num1TextField.getText());
- num2=Integer.parseInt(num2TextField.getText());
- result=num1-num2;
- resultTextField.setText(""+result);
- }
- private void KilometerToMileConvertButtonActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- n1 = num1TextField.getText();
- boolean f = true ;
- for(int i = 0 ; i<n1.length();i++){
- if(n1.charAt(i)<'0' || n1.charAt(i)>'9'){
- f = false ;
- break;
- }
- }
- if(f){
- num1=Integer.parseInt(n1); ;
- result=num1*0.621371;
- }
- if(f)
- resultTextField.setText(""+result);
- else
- resultTextField.setText("invalid io.");
- }
- private void PercentageButtonActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- num1=Integer.parseInt(num1TextField.getText());
- num2=Integer.parseInt(num2TextField.getText());
- result=(num1*num2)/100;
- resultTextField.setText(""+result);
- }
- private void LogarithmButtomActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- n1 = num1TextField.getText();
- boolean f = true ;
- for(int i = 0 ; i<n1.length();i++){
- if(n1.charAt(i)<'0' || n1.charAt(i)>'9'){
- f = false ;
- break;
- }
- }
- if(f){
- num1=Integer.parseInt(n1);
- result=Math.log(num1);
- }
- if(f)
- resultTextField.setText(""+result);
- else
- resultTextField.setText("invalid io.");
- }
- private void CelsiusToFahrenheitConvertButtonActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- n1 = num1TextField.getText(); ;
- boolean f = true ;
- for(int i = 0 ; i<n1.length();i++){
- if(n1.charAt(i)<'0' || n1.charAt(i)>'9'){
- f = false ;
- break;
- }
- }
- if(f){
- num1=Integer.parseInt(n1); ;
- result=num1*33.8;
- }
- if(f)
- resultTextField.setText(""+result);
- else
- resultTextField.setText("invalid io.");
- }
- private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- num1TextField.setText("");
- num2TextField.setText("");
- resultTextField.setText("");
- }
- private void num1TextFieldActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- /**
- * @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(MyCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(MyCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(MyCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(MyCalculator.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() {
- try {
- new MyCalculator().setVisible(true);
- } catch (Exception ex) {
- //Logger.getLogger(MyCalculator.class.getName()).log(Level.SEVERE, null, ex);
- System.out.println(""+ex);
- ex.printStackTrace();
- }
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton AddButton;
- private javax.swing.JButton CelsiusToFahrenheitConvertButton;
- private javax.swing.JButton ClearButton;
- private javax.swing.JButton DivideButton;
- private javax.swing.JButton KilometerToMileConvertButton;
- private javax.swing.JButton LogarithmButtom;
- private javax.swing.JButton ModulusButton;
- private javax.swing.JButton MultiplyButton;
- private javax.swing.JButton PercentageButton;
- private javax.swing.JButton SubstractButton;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JTextField num1TextField;
- private javax.swing.JTextField num2TextField;
- private javax.swing.JTextField resultTextField;
- // End of variables declaration
- }
Add Comment
Please, Sign In to add comment