Hello everyone,
I have a program that supposed to open a Swing window and create three tabs for calculating Factorials, Permutations, and Combinations.
Once you click on one of those tabs, text boxes and a button will appear. You type in the numbers, and it will give the desired calculations for you.
I compiled this program and it was successful, but when I tried to run it, I got this error message:
Exception in thread "main" java.lang.IllegalArgumentException: adding container'
s parent to itself
at java.awt.Container.addImpl(Container.java:1017)
at java.awt.Container.add(Container.java:903)
at newFactorialsGUI.<init>(newFactorialsGUI.java:40)
at newFactorialsGUI.createAndShowGUI(newFactorialsGUI.java:50)
at newFactorialsGUI.main(newFactorialsGUI.java:172)
I am probably making some painfully obvious and stupid mistake; but please excuse me, I am fairly new to Java.
If anybody can tell me what I did wrong and how I can fix so, that would be great. Thank you (and please excuse the bizarre variable and constructor names).
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.JComponent;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.SwingUtilities.*;
import java.beans.*;
public class newFactorialsGUI extends JFrame
{
JLabel informationarea;
JFrame frame;
String heading1 = "Enter the number that goes before the ! now:";
String heading2 = "Enter the numbers that go before and after the P now:";
String heading3 = "Enter the numbers that go before and after the C now:";
public newFactorialsGUI(JFrame frame)
{
this.frame = frame;
informationarea = new JLabel("Select a tab, then enter the numbers and this program will calculate it for you.", JLabel.CENTER);
Border padding = BorderFactory.createEmptyBorder(20,20,5,20);
JPanel allincommonpanel = new JPanel();
allincommonpanel.setBorder(padding);
JTabbedPane Cardamon = new JTabbedPane();
Cardamon.addTab("Factorials", null, allincommonpanel, heading1);
Cardamon.addTab("Permutations", null, allincommonpanel, heading2);
Cardamon.addTab("Combinations", null, allincommonpanel, heading3);
allincommonpanel.add(Cardamon, BorderLayout.CENTER);
allincommonpanel.add(informationarea, BorderLayout.PAGE_END);
informationarea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
createFactorialBox();
createPermutationBox();
createCombinationBox();
}
public static void createAndShowGUI()
{
JFrame frame = new JFrame("Factorials, Permutations, and Combinations Calculator");
new newFactorialsGUI(frame);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public int factorialize(int inputfactorial)
{
int factorialfinale = 1;
int factorialengine;
for (factorialengine = inputfactorial; factorialengine >= 1; factorialengine--)
{
factorialfinale *= factorialengine;
}
return factorialfinale;
}
public int permutationize(int inputpermutation1, int inputpermutation2)
{
int permutationsfinale = 1;
int rununtil;
rununtil = inputpermutation1 - inputpermutation2;
rununtil++;
int permutationsengine;
for (permutationsengine = inputpermutation1; permutationsengine >= rununtil; permutationsengine--)
{
permutationsfinale *= permutationsengine;
}
return permutationsfinale;
}
public int combinationize(int inputcombination1, int inputcombination2)
{
int combinationsqualify = 1;
int rununtil;
rununtil = inputcombination1 - inputcombination2;
rununtil++;
int comboengine1;
int combinationsqualify1 = 1;
for (comboengine1 = inputcombination1; comboengine1 >= rununtil; comboengine1--)
{
combinationsqualify1 *= comboengine1;
}
int comboengine2;
int combinationsqualify2 = 1;
for (comboengine2 = inputcombination2; comboengine2 >= 1; comboengine2--)
{
combinationsqualify2 *= comboengine2;
}
int combinationsfinale = combinationsqualify1 / combinationsqualify2;
return combinationsfinale;
}
void setLabel(String newtxt)
{
informationarea.setText(newtxt);
}
private void createFactorialBox()
{
JPanel controlpanel2 = new JPanel();
controlpanel2.setLayout(new BorderLayout());
final JTextField input = new JTextField(15);
JButton buttonOK = new JButton("Calculate");
controlpanel2.add(input);
controlpanel2.add(buttonOK);
buttonOK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String rawinput = input.getText();
int finalinput = Integer.parseInt(rawinput);
int answerfinale = factorialize(finalinput);
setLabel("The answer to your factorial problem is: " + answerfinale);
}
}
);
}
private void createPermutationBox()
{
JPanel controlpanel1 = new JPanel();
controlpanel1.setLayout(new BorderLayout());
final JTextField input1 = new JTextField(15);
final JTextField input2 = new JTextField(15);
JButton buttonOK = new JButton("Calculate");
controlpanel1.add(input1);
controlpanel1.add(input2);
controlpanel1.add(buttonOK);
buttonOK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String rawinput1 = input1.getText();
int finalinput1 = Integer.parseInt(rawinput1);
String rawinput2 = input2.getText();
int finalinput2 = Integer.parseInt(rawinput2);
int answerfinale = permutationize(finalinput1, finalinput2);
setLabel("The answer to your permutation problem is: " + answerfinale);
}
}
);
}
private void createCombinationBox()
{
JPanel controlpanel3 = new JPanel();
controlpanel3.setLayout(new BorderLayout());
final JTextField input1 = new JTextField(15);
final JTextField input2 = new JTextField(15);
JButton buttonOK = new JButton("Calculate");
controlpanel3.add(input1);
controlpanel3.add(input2);
controlpanel3.add(buttonOK);
buttonOK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String rawinput1 = input1.getText();
int finalinput1 = Integer.parseInt(rawinput1);
String rawinput2 = input2.getText();
int finalinput2 = Integer.parseInt(rawinput2);
int answerfinale = combinationize(finalinput1, finalinput2);
setLabel("The answer to your combination problem is: " + answerfinale);
}
}
);
}
public static void main(String[] args)
{
createAndShowGUI();
}
}