Здравствуйте, спасибо за вашу помощь заранее.Java: чтение массива каталога для отображения изображений, содержащихся в каталоге, чтобы иметь возможность перемещаться вперед и назад по массиву
Моя цель - загрузить каталог с именем 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--;
}
}
}
Вы отправили много кода и указали очень широкую цель, но Stack Overflow действительно подходит для * конкретных * вопросов. Какой * конкретный * аспект вашей задачи окажется для вас проблемой? –
Где я пошла не так, чтобы фотографии появились. Все, что я получаю, это мои выходные сообщения в терминале, когда я запускаю это. Я не вижу, что я сделал неправильно, чтобы загрузить его, независимо от того, какие изображения загружаются массивом из ресурса каталога. – Brokeandstealing