I am coding a simple calculator in Eclipse version 4.3.1 and I get this message when I run the program:
Exception in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself
at java.awt.Container.checkAddToSelf(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at FunctionalCalculator.<init>(FunctionalCalculator.j ava:98)
at FunctionalCalculator.main(FunctionalCalculator.jav a:152)
here are the code:
import javax.swing.*; import java.awt.GridLayout; import java.awt.BorderLayout; public class FunctionalCalculator{ JTextField Field = new JTextField(30); JButton button0=new JButton("0"); JButton button1=new JButton("1"); JButton button2=new JButton("2"); JButton button3=new JButton("3"); JButton button4=new JButton("4"); JButton button5=new JButton("5"); JButton button6=new JButton("6"); JButton button7=new JButton("7"); JButton button8=new JButton("8"); JButton button9=new JButton("9"); JButton Minus=new JButton ("-"); JButton Add=new JButton ("+"); JButton Divide=new JButton ("/"); JButton Multiply=new JButton ("*"); JButton FullStop= new JButton("."); JButton Equal= new JButton("="); JPanel pl = new JPanel(); FunctionalCalculator() { //first line is written to assign windowContent into a new object JPanel(). BorderLayout bl = new BorderLayout(); pl.setLayout(bl); pl.add("North", Field); JPanel pl = new JPanel(); GridLayout grid = new GridLayout (4,3); pl.setLayout(grid); pl.add(button1); pl.add(button2); pl.add(button3); pl.add(button4); pl.add(button5); pl.add(button6); pl.add(button7); pl.add(button8); pl.add(button9); pl.add(button0); pl.add(FullStop); pl.add(Equal); pl.add("Center",pl); JPanel part2= new JPanel(); GridLayout grid2 =new GridLayout(4,1); part2.setLayout(grid2); part2.add(Add); part2.add(Minus); part2.add(Multiply); part2.add(Divide); /*the new panel is added to the east (right)*/ pl.add("east",part2); /*the panel is assigned with a title called calculator */ JFrame frame = new JFrame("Calculator"); frame.setContentPane(pl); frame.pack(); frame.setVisible(true); CalculatorFunctionality function= new CalculatorFunctionality(this); button0.addActionListener(function); button1.addActionListener(function); button2.addActionListener(function); button3.addActionListener(function); button4.addActionListener(function); button5.addActionListener(function); button7.addActionListener(function); button8.addActionListener(function); button9.addActionListener(function); FullStop.addActionListener(function); Add.addActionListener(function); Minus.addActionListener(function); Divide.addActionListener(function); Multiply.addActionListener(function); Equal.addActionListener(function); } public static void main(String[] args){ FunctionalCalculator calculator= new FunctionalCalculator(); } }
here is the second code:
import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JButton; public class CalculatorFunctionality implements ActionListener { FunctionalCalculator parent; char SelectedMath=' '; double currentResult=0; CalculatorFunctionality(FunctionalCalculator parent){ this.parent=parent; } public void actionPerformed(ActionEvent e){ JButton ClickedButton=(JButton) e.getSource(); String DispTextField = parent.Field.getText(); double DisplayValue=0; if(!"".equals(DispTextField)){ DisplayValue= Double.parseDouble(DispTextField); } // Object src = e.getSource(); if(src==parent.Add){ SelectedMath='+'; currentResult=DisplayValue; parent.Field.setText(""); } else if(src==parent.Minus){ SelectedMath='-'; currentResult=DisplayValue; //clears text box when user presses new button in calculator. parent.Field.setText(""); } else if(src==parent.Divide){ SelectedMath='/'; currentResult=DisplayValue; parent.Field.setText(""); } else if(src==parent.Multiply){ SelectedMath='*'; currentResult=DisplayValue; parent.Field.setText(""); } else if(src==parent.Equal){ if(SelectedMath=='+') currentResult=DisplayValue; parent.Field.setText(""+currentResult); } else if(SelectedMath=='-'){ currentResult=DisplayValue; parent.Field.setText(""+currentResult); } else if(SelectedMath=='/'){ currentResult=DisplayValue; parent.Field.setText(""+ currentResult); } else if(SelectedMath=='*'){ currentResult=DisplayValue; parent.Field.setText(""+ currentResult); } else{ String ButtonLabelClicked= ClickedButton.getText(); parent.Field.setText(DispTextField + ButtonLabelClicked); } } }