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

Recommended Answers

All 5 Replies

When the user clicks the button you call init() (with a flag set), which always creates a new window from scratch. Rather than keep on adding & removing buttons, why not add panop2, but set its visibility to false. Then in the button handler you just have to make panop2 visible, and re-pack the window - without calling init() again. Similarly if the user de-selects scientific - just make it invisible & re-pack.

When the user clicks the button you call init() (with a flag set), which always creates a new window from scratch. Rather than keep on adding & removing buttons, why not add panop2, but set its visibility to false. Then in the button handler you just have to make panop2 visible, and re-pack the window - without calling init() again. Similarly if the user de-selects scientific - just make it invisible & re-pack.

Thankyou so much. Thats a nice idea. But i dont know what do you mean by repacking the window? please let me know the syntax if there,s any.

Same as line 198 above. If you don't call pack() again the window won't be resized to fit the newly visible panel.

Same as line 198 above. If you don't call pack() again the window won't be resized to fit the newly visible panel.

Thankyou so much. Its working just the way i wanted.

Mark this thread closed now?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.