我正在编写实现ActionListener
的代码,所以在我的actionPerformed()
函数中(实现ActionListener
时这是必需的)我在每个if event.getSource()
中都使用了JComponent
/ else子句用于查找触发了哪个JComponents
(因为我的代码中有多个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);
}
以下是前一段代码之前的其余代码:
{{1}}
答案 0 :(得分:1)
你的牙套不匹配。
此if (event.getSource() == newProj || event.getSource() == newFlick) {
直到:
} else {
JOptionPane.showMessageDialog(null, "Error: Not A Flick Directory", null, JOptionPane.ERROR_MESSAGE);
}
}
}
}//HERE
因此,如果第一个if语句失败,则函数结束,并且永远不会检查其他条件。
答案 1 :(得分:1)
第一个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时没有到达内部块。