开发者

Java homework question

开发者 https://www.devze.com 2023-01-15 22:13 出处:网络
kindly suggest how to use multiple checkboxes using else if in java and please check the code below if(cb1.isSelected())

kindly suggest how to use multiple checkboxes using else if in java and please check the code below

if(cb1.isSelected())
MyFrame9 m1=new MyFrame9();
else if(cb2.isSelected())
MyFrame10 m1=new MyFrame10();
else if(cb3.isSelected())
MyFrame11 m1=new MyFrame11();
else if(cb4.isSelected())
MyFrame12 m1=new MyFrame12();
else if(cb1.isSelected() && cb2.isSelected())
MyFrame13 m1=new MyFrame13();
else if(cb1.isSelected() && cb3.isSelected())
MyFrame14 m1=new MyFrame14();
else if(cb1.isSelected() && cb4.isSelected())
MyFrame15 m1=new MyFrame15();
else if(cb2.isSelected() && cb3.isSelected())
MyFrame16 m1=new MyFrame16();
else if(cb2.isSelected() && cb4.isSelected())
MyFrame17 m1=new MyFrame17();
else if(cb3.isSelected() && cb4.isSelected())
MyFrame18 m1=new MyFrame18();
else if(cb1.isSelected() && cb2.isSelected() && cb3.isSelected())
MyFrame19 m1=new MyFrame19();
else if(cb1.isSelected() && cb2.isSelected() && cb4.isSelected())
MyFrame20 m1=new MyFrame20();
else if(cb2.isSelected() && cb3.isSelected() && cb4.isSelected())
MyFrame21 m1=new MyFrame21();
else if(cb3.isSelected() && cb4.isSelected() && cb1.isSelected())
M开发者_JAVA技巧yFrame22 m1=new MyFrame22();
else(cb1.isSelected() && cb2.isSelected() && cb3.isSelected() && cb4.isSelected())
MyFrame23 m1=new MyFrame23();
}


The code is kind of awful; for each else content the code declare and instantiate a variable which will never be used (as a variable declared in a block can't be used outside the said block) your compiler will say that your code is not a valid statement and won't compile.

Plus, most of the if will never pass because the condition can't be accessed. For example :

if(cb1.isSelected())
    MyFrame9 m1=new MyFrame9();
else if(cb2.isSelected())
    MyFrame10 m1=new MyFrame10();
else if(cb3.isSelected())
    MyFrame11 m1=new MyFrame11();
else if(cb4.isSelected())
    MyFrame12 m1=new MyFrame12();
else if(cb1.isSelected() && cb2.isSelected())
    MyFrame13 m1=new MyFrame13();

if cb1 is checked, MyFrame9 m1=new MyFrame9() will be called. If cb2 is checked MyFrame10 m1=new MyFrame10(); so MyFrame13 m1=new MyFrame13(); can't be called at all.


Resources :

  • JLS - Blocks and statements


First of all, consider using a switch statement instead of a chain of else if statements.

Second, are those really your variable names? You may want to declare variables with a little more context to them.

Finally, what is this code supposed to be doing? Your question asks how to use multiple checkboxes with else if. Well, you've done that. It's a mess, and it doesn't seem to do much of anything, but you've done it. Is something not working? If so, please explain.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消