嵌套的if语句在另一个嵌套的if语句中

时间:2020-06-09 04:02:42

标签: java if-statement null nested-if

下面的Java代码具有3个标签,分别为button1,button2和button3。当用户导入图像时,当且仅当button1上没有图像时,才应首先进入button1。然后,下次导入图像时,仅当button1上有图像时,才应转到button。当且仅当button1和button上有图像时,才应第三次将图像放置在button3上。该代码适用于button1和button2,但不适用于button3。因此嵌套if语句从else {。开始。

  importBtn.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {


           JFileChooser file = new JFileChooser();
           file.setCurrentDirectory(new File(System.getProperty("user.home")));
           //filter the files
           FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpg","gif","png");
           file.addChoosableFileFilter(filter);
           int result = file.showSaveDialog(null);


           if(result == JFileChooser.APPROVE_OPTION){

               //NotWorking make check null not text
               if  (button1.getIcon() == null) {
                   File selectedFile = file.getSelectedFile();
                   String path = selectedFile.getAbsolutePath();
                   button1.setIcon(ResizeImage(path));   
               }
               else {
                    if  ((button1.getIcon() != null) && (button3.getIcon()) == null){
                        File selectedFile = file.getSelectedFile();
                        String path = selectedFile.getAbsolutePath();
                        button2.setIcon(ResizeImage(path));   
                        }



                        if  ((button1.getIcon() != null) && (button2.getIcon()) != null){
                            File selectedFile = file.getSelectedFile();
                            String path = selectedFile.getAbsolutePath();
                            button3.setIcon(ResizeImage(path));  



               }
               }



           }


           else if(result == JFileChooser.CANCEL_OPTION){
               System.out.println("No File Select");
           }
         }
     });

2 个答案:

答案 0 :(得分:0)

尝试一下:

importBtn.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {


           JFileChooser file = new JFileChooser();
           file.setCurrentDirectory(new File(System.getProperty("user.home")));
           //filter the files
           FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpg","gif","png");
           file.addChoosableFileFilter(filter);
           int result = file.showSaveDialog(null);


           if(result == JFileChooser.APPROVE_OPTION){

               //NotWorking make check null not text
               if  (button1.getIcon() == null) {
                   File selectedFile = file.getSelectedFile();
                   String path = selectedFile.getAbsolutePath();
                   button1.setIcon(ResizeImage(path));   
               }
               else {
                    if  (button2.getIcon() == null){
                        File selectedFile = file.getSelectedFile();
                        String path = selectedFile.getAbsolutePath();
                        button2.setIcon(ResizeImage(path));   
                        } else{
                           if  (button3.getIcon() == null){
                               File selectedFile = file.getSelectedFile();
                               String path = selectedFile.getAbsolutePath();
                               button3.setIcon(ResizeImage(path));  

                           }

                        }
               }



           }


           else if(result == JFileChooser.CANCEL_OPTION){
               System.out.println("No File Select");
           }
         }
     });

答案 1 :(得分:0)

您可以尝试一下。

importBtn.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {


      JFileChooser file = new JFileChooser();
      file.setCurrentDirectory(new File(System.getProperty("user.home")));
      //filter the files
      FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpg","gif","png");
      file.addChoosableFileFilter(filter);
      int result = file.showSaveDialog(null);


      if(result == JFileChooser.APPROVE_OPTION){

          File selectedFile = file.getSelectedFile();
          String path = selectedFile.getAbsolutePath();
          //NotWorking make check null not text
          if  (button1.getIcon() == null) 
          {
              button1.setIcon(ResizeImage(path));   
          }
          else if (button2.getIcon() == null)
          {
              button2.setIcon(ResizeImage(path));
          } 
          else if (button3.getIcon() == null)
          {
              button3.setIcon(ResizeImage(path));  
          }
          else 
          {
              //image set on all three
          }
      }
      else if(result == JFileChooser.CANCEL_OPTION){
          System.out.println("No File Select");
      }
    }
});

记住要始终保持简单。