0

Ive made this calculator using java swing.I want to add new buttons to my frame if user selects 'Scientific' from file menu and remove buttons frome the frame if user selects 'Standard' from the menu. By default it is selected to standard. Write now a new window pops up above the other if i select scientific. Please help. This is my code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ItemListener.*;

/*
  <applet code = "calc12" width = 0 height =0>
  </applet>
*/

 public class calc12 extends JApplet implements ActionListener
 {
   
   
   JTextField output ;
   
   boolean first ;
   boolean entered;
   boolean entered2;
   boolean error;
   boolean start = true;
   int i , s , p , top  ,t ,top1 , d , o ,scfic=0;
   double op1 ,op2 ,op;
   char ch;
   char stack[] = new char[10];
   double postfix[]= new double[10]; 
   double arrdigit[] = new double[10];
   char arroperator[] = new char[10];	
   int nos[] = new int[5];
   double result[] = new double[10];
   double isdigit[] = new double[10];
     		


   public void init()
    {   
	
	
	JFrame frame = new JFrame("Calculator");
	frame.getContentPane();
	frame.setLayout ( new BorderLayout());
        JButton blist[] = new JButton[11];
        JPanel panno = new JPanel();    // a panel to enter number keys
        panno.setLayout( new GridLayout (4 ,4 ));
	
	for ( int i = 9 ; i >= 0 ; i-- )
	     {
	 	 blist[ i ] = new JButton (""+ i);
	 	 blist[ i ].setFont(new Font("georgia",Font.BOLD,20));
                 blist[ i ].addActionListener ( this );
	         panno.add( blist[ i ] );
	     }
        blist[10] = new JButton(".");
	blist[ 10 ].setFont(new Font("georgia",Font.BOLD,20));	
	blist[10].addActionListener( this );
	panno.add(blist[10]);
        frame.add ( panno , BorderLayout.SOUTH ) ;
	
        JPanel panop = new JPanel();   // a panel to enter operator keys
	panop.setLayout ( new GridLayout ( 1 , 5));
        JButton opList[] = new JButton[5];
	
        opList[ 0 ] = new JButton ("+");
	opList[ 0 ].setFont(new Font("georgia",Font.BOLD,20));
	opList[ 0 ].addActionListener ( this );
	panop.add( opList[ 0 ] );

        opList[ 1 ] = new JButton ("-");
	opList[ 1 ].setFont(new Font("georgia",Font.BOLD,20));
	opList[ 1 ].addActionListener ( this);
	panop.add( opList[ 1 ] );

        opList[ 2 ] = new JButton ("*");
	opList[ 2 ].setFont(new Font("georgia",Font.BOLD,20));
	opList[ 2 ].addActionListener ( this );
	panop.add( opList[ 2 ] );

	opList[ 3 ] = new JButton ("/");
	opList[ 3 ].setFont(new Font("georgia",Font.BOLD,20));
	opList[ 3 ].addActionListener ( this );
	panop.add( opList[ 3 ] );

        opList[ 4 ] = new JButton ("=");
	opList[ 4 ].setFont(new Font("georgia",Font.BOLD,20));
	opList[ 4 ].addActionListener (  this);
	panop.add( opList[ 4 ] );

        frame.add ( panop , BorderLayout.WEST); 
        
        JPanel extra = new JPanel();  // a panel that will initialise textfield( output ) to 0.0 whenever you click CLR or ON 
        
	JButton clr = new JButton ("CLR" );
	clr.setFont(new Font("georgia",Font.BOLD,20));
	clr.addActionListener( this);	
	extra.add( clr);
	
        frame.add ( extra , BorderLayout.EAST );
        output = new JTextField( "0.0" , 20 );
	
        output.setFont(new Font("georgia",Font.BOLD,20));
        output.setBackground(Color.white);
	output.setBounds(65, 80, 200, 80);

        output.setEditable(false); 
        output.setHorizontalAlignment( JTextField.RIGHT);
	frame.add ( output , BorderLayout.NORTH );

	

	//creating menu
	JMenuBar menuBar;
	JMenu file, edit;
	JMenuItem menuItem;
	JRadioButtonMenuItem rbMenuItem, rbMenuItem2;
	
	menuBar = new JMenuBar();
	file = new JMenu( "File");
	file.setMnemonic(KeyEvent.VK_F);
	menuBar.add(file);
	
	ButtonGroup group = new ButtonGroup();
	rbMenuItem = new JRadioButtonMenuItem("Standard");
	rbMenuItem.setSelected( true );
	rbMenuItem.setMnemonic( KeyEvent.VK_1);
	group.add(rbMenuItem);
	file.add(rbMenuItem);

	rbMenuItem = new JRadioButtonMenuItem("Scientific");
	rbMenuItem.setMnemonic( KeyEvent.VK_2);
	rbMenuItem.addActionListener( this );
	//rbMenuItem.addItemListener( this);
	group.add(rbMenuItem);
	file.add(rbMenuItem);
	
	file.addSeparator();
	 
	ButtonGroup group2 = new ButtonGroup();
	rbMenuItem2 = new JRadioButtonMenuItem("Basic");
	rbMenuItem2.setSelected( true );
	rbMenuItem2.setMnemonic( KeyEvent.VK_F4);
	group2.add(rbMenuItem2);
	file.add(rbMenuItem2);

	rbMenuItem2 = new JRadioButtonMenuItem("UnitConversion");
	rbMenuItem2.setMnemonic( KeyEvent.VK_U);
	group2.add(rbMenuItem2);
	file.add(rbMenuItem2);

	edit = new JMenu( "Edit");
	edit.setMnemonic(KeyEvent.VK_E);
	menuBar.add(edit);
	
	menuItem = new JMenuItem("Copy");
	menuItem.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_C , ActionEvent.ALT_MASK));
	edit.add(menuItem);

	menuItem = new JMenuItem("paste");
	menuItem.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_V , ActionEvent.ALT_MASK));
	edit.add(menuItem);

	frame.setJMenuBar( menuBar );
