Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
If/Else语句-子句不执行Java_Java_If Statement - Fatal编程技术网

If/Else语句-子句不执行Java

If/Else语句-子句不执行Java,java,if-statement,Java,If Statement,我正在编写实现ActionListener的代码,因此在我的actionPerformed()函数(这在实现ActionListener时是必需的)中,我在每个if/else子句中使用了event.getSource(),以查找触发了哪个JComponent(因为我的代码中有多个JComponent)。但由于某些原因,只有第一个if语句是由它的JComponent触发的。下面是代码。提前感谢您的帮助 这是我的问题重点关注的代码: public void actionPerformed(Acti

我正在编写实现
ActionListener
的代码,因此在我的
actionPerformed()
函数(这在实现
ActionListener
时是必需的)中,我在每个if/else子句中使用了
event.getSource()
,以查找触发了哪个
JComponent
(因为我的代码中有多个
JComponent
)。但由于某些原因,只有第一个
if
语句是由它的
JComponent
触发的。下面是代码。提前感谢您的帮助


这是我的问题重点关注的代码:

public void actionPerformed(ActionEvent event) {

    if (event.getSource() == newProj || event.getSource() == newFlick) {

        String nameProj = JOptionPane.showInputDialog("Name of your Flick:");

        int option = 0;
        JFileChooser dirChooser = new JFileChooser();

        if (nameProj != null) {

            dirChooser.setCurrentDirectory(new File("~"));
            dirChooser.setDialogTitle("Choose Directory");
            dirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            dirChooser.setAcceptAllFileFilterUsed(false);
            option = dirChooser.showOpenDialog(this);

        }

        if (option == JFileChooser.APPROVE_OPTION) {    

            File dir = dirChooser.getSelectedFile();
            String dirPath = dir.getAbsolutePath();
            String newDirPath = dirPath + "\\" + nameProj;
            new File(newDirPath).mkdir();

            File config = null;

            try {

                config = new File(newDirPath + "\\.flick.flick");

                if (!config.exists()) {

                    config.createNewFile();             
                }

                PrintWriter writer = new PrintWriter(config);
                writer.append("*WARNING - TAMPERING WITH THIS DATA COULD LEAD TO PROBLEMS IN YOUR FLICK.* \n");
                writer.append("Project Name: " + nameProj + "\n");      
                writer.close();

            } catch (FileNotFoundException e) {

                JOptionPane.showMessageDialog(null, "Error: " + e, null, JOptionPane.ERROR_MESSAGE);

            } catch (UnsupportedEncodingException e) {

                JOptionPane.showMessageDialog(null, "Error: " + e, null, JOptionPane.ERROR_MESSAGE);

            } catch (IOException e) {

                JOptionPane.showMessageDialog(null, "Error: " + e, null, JOptionPane.ERROR_MESSAGE);

            }


            dispose();
            new Flick(nameProj);

        } else if (event.getSource() == loadProj || event.getSource() == loadFlick) {

            JOptionPane.showMessageDialog(null, "Loaded");

            option = 0;
            dirChooser = new JFileChooser();


            dirChooser.setCurrentDirectory(new File("~"));
            dirChooser.setDialogTitle("Load Flick Directory");
            dirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            dirChooser.setAcceptAllFileFilterUsed(false);
            option = dirChooser.showOpenDialog(this);

            if (option == JFileChooser.APPROVE_OPTION) {    

                File dir = dirChooser.getSelectedFile();
                File config = new File(dir + "\\.flick");

                if (config.exists()) {

                    dispose();
                    new Flick(config.getName());

                } else {

                    JOptionPane.showMessageDialog(null, "Error: Not A Flick Directory", null, JOptionPane.ERROR_MESSAGE);
                }

            }

        }

     }

  }
}

以下是前一个代码之前的其余代码:

package com.shreypandya.flickstudios;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Home extends JFrame implements ActionListener {


private static final long serialVersionUID = 1L;

JLabel title = new JLabel("Welcome to FlickStudios!");
MenuBar menuBar = new MenuBar();
Menu file = new Menu("File");
MenuItem newProj = new MenuItem("New Flick");
MenuItem loadProj = new MenuItem("Load Flick");
JButton newFlick = new JButton("New Flick");
JButton loadFlick = new JButton("Load Flick");
Panel panel = new Panel();

public Home() throws FontFormatException, IOException {

    super("FlickStudios");

    InputStream myStream = new BufferedInputStream(new FileInputStream("Resources/OpenSans.ttf"));
    Font ttf = Font.createFont(Font.TRUETYPE_FONT, myStream);
    Font openSans = ttf.deriveFont(Font.PLAIN, 16);

    add(panel);
    panel.setLayout(new BorderLayout());

    panel.add(title, BorderLayout.PAGE_START);
    title.setFont(openSans.deriveFont(Font.PLAIN, 36));

    panel.add(newFlick, BorderLayout.LINE_START);
    newFlick.setFont(openSans);
    newFlick.setFocusPainted(false);
    newFlick.addActionListener(this);

    panel.add(loadFlick, BorderLayout.LINE_END);
    loadFlick.setFont(openSans);
    loadFlick.setFocusPainted(false);
    loadFlick.addActionListener(this);

    setMenuBar(menuBar);
    menuBar.add(file);

    file.add(newProj);
    newProj.addActionListener(this);
    newProj.setShortcut(new MenuShortcut(KeyEvent.VK_N, false));

    file.add(loadProj);
    loadProj.addActionListener(this);
    loadProj.setShortcut(new MenuShortcut(KeyEvent.VK_L, false));

    setExtendedState(JFrame.MAXIMIZED_BOTH);
    setVisible(true);
    setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);

    }

你的牙套不匹配

如果(event.getSource()==newProj | | event.getSource()==newFlick){在以下情况下才会结束:

                 } else {

                    JOptionPane.showMessageDialog(null, "Error: Not A Flick Directory", null, JOptionPane.ERROR_MESSAGE);
                }

            }

        }

     }//HERE

因此,如果第一个if语句失败,您的函数将结束,并且不会检查其他条件。

第一个if封装了您的整个函数:

public void actionPerformed(ActionEvent event) {    
   if (event.getSource() == newProj || event.getSource() == newFlick) {    
      if (nameProj != null) {
      }    
      if (option == JFileChooser.APPROVE_OPTION) {            
      } else if (event.getSource() == loadProj || event.getSource() == loadFlick) {    
         if (option == JFileChooser.APPROVE_OPTION) {        
            if (config.exists()) {        
            } else {    
            }    
         }    
      }    
   }
}

我不认为这是你想要的,并且肯定会解释为什么当第一个if计算为false时没有到达内部块。

检查你是如何嵌套if块的。看起来你嵌套的else块太高了,这就解释了为什么从来没有调用它们。这是缩进有帮助的地方,也是重构的地方(将大块代码提取到方法中)有帮助。谢谢,只是一个小错误:)一个小错误把事情搞砸了大:)