简体   繁体   中英

If/Else Statement - Clause Not Executing Java

I'm writing code that implements ActionListener , so in my actionPerformed() function (which is necessary when implementing ActionListener ) I have used event.getSource() in each of my if/else clauses to find which JComponent was triggered (because I have multiple JComponents in my code). But for some reason only the first if statement is triggered by its JComponent . Code is below. Thanks in advance for any help.


This is the code my question is focused on:

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

            }

        }

     }

  }
}

Here is the rest of the code that came before the previous code:

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

    }

Your braces are mismatched.

This if (event.getSource() == newProj || event.getSource() == newFlick) { does not end until:

                 } else {

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

            }

        }

     }//HERE

Therefore, if the first if statement fails, your function ends, and never checks the other conditions.

The first if encapsulates your entire function:

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 {    
            }    
         }    
      }    
   }
}

I don't think this is as you intend, and would certainly explain why no inner blocks are being reached when the first if evaluates to false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM