Sco*_*n83 4 java swing for-loop symbols
有人能告诉我我的错误for loop吗?我花了很多时间试错,并在互联网上进行研究,但我仍然无法找到解决方案.它继续说预期的标识符找不到符号.请帮忙谢谢!
package imageviewer;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ImageViewer extends JPanel implements ActionListener {
JPanel bottomPanel;
JLabel imageLabel;
JButton previousBtn;
JButton nxtBtn;
ImageViewer()
{
imageLabel = new JLabel("Image Display");
imageLabel.setBackground(Color.WHITE);
imageLabel.setOpaque(true);
add(imageLabel);
previousBtn = new JButton("Previous");
previousBtn.addActionListener(this);
add(previousBtn);
nxtBtn = new JButton("Next");
nxtBtn.addActionListener(this);
add(nxtBtn);
}
String userInput = JOptionPane.showInputDialog("Please input name of first image file");
Scanner myScanner = new Scanner(userInput);
String fileDetail = myScanner.next();
String[] arr = fileDetail.split("\\.");
String fileName = arr[0].replaceAll("[0-9.]", "");
String fileNumber = arr[0].replaceAll("[^0-9]", "");
String fileFormat = arr[1];
int num = Integer.parseInt(fileNumber);
String totalImage = JOptionPane.showInputDialog("Please enter the number of images in total");
Scanner secondScanner = new Scanner(totalImage);
int numberInput = secondScanner.nextInt();
ImageIcon imageGraphic = new ImageIcon(fileName + fileNumber + "." + fileFormat);
Vector <String> imageDetail = new Vector <String>();
int total = (num + numberInput);
for(int i = num; i < total; i++)
{
}
public void actionPerformed(ActionEvent e)
{
}
}
Run Code Online (Sandbox Code Playgroud)
它的错误在于它是在开放空间中声明的,在任何方法,构造函数,静态块等之外,并且你在这个区域中根本不能有for循环或任何类似的控制结构.解决方案:将它放在它所属的位置,无论是方法还是构造函数,都是您的选择.
实际上,你的很多行看起来都不属于"开放国家",包括调用JOptionPane的行,以及使用它下面的用户输入的行,我想知道其中一些行属于在这堂课里面.课程应该只关注一个单一的责任 - 在这里查看图像,就是这样.你应该重新考虑你的程序设计.
例如,您似乎希望此类显示图像集合,这就是它应该做的事情.如果它是我的类,我会给它一些方法,允许其他类给它一个ImageIcons列表,然后所有这个类在按钮按下滚动图像.任何询问用户从哪里开始寻找图像的代码都应该在其他地方,而不是在这个类中.
| 归档时间: |
|
| 查看次数: |
168 次 |
| 最近记录: |