Я пытаюсь импортировать изображения в Intellij Idea.Изображения находятся в файле проекта.
Я пытался читать файлы с помощью сканера.Файл был размещен в том же месте, что и изображения в папке проекта.
Есть ли другое место для размещения изображений?
import javax.swing.*; // JApplet JButton JLabel ImageIcon
import java.awt.*; // Container FlowLayout Color
import java.awt.event.*; // ActionListener ActionEvent
public class ButtonDemo extends JApplet implements ActionListener
{
Container cp;
JLabel sunnyLabel, cloudyLabel;
public void init()
{
cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.setBackground(Color.WHITE);
JButton sunnyButton = new JButton("Sunny");
cp.add(sunnyButton);
sunnyButton.addActionListener(this); ////////////////////////////
JButton cloudyButton = new JButton("Cloudy");
cp.add(cloudyButton);
cloudyButton.addActionListener(this); //////////////////////////
ImageIcon shades = new ImageIcon("shades.png");
sunnyLabel = new JLabel("Take your shades!");
sunnyLabel.setIcon(shades);
cp.add(sunnyLabel);
sunnyLabel.setVisible(false);
ImageIcon cloud = new ImageIcon("cloud.png");
cloudyLabel = new JLabel("It may rain! :-D");
cloudyLabel.setIcon(cloud);
cp.add(cloudyLabel);
cloudyLabel.setVisible(false);
}
public void actionPerformed(ActionEvent e)
{
String ac = e.getActionCommand();
if(ac.equals("Sunny"))
{
cp.setBackground(Color.YELLOW);
sunnyLabel.setVisible(true);
cloudyLabel.setVisible(false);
}
else if(ac.equals("Cloudy"))
{
cp.setBackground(Color.GRAY);
cloudyLabel.setVisible(true);
sunnyLabel.setVisible(false);
}
else
System.out.println("Error");
}
}