我下面的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");
           }
         }
     });

最佳答案

你可以试试看

记住要始终保持简单。

关于java - 嵌套在另一个嵌套的if语句中的if语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62274824/

10-11 08:46