我仍然在完成我的图像查看器应用程序,我已经到达了部分,图像的文件名将存储在Vector
中。例如,打印到控制台时Vector
的第一个元素是 image1.gif ,我明白要将ImageIcon
添加到JLabel
,我需要为参数插入说" IMAGES / image1.gif" 。但是我怎么会这样做,因为" 通常与我们的论点相关联?
另一件事是我被认为是编程的新手,我有时会迷惑自己或忘记我学到的一些基础知识,所以我想知道是否每个人都可以给我一些关于编码的指导?另外,到目前为止,我已经想到了任何私有方法,所以你们可以给我一些建议吗?
提前致谢!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class ImageViewer extends JPanel implements ActionListener {
//Instance variables
JPanel bottomPanel;
JLabel imageLabel;
JButton previousBtn;
JButton nxtBtn;
//Constructor
ImageViewer()
{
previousBtn = new JButton("Previous");
previousBtn.addActionListener(this);
add(previousBtn);
nxtBtn = new JButton("Next");
nxtBtn.addActionListener(this);
add(nxtBtn);
}
public void actionPerformed(ActionEvent e)
{
}
}
我的驱动程序类
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class TestImageViewer {
public static void main(String [] args)
{
//First input dialog. Example from user will be like: image1.gif
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 letter, number, ext;
letter = arr[0].replaceAll("[0-9.]", "");
number = arr[0].replaceAll("[^0-9.]", "");
ext = arr[1];
int num = Integer.parseInt(number);
System.out.println(letter);
System.out.println(number);
System.out.println(ext);
//Second input dialog
String imageNumber = JOptionPane.showInputDialog("Please enter the number of images");
Scanner secondScanner = new Scanner(imageNumber);
int numberInput = secondScanner.nextInt();
Vector <String> imageDetails = new Vector<String>();
int total = (num + numberInput);
for(int i = num; i <= total; i++)
{
imageDetails.add(letter + i + "." + ext);
}
for(String s: imageDetails)
{
System.out.println(s);
}
//JLabel for displaying the images
JLabel imageLabel = new JLabel(); //<--------This is the part which I'm stuck
imageLabel.setBackground(Color.WHITE);
imageLabel.setOpaque(true);
JFrame imageFrame = new JFrame("Image Viewer");
ImageViewer imagePanel = new ImageViewer();
imagePanel.add(imageLabel);
imageFrame.add(imagePanel);
imageFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
imageFrame.setVisible(true);
imageFrame.pack();
}
}