Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.imageio.ImageIO;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- public class qqq extends JFrame {
- static JFrame jFrame = getFrame();
- static JPanel jPanel = new JPanel();
- public static void main(String[] args) throws IOException {
- jFrame.add(jPanel);
- BufferedImage myPicture_1 = ImageIO.read(new File("D:\\new\\1.png")); // ввести шлях до зображення 1
- BufferedImage myPicture_2 = ImageIO.read(new File("D:\\new\\2.jpg")); // ввести шлях до зображення 2
- JLabel picLabel = new JLabel(new ImageIcon(myPicture_1));
- final boolean[] counter = {true};
- picLabel.addMouseListener(new MouseAdapter()
- {
- public void mouseClicked(MouseEvent e)
- {
- if (counter[0]) {
- picLabel.setIcon(new ImageIcon(myPicture_2));
- counter[0] = false;
- }
- else {
- picLabel.setIcon(new ImageIcon(myPicture_1));
- counter[0] = true;
- }
- }
- });
- jPanel.add(picLabel);
- jPanel.revalidate();
- }
- static JFrame getFrame() {
- JFrame jFrame = new JFrame() {};
- jFrame.setVisible(true);
- jFrame.setResizable(false);
- Toolkit toolkit = Toolkit.getDefaultToolkit();
- Dimension dimension = toolkit.getScreenSize();
- jFrame.setBounds(dimension.width/2 - 300, dimension.height/2 - 200, 600, 400);
- jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- return jFrame;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement