If your teacher is using this exercise to help you learn if statements, then you need to consider your more complex cases before your basic cases.
Know that if Boxes{1,2,3,4} are all selected but you perform your check on Boxes{1} first, then you will only ever get your Frame9 to show, no matter how many boxes are checked. Try rearranging your if structure to check the case that all 4 are checked, Then if any 3 are checked, and work your way backwards down to your final if/else statement of the individual boxes.
if(all 4 checked)
else if (any 3 checked)
else if (any 2 checked)
else if (any 1 checked)
This 2nd option may be a bit more then what you need for your homework or have already learned. But consider initializing the various states inside a HashMap? Then when a button is checked, you go back and check the state of each check box. If the box is checked you set some form of a flag (bitwise operation works good here). Once you do this you can perform a look up in the hashmap to retrieve the proper class.
The positive is less if statements to determine your course of action, the negative is more memory and increased initialization time.
e.x. Assume all of your "new Frame" are classes which extend JFrame. If you create an action listener on your buttons you could have the following global variable
HashMap<Integer, Class<? extends JFrame>> frameName = null;
Then when the constructor is called, you populate the HashMap
frameName = new HashMap<Integer, Class<? extends JFrame>>();
frameName.put(1<<0, Frame9.class); //only the first box is checked
//Box 2,3 and 4 checked, resulting in 1110 (binary) which stores
//in the hashmap as 14
frameName.put(1<<1 | 1<<2 | 1<<3, Frame21.class);
//add this for all 15 options.
Once the check Box is clicked, you can then populate the state and perform a look up in the HashMap table. assuming the default action is to create the given JFrame and show it.
public void actionPerformed(ActionEvent e) {
JFrame myNewFrame = null;
int state = 0;
if(e.getSource() instanceof JCheckBox){
if(((JCheckBox)e.getSource()).isSelected()){
if(cb1.isSelected())
state |= 1<<0; // 1b
if(cb2.isSelected())
state |= 1<<1; // 10b
if(cb3.isSelected())
state |= 1<<2; // 100b
if(cb4.isSelected())
state |= 1<<3; // 1000b
try {
myNewFrame = frameName.get(state).newInstance();
myNewFrame.setVisible(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
This is by no means the most optimal solution, but it does help cut down on the amount of nested if statements you have in your solution and makes it more readable.