I need to help figuring out this code, I want to make an array of images appear randomly by a button click, and another image appear with another button click.
But there is an error, I can't figure it out:
package m03Test;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Font;
import java.util.Random;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class M03 extends JFrame {
private ImageIcon Light1 = new ImageIcon(M03.class.getResource("/resources/Planet1.png"));
private ImageIcon Light2 = new ImageIcon(M03.class.getResource("/resources/Planet2.png"));
private ImageIcon Light3 = new ImageIcon(M03.class.getResource("/resources/Planet3.png"));
private ImageIcon Light4 = new ImageIcon(M03.class.getResource("/resources/Planet4.png"));
private ImageIcon Light5 = new ImageIcon(M03.class.getResource("/resources/Planet5.png"));
private ImageIcon Light6 = new ImageIcon(M03.class.getResource("/resources/Planet6.png"));
private ImageIcon Light7 = new ImageIcon(M03.class.getResource("/resources/Planet7.png"));
private ImageIcon Light8 = new ImageIcon(M03.class.getResource("/resources/Planet8.png"));
private ImageIcon Light9 = new ImageIcon(M03.class.getResource("/resources/Planet9.png"));
private JLabel lblDisplay;
private final Random rand = new Random();
int spread = rand.nextInt(9);
int vertify = 0;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
M03 frame = new M03();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public M03() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel ImagePanel = new JPanel();
ImagePanel.setBackground(new Color(0, 0, 139));
contentPane.add(ImagePanel, BorderLayout.SOUTH);
JButton ResetButton = createResetButton();
ImagePanel.add(ResetButton);
JButton RandomButton = createRandomButton();
ImagePanel.add(RandomButton);
JLabel lblTitle = createlblTitle();
contentPane.add(lblTitle, BorderLayout.NORTH);
lblDisplay = satellite();
JLabel lblDisplay = satellite();
contentPane.add(lblDisplay, BorderLayout.CENTER);
}
private JButton createRandomButton() {
JButton RandomButton = new JButton("Random");
RandomButton.setBackground(new Color(0, 191, 255));
ImageIcon[] Satellite = { Light1, Light2, Light3, Light4, Light5, Light6, Light7, Light8, Light9};
RandomButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do
{
spread = rand.nextInt(9);
} while (spread == vertify);
vertify = spread;
lblDisplay.setIcon(Satellite[vertify]);
}
});
return RandomButton;
}
private JLabel satellite() {
JLabel lblDisplay = new JLabel("");
lblDisplay.setIcon(Satellite [spread]);
lblDisplay.setHorizontalAlignment(SwingConstants.CENTER);
return lblDisplay;
}
private JButton createResetButton() {
ImageIcon Starlight = new ImageIcon(M03.class.getResource("/resources/Solar.jpeg"));
JButton ResetButton = new JButton("Solar Reset");
ResetButton.setBackground(new Color(0, 191, 255));
ResetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ResetButton.setIcon(Starlight);
}
});
return ResetButton;
}
private JLabel createlblTitle() {
JLabel lblTitle = new JLabel("New label");
lblTitle.setForeground(new Color(255, 255, 0));
lblTitle.setBackground(new Color(0, 0, 0));
lblTitle.setFont(new Font("Verdana", Font.PLAIN, 12));
lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
lblTitle.setOpaque(true);
return lblTitle;
}
}