嵌套的 if 语句在另一个嵌套的 if 语句中没有捕获
nested if statement inside another nested if statement not catch
我的 java 下面的代码具有 3 个标签,分别称为 button1、button2 和 button3。当用户导入图像时,当且仅当 button1 上没有图像时,它应该首先转到 button1。然后下次导入图像时,当且仅当 button1 上有图像时,它应该转到按钮。当且仅当 button1 和 button 上有图像时,第三次图像才应该放在 button3 上。该代码适用于 button1 和 button2 但不适用于 button3。所以在 else{.
中开始的嵌套 if 语句中存在问题
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");
}
}
});
试试这个:
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 (button2.getIcon() == null){
File selectedFile = file.getSelectedFile();
String path = selectedFile.getAbsolutePath();
button2.setIcon(ResizeImage(path));
} else{
if (button3.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");
}
}
});
你可以试试这个。
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){
File selectedFile = file.getSelectedFile();
String path = selectedFile.getAbsolutePath();
//NotWorking make check null not text
if (button1.getIcon() == null)
{
button1.setIcon(ResizeImage(path));
}
else if (button2.getIcon() == null)
{
button2.setIcon(ResizeImage(path));
}
else if (button3.getIcon() == null)
{
button3.setIcon(ResizeImage(path));
}
else
{
//image set on all three
}
}
else if(result == JFileChooser.CANCEL_OPTION){
System.out.println("No File Select");
}
}
});
记住要始终保持简单。
我的 java 下面的代码具有 3 个标签,分别称为 button1、button2 和 button3。当用户导入图像时,当且仅当 button1 上没有图像时,它应该首先转到 button1。然后下次导入图像时,当且仅当 button1 上有图像时,它应该转到按钮。当且仅当 button1 和 button 上有图像时,第三次图像才应该放在 button3 上。该代码适用于 button1 和 button2 但不适用于 button3。所以在 else{.
中开始的嵌套 if 语句中存在问题 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");
}
}
});
试试这个:
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 (button2.getIcon() == null){
File selectedFile = file.getSelectedFile();
String path = selectedFile.getAbsolutePath();
button2.setIcon(ResizeImage(path));
} else{
if (button3.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");
}
}
});
你可以试试这个。
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){
File selectedFile = file.getSelectedFile();
String path = selectedFile.getAbsolutePath();
//NotWorking make check null not text
if (button1.getIcon() == null)
{
button1.setIcon(ResizeImage(path));
}
else if (button2.getIcon() == null)
{
button2.setIcon(ResizeImage(path));
}
else if (button3.getIcon() == null)
{
button3.setIcon(ResizeImage(path));
}
else
{
//image set on all three
}
}
else if(result == JFileChooser.CANCEL_OPTION){
System.out.println("No File Select");
}
}
});
记住要始终保持简单。