if(scfic==1)
{	
	JPanel panop2 = new JPanel();   // a panel to enter operator keys
	panop2.setLayout ( new GridLayout ( 1 , 4));
        JButton opList2[] = new JButton[4];
	
        opList2[ 0 ] = new JButton ("!");
	opList2[ 0 ].setFont(new Font("georgia",Font.BOLD,20));
	opList2[ 0 ].addActionListener ( this );
	panop2.add( opList2[ 0 ] );

        opList2[ 1 ] = new JButton ("^");
	opList2[ 1 ].setFont(new Font("georgia",Font.BOLD,20));
	opList2[ 1 ].addActionListener ( this);
	panop2.add( opList2[ 1 ] );

        opList2[ 2 ] = new JButton ("%");
	opList2[ 2 ].setFont(new Font("georgia",Font.BOLD,20));
	opList2[ 2 ].addActionListener ( this );
	panop2.add( opList2[ 2 ] );

	opList2[ 3 ] = new JButton ("$");
	opList2[ 3 ].setFont(new Font("georgia",Font.BOLD,20));
	opList2[ 3 ].addActionListener ( this );
	panop2.add( opList2[ 3 ] );

        /*opList2[ 4 ] = new JButton ("");
	opList[ 4 ].setFont(new Font("georgia",Font.BOLD,20));
	opList[ 4 ].addActionListener (  this);
	panop2.add( opList[ 4 ] );*/

        frame.add ( panop2 , BorderLayout.EAST); 

}

	//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.pack();
	//frame.setSize(500,517);
	frame.setLocation(300, 250);

	
	frame.setVisible( true );

     }	

        public void ClrListener( )
	   {	  
			start = false;
			first = true ;
			entered = false;
			entered2 = false;
			error = false;
			output.setText( "0.0");
                        int nos[] = new int[5];
			i = 0; d = 0; o = 0;s = 0 ; p = 0 ;op = 0; top = 0 ;t = 0 ;top1 =-1 ; op1 = 0 ; op2 = 0 ;  
                        stack[top] = '0';
		        result[0] = 0;   
	 	
            }
       public void actionPerformed (ActionEvent ae )
         {
             if( start )
               { 
	         ClrListener();
               }

           String str = ae.getActionCommand();

           if(str.equals("1") || str.equals("2") || str.equals("3") || str.equals("4") || str.equals("5") || str.equals("6") || str.equals("7") || str.equals("8") || str.equals("9") || str.equals("0") || str.equals(".") )
		{
                   NumberListener( str );
 		}
	 
           else if( str.equals("-") || str.equals("+") || str.equals("*") || str.equals("/") || str.equals("="))
		{
		   OperatorListener( str );
		}
	   
	else if(str.equals("Scientific"))
		{
		 Scientific();
		}
	  else
		{
		   ClrListener();
		}
         } 	
  

	 

	public void Scientific()
	{
			
           scfic=1;
	   init();	

	}

       public void NumberListener ( String operand ) 
         {       
                if(error)
		{
		 output.setText("Syntax Error . Press CLR to Continue");
		}

		 else
		 {		

   		  if( first )
		    {

			output.setText( operand );
			first = false ;
		    }
		  else
		    {
			if ( entered )
			  {
			     output.setText( operand );
			     entered = false ;
			  }
			else
                          {
        		     output.setText( output.getText() + operand );
		          }
                    }
		entered2 = true;
           	 }// end of else error= true
	}// end of function		
					 	     

	 public void OperatorListener( String operator )
           {
      	      if(entered2)		
	       {
		  
  		  if(operator.equals("="))
		     {  
			String no1 = output.getText();
		    	entered = true ;
		    	arrdigit[d] = Double.parseDouble( no1 );
		    	d++;
		    	entered2 = false;		
		    	CalculateListener();
		     }
		
		  else
                     {
			String no1 = output.getText();
			output.setText( no1 + operator ) ;
			entered = true ;
			arrdigit[d] = Double.parseDouble(no1);
			d++;
			arroperator[o] = operator.charAt(0);
			o++;
			entered2 = false;
       		     }
	       }  
	    else
	       { 
		 error = true;
		 output.setText("Syntax Error . Press CLR to Continue");
	       }			
	  }
	
	  public void CalculateListener()
	   {		
	  	// code to calculate. 
          
     }//end of calculate



}//end of class

Edited by Nick Evan: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by mikeNIkesT
0

What exactly is the problem? Please only include relevant code. You can always just "hide/show" buttons to give the illusion of new buttons. It would be nice if you could re-post the code formatted so that is more readable.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.