【问题标题】:nested if statement inside another nested if statement not catch嵌套 if 语句在另一个嵌套 if 语句中未捕获
【发布时间】:2020-06-09 04:02:42
【问题描述】:

我下面的 java 代码具有 3 个标签,分别称为 button1、button2 和 button3。当用户导入图像时,当且仅当 button1 上没有图像时,它应该首先转到 button1。然后下一次导入图像时,当且仅当 button1 上有图像时,它应该转到按钮。当且仅当 button1 和 button 上有图像时,第三次图像应该放在 button3 上。该代码适用于 button1 和 button2,但不适用于 button3。因此,从 else{ 开始的嵌套 if 语句存在问题。

  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");
           }
         }
     });

【问题讨论】:

    标签: java if-statement null nested-if


    【解决方案1】:

    你可以试试这个。

    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");
          }
        }
    });
    

    记住要始终保持简单。

    【讨论】:

      【解决方案2】:

      试试这个:

      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");
                 }
               }
           });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-01
        • 2016-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多