0% found this document useful (0 votes)
21 views

Calculator

This document contains code for a Java calculator application. It defines a Calculator class that extends JFrame and contains methods for initializing the GUI components like buttons and text fields. It also includes handler methods for button clicks to perform calculations and display results. The main method creates an instance of the Calculator class and sets it to visible, launching the application.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Calculator

This document contains code for a Java calculator application. It defines a Calculator class that extends JFrame and contains methods for initializing the GUI components like buttons and text fields. It also includes handler methods for button clicks to perform calculations and display results. The main method creates an instance of the Calculator class and sets it to visible, launching the application.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

/*

* To change this template, choose Tools | Templates


* and open the template in the editor.
*/
package oop5;

/**
*
* @author ‫شهاب الحاج‬
*/
public class Calculator extends javax.swing.JFrame {

/**
* Creates new form Calculator
*/
public Calculator() {
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() {

jButton33 = new javax.swing.JButton();


jButtonmult = new javax.swing.JButton();
jButtonequle = new javax.swing.JButton();
jButton22 = new javax.swing.JButton();
txt = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton33.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N


jButton33.setText("3");
jButton33.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton33ActionPerformed(evt);
}
});

jButtonmult.setBackground(new java.awt.Color(204, 102, 255));


jButtonmult.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
jButtonmult.setText("X");
jButtonmult.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonmultActionPerformed(evt);
}
});

jButtonequle.setBackground(new java.awt.Color(255, 51, 51));


jButtonequle.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
jButtonequle.setText("=");
jButtonequle.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonequleActionPerformed(evt);
}
});

jButton22.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N


jButton22.setText("2");
jButton22.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton22ActionPerformed(evt);
}
});

txt.setBackground(new java.awt.Color(255, 255, 204));


txt.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
txt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtActionPerformed(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()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.LEADING)
.addComponent(txt)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayou
t.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING,
layout.createSequentialGroup()
.addComponent(jButton22,
javax.swing.GroupLayout.PREFERRED_SIZE, 106,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentP
lacement.RELATED)
.addComponent(jButtonequle,
javax.swing.GroupLayout.PREFERRED_SIZE, 106,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING,
layout.createSequentialGroup()
.addComponent(jButton33,
javax.swing.GroupLayout.PREFERRED_SIZE, 106,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentP
lacement.UNRELATED)
.addComponent(jButtonmult,
javax.swing.GroupLayout.PREFERRED_SIZE, 106,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap()
.addComponent(txt, javax.swing.GroupLayout.PREFERRED_SIZE, 67,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.BASELINE)
.addComponent(jButton33,
javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonmult,
javax.swing.GroupLayout.PREFERRED_SIZE, 90,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.BASELINE)
.addComponent(jButton22,
javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonequle,
javax.swing.GroupLayout.PREFERRED_SIZE, 90,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(23, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButtonmultActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:

n1=Double.parseDouble(txt.getText());
oper='*';
txt.setText("");
}

private void jButtonequleActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
n2=Double.parseDouble(txt.getText());
switch (oper){
case '*':
res=n1*n2;
break;
}
txt.setText(String.valueOf(res));
}

private void txtActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void jButton22ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
txt.setText(txt.getText()+"2");
}

private void jButton33ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
txt.setText(txt.getText()+"3");
}

double n1,n2,res;
char oper;
/**
* @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(Calculator.class.getName()).log(java.util.loggin
g.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.loggin
g.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.loggin
g.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.loggin
g.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Calculator().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton22;
private javax.swing.JButton jButton33;
private javax.swing.JButton jButtonequle;
private javax.swing.JButton jButtonmult;
private javax.swing.JTextField txt;
// End of variables declaration
}

=====================================

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package oop5;

/**
*
* @author ‫شهاب الحاج‬
*/
public class CalcTest {

public static void main(String[] args) {


new Calculator().setVisible(true);
}

You might also like