[英]Java: Reading an array of a directory to display the images contained in the directory to be able to go forwards and backwards through the array
您好,感謝您的提前協助。
我的目標是加載一個名為resource的目錄以創建一個數組。 這樣做之后,程序應使用“下一個”和“上一個”按鈕通過使用計數器在數組中前后循環。 遍歷數組應該顯示適當的圖片(不知道圖片是什么,只是圖片在數組中的順序)。
到目前為止,我的代碼:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import java.io.IOException;
import javax.swing.ImageIcon;
import java.io.*;
import java.util.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.awt.*;
import java.awt.event.*;
public class viewer extends JFrame implements ActionListener
{
int counter = 0; //Initial counter value
int maxItems = 0; // Set it equal to the length of the array
int minItems = 0; //Minimum items for previous button
JPanel botPanel = null;
JPanel midPanel = null;
JLabel midLabel = null;
ImageIcon image;
String[] picture = new String [1000];
public viewer()
{
setTitle ("Roberts Viewer");
setSize (1000, 1000);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Allow the X button to close the program and not just the frame
JPanel midPanel = new JPanel ();
JPanel botPanel = new JPanel ();
midLabel = new JLabel();
JButton prv = new JButton("Previous"); botPanel.add(prv); prv.addActionListener(this);
JButton nxt = new JButton("Next"); botPanel.add(nxt); nxt.addActionListener(this);
JButton quitButton = new JButton ("Quit Program"); botPanel.add(quitButton); quitButton.addActionListener(this);
add(midPanel, BorderLayout.CENTER);
add(botPanel, BorderLayout.SOUTH);
File dir = new File ("resource");
File[] picture = dir.listFiles();
for (int maxItems = 0;maxItems < picture.length; maxItems++);
midPanel.add(midLabel);
setVisible(true);
}
public static void main(String[] args)
{
viewer v = new viewer();
}
public void actionPerformed (ActionEvent e)
{
String action = e.getActionCommand();
if (action.equals("Quit Program"))
{
System.exit(0);
}
if(action.equals("Next"))
{
if (counter < 0){counter = 0;}
if (counter> maxItems) {counter = 0;}
String pictureString = String.format("resource/%s", picture[counter]);
System.out.printf("Retrieving[%s]\n",pictureString);
try
{
image = new ImageIcon(getClass().getClassLoader().getResource(pictureString));
}
catch (Exception xu)
{
System.out.println("Woops");
}
midLabel.setIcon(image);
counter++;
}
if(action.equals("Previous"))
{
if (counter < 0) {counter = maxItems;}
if (counter < minItems) {counter = maxItems;}
String pictureString = String.format("resource/%s",picture[counter]);
System.out.printf("Retrieving[%s]\n", pictureString);
try
{
image = new ImageIcon(getClass().getClassLoader().getResource(pictureString));
}
catch (Exception xu)
{
System.out.println("Woops");
}
midLabel.setIcon(image);
counter--;
}
}
}
問題解決了。 我有一些錯誤,但這是固定的代碼。 導入javax.swing.JFrame; 導入javax.swing.JPanel; 導入javax.swing.JLabel; 導入javax.swing.JButton; 導入javax.swing.BoxLayout; 導入javax.swing.JTextField;
import java.io.IOException;
import javax.swing.ImageIcon;
import java.io.*;
import java.util.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.awt.*;
import java.awt.event.*;
public class viewer extends JFrame implements ActionListener
{
int counter = 0; //Initial counter value
int maxItems = 0; // Set it equal to the length of the array
int minItems = 0; //Minimum items for previous button
JPanel botPanel = null;
JPanel midPanel = null;
JLabel midLabel = null;
ImageIcon image;
File[] picture;
public viewer()
{
setTitle ("Roberts Viewer");
setSize (500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel midPanel = new JPanel ();
JPanel botPanel = new JPanel ();
midLabel = new JLabel();
JButton prv = new JButton("Previous"); botPanel.add(prv); prv.addActionListener(this);
JButton nxt = new JButton("Next"); botPanel.add(nxt); nxt.addActionListener(this);
JButton quitButton = new JButton ("Quit Program"); botPanel.add(quitButton); quitButton.addActionListener(this);
add(midPanel, BorderLayout.CENTER);
add(botPanel, BorderLayout.SOUTH);
File dir = new File ("resource");
picture = dir.listFiles();
midPanel.add(midLabel);
image = new ImageIcon("resource/"+picture [0].getName());
midLabel.setIcon(image);
setVisible(true);
}
public static void main(String[] args)
{
viewer v = new viewer();
}
public void actionPerformed (ActionEvent e)
{
String action = e.getActionCommand();
if (action.equals("Quit Program"))
{
System.exit(0);
}
if(action.equals("Next"))
{
counter++;
if (counter>= picture.length) {counter = 0;}
try
{
image = new ImageIcon("resource/"+picture[counter].getName());
midLabel.setIcon(image);
}
catch (Exception xu)
{
System.out.println("Woops");
}
System.out.printf("Exit next: " + counter + "\n");
}
if(action.equals("Previous"))
{
counter--;
if (counter < 0) {counter = picture.length-1;}
String.format("resource/%s",picture[counter].getName());
try
{
image = new ImageIcon("resource/"+picture[counter].getName());
midLabel.setIcon(image);
}
catch (Exception xu)
{
System.out.println("Woops");
}
System.out.printf("Exit prev: " + counter + "\n");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.