嵌套的 if 语句在另一个嵌套的 if 语句中,未捕获



我下面的java代码具有3个标签,分别称为button1,button2和button3。当用户导入图像时,当且仅当按钮 1 上没有图像时,它应首先转到 button1。然后下次导入图像时,它应该转到按钮,当且仅当按钮 1 上有图像时。第三次将图像放置在按钮 3 上,当且仅当按钮 1 和按钮上有图像时。该代码适用于按钮 1 和按钮 2,但不适用于按钮 3。因此,从 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){
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");
}
}
});

补救措施始终保持简单。

试试这个:

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

相关内容

  • 没有找到相关文章

最新更新