Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package guicalculator;
- import java.awt.AWTException;
- public class CalculatorGui extends javax.swing.JFrame {
- double num, num1, ans;
- String sign;
- int cal;
- /**
- * Creates new form CalculatorGui
- */
- public CalculatorGui() {
- super("CalCulator");
- initComponents();
- rdbt1.setEnabled(false);
- }
- public void arithmetic_operation() {
- switch (cal) {
- case 1:
- num1 = Double.parseDouble(text.getText());
- ans = num + num1;
- l.setText(l.getText()+num1);
- text.setText(Double.toString(ans));
- break;
- case 2:
- num1 = Double.parseDouble(text.getText());
- ans = num - num1;
- l.setText(l.getText()+num1);
- text.setText(Double.toString(ans));
- break;
- case 3:
- num1 = Double.parseDouble(text.getText());
- ans = num * num1;
- l.setText(l.getText()+num1);
- text.setText(Double.toString(ans));
- break;
- case 4:
- try {
- num1 = Double.parseDouble(text.getText());
- ans = num / num1;
- l.setText(l.getText()+num1);
- text.setText(Double.toString(ans));
- break;
- } catch (ArithmeticException e) {
- text.setText("Math Error");
- }
- case 5:
- try{
- ans = fact(num);
- text.setText(Double.toString(ans));
- break;
- }catch(ArithmeticException e){
- text.setText("Invalid");
- }
- case 6:
- num = Double.parseDouble(text.getText());
- l.setText(l.getText() + num + ")");
- ans = Math.sin(num);
- text.setText(Double.toString(ans));
- break;
- case 7:
- num = Double.parseDouble(text.getText());
- l.setText(l.getText() + num + ")");
- ans = Math.cos(num);
- text.setText(Double.toString(ans));
- break;
- case 8:
- num = Double.parseDouble(text.getText());
- l.setText(l.getText() + num + ")");
- ans = Math.tan(num);
- text.setText(Double.toString(ans));
- break;
- case 9:
- try{
- num = Double.parseDouble(text.getText());
- l.setText(l.getText() + num + ")");
- ans = 1.0/Math.tan(num);
- text.setText(Double.toString(ans));
- break;
- }catch(ArithmeticException e){
- text.setText("Invalid");
- }
- case 10:
- try{
- num1 = Double.parseDouble(text.getText());
- l.setText(l.getText() + num);
- ans = Math.pow(num,num1);
- text.setText(Double.toString(ans));
- break;
- }catch(NumberFormatException e){
- text.setText("Invalid");
- }
- }
- }
- static double fact(double n) {
- if (n == 1.0) {
- return 1;
- } else {
- return (n * fact(n - 1.0));
- }
- }
- @Override
- public void enable() {
- text.setEnabled(true);
- rdbt1.setEnabled(false);
- rdbt2.setEnabled(true);
- bt1.setEnabled(true);
- bt20.setEnabled(true);
- bt20.setEnabled(true);
- bt25.setEnabled(true);
- bt28.setEnabled(true);
- bt2.setEnabled(true);
- bt30.setEnabled(true);
- bt32.setEnabled(true);
- bt3.setEnabled(true);
- bt4.setEnabled(true);
- bt5.setEnabled(true);
- bt6.setEnabled(true);
- bt19.setEnabled(true);
- bt23.setEnabled(true);
- bt26.setEnabled(true);
- bt29.setEnabled(true);
- bt31.setEnabled(true);
- bt33.setEnabled(true);
- bt22.setEnabled(true);
- l.setEnabled(true);
- btb.setEnabled(true);
- bts.setEnabled(true);
- btc.setEnabled(true);
- btc1.setEnabled(true);
- btsq.setEnabled(true);
- btr.setEnabled(true);
- btc.setEnabled(true);
- btt.setEnabled(true);
- btp.setEnabled(true);
- }
- @Override
- public void disable() {
- text.setEnabled(false);
- rdbt1.setEnabled(true);
- rdbt2.setEnabled(false);
- bt1.setEnabled(false);
- bt20.setEnabled(false);
- bt20.setEnabled(false);
- bt25.setEnabled(false);
- bt28.setEnabled(false);
- bt2.setEnabled(false);
- bt30.setEnabled(false);
- bt32.setEnabled(false);
- bt3.setEnabled(false);
- bt4.setEnabled(false);
- bt5.setEnabled(false);
- bt6.setEnabled(false);
- bt19.setEnabled(false);
- bt23.setEnabled(false);
- bt26.setEnabled(false);
- bt29.setEnabled(false);
- bt31.setEnabled(false);
- bt33.setEnabled(false);
- bt22.setEnabled(false);
- l.setEnabled(false);
- btb.setEnabled(false);
- bts.setEnabled(false);
- btc.setEnabled(false);
- btc1.setEnabled(false);
- btsq.setEnabled(false);
- btr.setEnabled(false);
- btc.setEnabled(false);
- btt.setEnabled(false);
- btp.setEnabled(false);
- }
- /**
- * 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() {
- buttonGroup1 = new javax.swing.ButtonGroup();
- jButton3 = new javax.swing.JButton();
- jPanel1 = new javax.swing.JPanel();
- text = new javax.swing.JTextField();
- bt1 = new javax.swing.JButton();
- rdbt1 = new javax.swing.JRadioButton();
- rdbt2 = new javax.swing.JRadioButton();
- bt19 = new javax.swing.JButton();
- bt20 = new javax.swing.JButton();
- bt22 = new javax.swing.JButton();
- bt23 = new javax.swing.JButton();
- bt2 = new javax.swing.JButton();
- bt25 = new javax.swing.JButton();
- bt26 = new javax.swing.JButton();
- bt3 = new javax.swing.JButton();
- bt28 = new javax.swing.JButton();
- bt29 = new javax.swing.JButton();
- bt4 = new javax.swing.JButton();
- bt30 = new javax.swing.JButton();
- bt31 = new javax.swing.JButton();
- bt5 = new javax.swing.JButton();
- bt32 = new javax.swing.JButton();
- bt33 = new javax.swing.JButton();
- bt6 = new javax.swing.JButton();
- l = new javax.swing.JLabel();
- btb = new javax.swing.JButton();
- bts = new javax.swing.JButton();
- btc = new javax.swing.JButton();
- btt = new javax.swing.JButton();
- btc1 = new javax.swing.JButton();
- btsq = new javax.swing.JButton();
- btp = new javax.swing.JButton();
- btr = new javax.swing.JButton();
- jButton3.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
- jButton3.setText("sin");
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jPanel1.setBackground(new java.awt.Color(153, 255, 255));
- jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
- jPanel1.setName(""); // NOI18N
- text.setEditable(false);
- text.setBackground(new java.awt.Color(15, 88, 108));
- text.setForeground(new java.awt.Color(102, 255, 204));
- text.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
- text.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- textActionPerformed(evt);
- }
- });
- bt1.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt1.setText("9");
- bt1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt1ActionPerformed(evt);
- }
- });
- buttonGroup1.add(rdbt1);
- rdbt1.setText("ON");
- rdbt1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- rdbt1ActionPerformed(evt);
- }
- });
- buttonGroup1.add(rdbt2);
- rdbt2.setText("OFF");
- rdbt2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- rdbt2ActionPerformed(evt);
- }
- });
- bt19.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt19.setText("x!");
- bt19.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt19ActionPerformed(evt);
- }
- });
- bt20.setBackground(new java.awt.Color(153, 0, 0));
- bt20.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt20.setForeground(new java.awt.Color(255, 255, 255));
- bt20.setText("AC");
- bt20.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt20ActionPerformed(evt);
- }
- });
- bt22.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt22.setText("8");
- bt22.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt22ActionPerformed(evt);
- }
- });
- bt23.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt23.setText("/");
- bt23.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt23ActionPerformed(evt);
- }
- });
- bt2.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt2.setText("7");
- bt2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt2ActionPerformed(evt);
- }
- });
- bt25.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt25.setText("6");
- bt25.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt25ActionPerformed(evt);
- }
- });
- bt26.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt26.setText("X");
- bt26.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt26ActionPerformed(evt);
- }
- });
- bt3.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt3.setText("5");
- bt3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt3ActionPerformed(evt);
- }
- });
- bt28.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt28.setText("4");
- bt28.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt28ActionPerformed(evt);
- }
- });
- bt29.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt29.setText("-");
- bt29.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt29ActionPerformed(evt);
- }
- });
- bt4.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt4.setText("3");
- bt4.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt4ActionPerformed(evt);
- }
- });
- bt30.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt30.setText("2");
- bt30.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt30ActionPerformed(evt);
- }
- });
- bt31.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt31.setText("+");
- bt31.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt31ActionPerformed(evt);
- }
- });
- bt5.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt5.setText("1");
- bt5.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt5ActionPerformed(evt);
- }
- });
- bt32.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt32.setText(".");
- bt32.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt32ActionPerformed(evt);
- }
- });
- bt33.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt33.setText("=");
- bt33.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt33ActionPerformed(evt);
- }
- });
- bt6.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
- bt6.setText("0");
- bt6.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bt6ActionPerformed(evt);
- }
- });
- l.setForeground(new java.awt.Color(255, 51, 51));
- btb.setBackground(new java.awt.Color(153, 0, 0));
- btb.setForeground(new java.awt.Color(255, 255, 255));
- btb.setText("DEL");
- btb.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btbActionPerformed(evt);
- }
- });
- bts.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
- bts.setText("sin");
- bts.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btsActionPerformed(evt);
- }
- });
- btc.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
- btc.setText("cos");
- btc.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btcActionPerformed(evt);
- }
- });
- btt.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
- btt.setText("tan");
- btt.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bttActionPerformed(evt);
- }
- });
- btc1.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
- btc1.setText("cot");
- btc1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btc1ActionPerformed(evt);
- }
- });
- btsq.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
- btsq.setText("x2");
- btsq.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btsqActionPerformed(evt);
- }
- });
- btp.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
- btp.setText("pow");
- btp.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btpActionPerformed(evt);
- }
- });
- btr.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
- btr.setText(" √");
- btr.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btrActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
- jPanel1.setLayout(jPanel1Layout);
- jPanel1Layout.setHorizontalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(text)
- .addComponent(l, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(rdbt1)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(bts)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(btc)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(btt)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(rdbt2)
- .addComponent(btc1))))
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(bt19)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(btsq)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(btp)
- .addGap(4, 4, 4)
- .addComponent(btr, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGap(73, 73, 73)
- .addComponent(btb, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(bt1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(0, 0, Short.MAX_VALUE))
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(bt2, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(bt22, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(bt3, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(bt25, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addGap(11, 11, 11))
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(bt4, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(bt28, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addGap(11, 11, 11))
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(bt5, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(bt30, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addGap(11, 11, 11))
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(bt6, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(bt32, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(11, 11, 11)))
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(bt33, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(bt31, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(bt29, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(bt26, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(bt20, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(bt23, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))))
- .addGap(1, 1, 1)))
- .addGap(148, 148, 148))
- );
- jPanel1Layout.setVerticalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(l, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(text, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(10, 10, 10)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(rdbt1)
- .addComponent(rdbt2))
- .addGap(18, 18, 18)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(bts)
- .addComponent(btc)
- .addComponent(btt)
- .addComponent(btc1))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(bt19)
- .addComponent(btsq)
- .addComponent(btr)
- .addComponent(btp))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(bt1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(bt20, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(btb, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(bt2, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(bt22, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(bt23, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(bt3, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(bt25, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(bt26, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(bt4, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(bt28, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(bt29, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(bt5, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(bt30, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(bt31, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(bt6, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(bt32, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(bt33, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- 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()
- .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addContainerGap())
- );
- pack();
- }// </editor-fold>
- private void textActionPerformed(java.awt.event.ActionEvent evt) {
- text.setEditable(false);
- }
- private void bt1ActionPerformed(java.awt.event.ActionEvent evt) {
- text.setText(text.getText() + "9");
- }
- private void bt2ActionPerformed(java.awt.event.ActionEvent evt) {
- text.setText(text.getText() + "7");
- }
- private void bt22ActionPerformed(java.awt.event.ActionEvent evt) {
- text.setText(text.getText() + "8");
- }
- private void bt3ActionPerformed(java.awt.event.ActionEvent evt) {
- text.setText(text.getText() + "5");
- }
- private void bt25ActionPerformed(java.awt.event.ActionEvent evt) {
- text.setText(text.getText() + "6");
- }
- private void bt4ActionPerformed(java.awt.event.ActionEvent evt) {
- text.setText(text.getText() + "3");
- }
- private void bt28ActionPerformed(java.awt.event.ActionEvent evt) {
- text.setText(text.getText() + "4");
- }
- private void bt5ActionPerformed(java.awt.event.ActionEvent evt) {
- text.setText(text.getText() + "1");
- }
- private void bt30ActionPerformed(java.awt.event.ActionEvent evt) {
- text.setText(text.getText() + "2");
- }
- private void bt6ActionPerformed(java.awt.event.ActionEvent evt) {
- text.setText(text.getText() + "0");
- }
- private void bt20ActionPerformed(java.awt.event.ActionEvent evt) {
- text.setText("");
- l.setText("");
- }
- private void bt32ActionPerformed(java.awt.event.ActionEvent evt) {
- text.setText(text.getText() + ".");
- }
- private void rdbt1ActionPerformed(java.awt.event.ActionEvent evt) {
- enable();
- }
- private void rdbt2ActionPerformed(java.awt.event.ActionEvent evt) {
- disable();
- }
- private void bt31ActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- num = Double.parseDouble(text.getText());
- cal = 1;
- sign = "+";
- text.setText("");
- l.setText(num + "+");
- } catch (NumberFormatException e) {
- text.setText("Invalid");
- }
- }
- private void bt33ActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- arithmetic_operation();
- } catch (ArithmeticException e ) {
- text.setText("Invalid");
- }
- }
- private void bt29ActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- num = Double.parseDouble(text.getText());
- cal = 2;
- text.setText("");
- sign = "-";
- l.setText(num + "-");
- } catch (NumberFormatException e) {
- text.setText("Invalid");
- }
- }
- private void bt26ActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- num = Double.parseDouble(text.getText());
- cal = 3;
- text.setText("");
- sign = "X";
- l.setText(num + "X");
- } catch (NumberFormatException e) {
- text.setText("Invalid");
- }
- }
- private void bt23ActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- num = Double.parseDouble(text.getText());
- cal = 4;
- text.setText("");
- sign = "/";
- l.setText(num + "/");
- } catch (NumberFormatException e) {
- text.setText("Invalid");
- }
- }
- private void bt19ActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- cal = 5;
- num = Double.parseDouble(text.getText());
- sign = "!";
- l.setText(num + "!");
- } catch (NumberFormatException e) {
- text.setText("Invalid");
- }
- }
- private void btbActionPerformed(java.awt.event.ActionEvent evt) {
- int ln = text.getText().length();
- int n = ln - 1;
- if (ln > 0) {
- StringBuilder back = new StringBuilder(text.getText());
- back.deleteCharAt(n);
- String store = back.toString();
- text.setText(store);
- }
- }
- private void btsActionPerformed(java.awt.event.ActionEvent evt) {
- l.setText("sin" + "(");
- cal = 6;
- }
- private void btcActionPerformed(java.awt.event.ActionEvent evt) {
- l.setText("cos" + "(");
- cal = 7;
- }
- private void bttActionPerformed(java.awt.event.ActionEvent evt) {
- l.setText("tan" + "(");
- cal = 8;
- }
- private void btc1ActionPerformed(java.awt.event.ActionEvent evt) {
- l.setText("cot" + "(");
- cal = 9;
- }
- private void btsqActionPerformed(java.awt.event.ActionEvent evt) {
- try{
- num=Double.parseDouble(text.getText());
- ans=num*num;
- l.setText(num+" * "+num);
- text.setText(Double.toString(ans));
- }catch(NumberFormatException e){
- text.setText("Invalid");
- }
- }
- private void btpActionPerformed(java.awt.event.ActionEvent evt) {
- try{
- cal=10;
- num=Double.parseDouble(text.getText());
- l.setText(num+" pow ");
- text.setText("");
- }catch(NumberFormatException e){
- text.setText("Invalid");
- }
- }
- private void btrActionPerformed(java.awt.event.ActionEvent evt) {
- try{
- num=Double.parseDouble(text.getText());
- l.setText("√"+num);
- ans=Math.sqrt(num);
- text.setText(Double.toString(ans));
- }catch(NumberFormatException e){
- text.setText("Invalid");
- }
- }
- /**
- * @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(CalculatorGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(CalculatorGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(CalculatorGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(CalculatorGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- @Override
- public void run() {
- new CalculatorGui().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton bt1;
- private javax.swing.JButton bt19;
- private javax.swing.JButton bt2;
- private javax.swing.JButton bt20;
- private javax.swing.JButton bt22;
- private javax.swing.JButton bt23;
- private javax.swing.JButton bt25;
- private javax.swing.JButton bt26;
- private javax.swing.JButton bt28;
- private javax.swing.JButton bt29;
- private javax.swing.JButton bt3;
- private javax.swing.JButton bt30;
- private javax.swing.JButton bt31;
- private javax.swing.JButton bt32;
- private javax.swing.JButton bt33;
- private javax.swing.JButton bt4;
- private javax.swing.JButton bt5;
- private javax.swing.JButton bt6;
- private javax.swing.JButton btb;
- private javax.swing.JButton btc;
- private javax.swing.JButton btc1;
- private javax.swing.JButton btp;
- private javax.swing.JButton btr;
- private javax.swing.JButton bts;
- private javax.swing.JButton btsq;
- private javax.swing.JButton btt;
- private javax.swing.ButtonGroup buttonGroup1;
- private javax.swing.JButton jButton3;
- private javax.swing.JPanel jPanel1;
- private javax.swing.JLabel l;
- private javax.swing.JRadioButton rdbt1;
- private javax.swing.JRadioButton rdbt2;
- private javax.swing.JTextField text;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement