Я создаю слайд-шоу, в котором будет одна кнопка для перехода к следующему изображению, и предполагается, что они будут циклически перемещаться по ним.
Iv попытался переключиться на оператор case и вложенные логические операторы для моего цикла.
/*
*
*/
import java.util.Scanner;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;
public class SlideShow implements ActionListener {
JFrame frame;
JPanel picture;
JLabel label, picture1;
JButton button;
JPanel contentPane;
int x = 0;
public SlideShow() {
/* Create and set up the frame */
frame = new JFrame("SlideShow");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* Create a content pane with a BoxLayout and empty borders */
contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
contentPane.setBackground(Color.white);
/* Create a label that shows a die face */
picture1 = new JLabel(new ImageIcon("attachment_142650738.jpg"));
picture1.setAlignmentX(JLabel.CENTER_ALIGNMENT);
picture1.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
contentPane.add(picture1);
button = new JButton("Next photo");
button.setAlignmentX(JButton.CENTER_ALIGNMENT);
button.addActionListener(this);
contentPane.add(button);
/* Add content pane to frame */
frame.setContentPane(contentPane);
/* Size and then display the frame. */
frame.pack();
frame.setVisible(true);
}
//ActionPerformed//
public void actionPerformed(ActionEvent event) {
String[] array = {
"attachment_142650738.jpg", // 0
"attachment_142650739.jpg", // 1
"attachment_142650741.jpg", // 2
"attachment_142650742.jpg" // 3
};
x += 0;
if (x == 1) {
picture1.setIcon(new ImageIcon("attachment_142650738.jpg"));
} else if (x == 2) {
picture1.setIcon(new ImageIcon("attachment_142650738.jpg"));
} else if (x == 3) {
picture1.setIcon(new ImageIcon("attachment_142650738.jpg"));
} else if (x == 4) {
picture1.setIcon(new ImageIcon("attachment_142650738.jpg"));
} else if (x == 5) {
x = 0;
picture1.setIcon(new ImageIcon("attachment_142650738.jpg"));
}
}
//GUI//
private static void runGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
SlideShow ok = new SlideShow();
}
public static void main(String[] args) {
/* Methods that create and show a GUI should be
run from an event-dispatching thread */
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
runGUI();
}
});
}
}
В данный момент ошибок нет, но у кнопки нет результата.