[英]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). 我正在编写实现
ActionListener
代码,所以在我的actionPerformed()
函数中(在实现ActionListener
时是必需的)我在每个if / else子句中使用了event.getSource()
来查找触发了哪个JComponent
(因为我有我的代码中有多个JComponents
)。 But for some reason only the first if
statement is triggered by its JComponent
. 但由于某种原因,只有第一个
if
语句由其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: 这个
if (event.getSource() == newProj || event.getSource() == newFlick) {
直到:
} 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. 因此,如果第一个if语句失败,则您的函数结束,并且永远不会检查其他条件。
The first if encapsulates your entire function: 第一个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 {
}
}
}
}
}
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. 我不认为这是你想要的,并且肯定会解释为什么当第一个if评估为false时没有到达内部块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.