Home > OS >  the label do the same thing once the i closed the frame and open it the label do twice then three ti
the label do the same thing once the i closed the frame and open it the label do twice then three ti

Time:03-12

when i open f7 [JFrame] it's do its jop ,but when i close and open the same frame It does the same jop ,but twice and when i open it and close it ,again it does its jop ,but three times how can i fix this probleme

This is the code in a function called "u7" and the variables are out of the function

i called it from the same script that the function in it and the main frame in it


ImageIcon les7[] = {
               new ImageIcon(getClass().getClassLoader().getResource("U7-1.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-2.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-3.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-4.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-5.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-6.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-7.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-8.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-9.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-10.png"))};
    Image be7 = les7[0].getImage();
    Image in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
    ImageIcon af7 = new ImageIcon(in7);
    
    JFrame f7 = new JFrame();
    JLabel lessons7 = new JLabel();
    
    JLabel bak7 = new JLabel("Back");
    JLabel nxt7 = new JLabel("Next");
    JLabel ext7 = new JLabel("Exit");
    
    boolean b71 = true, b72 = true, b73 = true, b74 = true, b75 = true, b76 = true, b77 = true, b78 = true, b79 = true;
    boolean b71f = true, b72f = true, b73f = true, b74f = true, b75f = true, b76f = true, b77f = true, b78f = true, b79f = true;
    
    boolean su7 = false;

    void u7() {
        
        f7.setTitle("English");
        f7.setSize(600, 800);
        f7.setLocationRelativeTo(null);
        f7.setResizable(false);
        f7.setLayout(null);
        
        lessons7.setBounds(0, 0, 600, 711);
        
        lessons7.setIcon(af7);
        
        nxt7.setBounds(0, 711, 150, 50);
        nxt7.setBackground(new Color(50, 50, 50));
        nxt7.setOpaque(true);
        nxt7.setForeground(Color.white);
        nxt7.setHorizontalAlignment(nxt7.CENTER);
        nxt7.setFont(font);
        nxt7.setBorder(BorderFactory.createLineBorder(Color.black, 5));
        nxt7.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evt) {
                if(be7.equals(les7[0].getImage()) && b71) {
                    be7 = les7[1].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b71 = false;
                    b71f = true;
                }
                else if(be7.equals(les7[1].getImage()) && b72) {
                    be7 = les7[2].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b72 = false;
                    b72f = true;
                }
                else if(be7.equals(les7[2].getImage()) && b73) {
                    be7 = les7[3].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b73 = false;
                    b73f = true;
                }
                else if(be7.equals(les7[3].getImage()) && b74) {
                    be7 = les7[4].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b74 = false;
                    b74f = true;
                }
                else if(be7.equals(les7[4].getImage()) && b75) {
                    be7 = les7[5].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b75 = false;
                    b75f = true;
                }
                else if(be7.equals(les7[5].getImage()) && b76) {
                    be7 = les7[6].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b76 = false;
                    b76f = true;
                }
                else if(be7.equals(les7[6].getImage()) && b77) {
                    be7 = les7[7].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b77 = false;
                    b77f = true;
                }
                else if(be7.equals(les7[7].getImage()) && b78) {
                    be7 = les7[8].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b78 = false;
                    b78f = true;
                }
                else if(be7.equals(les7[8].getImage()) && b79) {
                    be7 = les7[9].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b79 = false;
                    b79f = true;
                }
            }
        });
        
        bak7.setBounds(434, 711, 150, 50);
        bak7.setBackground(new Color(50, 50, 50));
        bak7.setOpaque(true);
        bak7.setForeground(Color.white);
        bak7.setHorizontalAlignment(bak7.CENTER);
        bak7.setFont(font);
        bak7.setBorder(BorderFactory.createLineBorder(Color.black, 5));
        bak7.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evt) {
                if(be7.equals(les7[1].getImage()) && b71f) {
                    be7 = les7[0].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b71f = false;
                    b71 = true;
                }
                else if(be7.equals(les7[2].getImage()) && b72f) {
                    be7 = les7[1].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b72f = false;
                    b72 = true;
                }
                else if(be7.equals(les7[3].getImage()) && b73f) {
                    be7 = les7[2].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b73f = false;
                    b73 = true;
                }
                else if(be7.equals(les7[4].getImage()) && b74f) {
                    be7 = les7[3].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b74f = false;
                    b74 = true;
                }
                else if(be7.equals(les7[5].getImage()) && b75f) {
                    be7 = les7[4].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b75f = false;
                    b75 = true;
                }
                else if(be7.equals(les7[6].getImage()) && b76f) {
                    be7 = les7[5].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b76f = false;
                    b76 = true;
                }
                else if(be7.equals(les7[7].getImage()) && b77f) {
                    be7 = les7[6].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b77f = false;
                    b77 = true;
                }
                else if(be7.equals(les7[8].getImage()) && b78f) {
                    be7 = les7[7].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b78f = false;
                    b78 = true;
                }
                else if(be7.equals(les7[9].getImage()) && b79f) {
                    be7 = les7[8].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b79f = false;
                    b79 = true;
                }
            }
        });
        
        ext7.setBounds(220, 711, 150, 50);
        ext7.setBackground(new Color(50, 50, 50));
        ext7.setOpaque(true);
        ext7.setForeground(Color.white);
        ext7.setHorizontalAlignment(ext7.CENTER);
        ext7.setFont(font);
        ext7.setBorder(BorderFactory.createLineBorder(Color.black, 5));
        ext7.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evt) {
                ex7(bak7, ext7, nxt7, 434, 220, 0);
            }
        });
        
        f7.add(lessons7);
        f7.add(bak7);
        f7.add(ext7);
        f7.add(nxt7);
        if(!su7) {
            su7 = true;
            f7.setVisible(true);            
        }
        f7.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent evt) {
                su7 = false;
            }
        });
    }
    void ex7(JLabel b , JLabel e, JLabel n, int bx, int ex, int nx) {
        SwingWorker<Void, Void> w = new SwingWorker<Void, Void>() {
            @Override
            public Void doInBackground() throws Exception{
                int speed1 = 0, speed2 = 0;
                for(int y = 711; y > 600;) {
                    b.setLocation(bx, y);
                    n.setLocation(nx, y);
                    y -= speed1;
                    speed1  = 2;
                    Thread.sleep(10);
                    System.out.println(y);
                }
                Thread.sleep(100);
                for(int y = 711; y < 800;) {
                    e.setLocation(ex, y);
                    y  = speed2;
                    speed2  = 2;
                    Thread.sleep(10);
                }
                Thread.sleep(100);
                f7.setVisible(false);
                su7 = false;
                return null;
                
            }
        };
        w.execute();
    }

All Script Here

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;


public class EnglishFrame extends JFrame{
    JLabel units[] = {new JLabel("Unit 7"), new JLabel("Unit 8"), new JLabel("Unit 9"), new JLabel("Unit 10"), new JLabel("Unit 11"), new JLabel("Unit 12")};
    JFrame Units[] = {new JFrame("Unit 7"), new JFrame("Unit 8"), new JFrame("Unit 9"), new JFrame("Unit 10"), new JFrame("Unit 11"), new JFrame("Unit 12")};
    JLabel LabelIcons[] = {new JLabel(""), new JLabel(""), new JLabel(""), new JLabel(""), new JLabel(""), new JLabel(""), new JLabel("")};
    ImageIcon units_icons[] = {new ImageIcon(getClass().getClassLoader().getResource("Unit7.png")), new ImageIcon(getClass().getClassLoader().getResource("Unit8.png")), new ImageIcon(getClass().getClassLoader().getResource("Unit9.png")), new ImageIcon(getClass().getClassLoader().getResource("Unit10.png")), new ImageIcon(getClass().getClassLoader().getResource("Unit11.png")), new ImageIcon(getClass().getClassLoader().getResource("Unit12.png"))};
    MainFrame mainframe ;
    Font font = new Font("Ink Free", Font.BOLD, 30);
    
    JLabel bak = new JLabel("Back");
    
        
    public EnglishFrame(MainFrame mainframe) {
        this.mainframe = mainframe;
        
        create_Frame();
        create_Units();
        create_Frames();
        create_buttons();
    }
    void create_Frame() {
        setTitle("English");
        setSize(800, 600);
        setLocationRelativeTo(null);
        setResizable(false);
        setLayout(null);
    }
    void create_Frames() {
        int counter = 7;
        for(int count = 0; count < 6; count   ) {
            Units[count].setTitle("Unit "   counter);
            Units[count].setSize(800, 600);
            Units[count].setLocationRelativeTo(null);
            Units[count].setResizable(false);
            Units[count].setLayout(null);
            counter   ;
        }
    }
    void create_Units() {
        int x = 20;
        
        for(int count = 0; count < 5; count   ) {
            units[count].setBounds(x, 40, 100, 30);
            units[count].setFont(font);
            units[count].setForeground(Color.black);
            
            add(units[count]);
            
            x  = 160;
        }
            units[5].setBounds(340, 184, 105, 30);
            units[5].setFont(font);
            units[5].setForeground(Color.black);
            
            add(units[5]);
                    
        units[0].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                LabelIcons[0].setBounds(8, 70, 105, 60);
                
                Image img = units_icons[0].getImage();
                Image imgscale = img.getScaledInstance(LabelIcons[0].getWidth(), LabelIcons[0].getHeight(), Image.SCALE_SMOOTH);
                ImageIcon icon = new ImageIcon(imgscale);
                LabelIcons[0].setIcon(icon);
                
            }
            @Override
            public void mouseExited(MouseEvent e) {
//              LabelIcons[0].setBounds(0, 0, 0, 0);
                Animations.fa(LabelIcons, 0, 8, 70);
                
            }
            @Override
            public void mouseClicked(MouseEvent e) {
                u7();
            }
        });
        units[1].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                LabelIcons[1].setBounds(170, 70, 105, 60);
                
                Image img = units_icons[1].getImage();
                Image imgscale = img.getScaledInstance(LabelIcons[1].getWidth(), LabelIcons[1].getHeight(), Image.SCALE_SMOOTH);
                ImageIcon icon = new ImageIcon(imgscale);
                LabelIcons[1].setIcon(icon);
                
            }
            @Override
            public void mouseExited(MouseEvent e) {
            
                Animations.fa(LabelIcons, 1, 170, 70);
                
            }
            @Override
            public void mouseClicked(MouseEvent e) {
//              eng.create_Frame();
                JOptionPane.showMessageDialog(null, "Sorry There Wasn't Any Time To Make This", "U9", JOptionPane.CLOSED_OPTION);
            }
        });
        units[2].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                LabelIcons[2].setBounds(330, 70, 105, 60);
                
                Image img = units_icons[2].getImage();
                Image imgscale = img.getScaledInstance(LabelIcons[2].getWidth(), LabelIcons[2].getHeight(), Image.SCALE_SMOOTH);
                ImageIcon icon = new ImageIcon(imgscale);
                LabelIcons[2].setIcon(icon);
                
            }
            @Override
            public void mouseExited(MouseEvent e) {
                Animations.fa(LabelIcons, 2, 330, 70);
                
            }
            @Override
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(null, "Sorry There Wasn't Any Time To Make This", "U9", JOptionPane.CLOSED_OPTION);
            }
        });
        units[3].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                LabelIcons[3].setBounds(497, 70, 105, 60);
                
                Image img = units_icons[3].getImage();
                Image imgscale = img.getScaledInstance(LabelIcons[3].getWidth(), LabelIcons[3].getHeight(), Image.SCALE_SMOOTH);
                ImageIcon icon = new ImageIcon(imgscale);
                LabelIcons[3].setIcon(icon);
                
            }
            @Override
            public void mouseExited(MouseEvent e) {
//              LabelIcons[3].setBounds(0, 0, 0, 0);
                Animations.fa(LabelIcons, 3, 497, 70);
                
            }
            @Override
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(null, "Sorry There Wasn't Any Time To Make This", "U10", JOptionPane.CLOSED_OPTION);
            }
        });
        units[4].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                LabelIcons[4].setBounds(654, 70, 105, 60);
                
                Image img = units_icons[4].getImage();
                Image imgscale = img.getScaledInstance(LabelIcons[4].getWidth(), LabelIcons[4].getHeight(), Image.SCALE_SMOOTH);
                ImageIcon icon = new ImageIcon(imgscale);
                LabelIcons[4].setIcon(icon);
                
            }
            @Override
            public void mouseExited(MouseEvent e) {
//              LabelIcons[4].setBounds(0, 0, 0, 0);
                Animations.fa(LabelIcons, 4, 654, 70);
                
            }
            @Override
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(null, "Sorry There Wasn't Any Time To Make This", "U11", JOptionPane.CLOSED_OPTION);
            }
        });
        units[5].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                LabelIcons[5].setBounds(335, 214, 105, 60);
                
                Image img = units_icons[5].getImage();
                Image imgscale = img.getScaledInstance(LabelIcons[5].getWidth(), LabelIcons[5].getHeight(), Image.SCALE_SMOOTH);
                ImageIcon icon = new ImageIcon(imgscale);
                LabelIcons[5].setIcon(icon);
                
            }
            @Override
            public void mouseExited(MouseEvent e) {
//              LabelIcons[5].setBounds(0, 0, 0, 0);
                Animations.fa(LabelIcons, 5, 335, 214);
                
            }
            @Override
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(null, "Sorry There Wasn't Any Time To Make This", "U12", JOptionPane.CLOSED_OPTION);
            }
        });
        
        add(LabelIcons[0]);
        add(LabelIcons[1]);
        add(LabelIcons[2]);
        add(LabelIcons[3]);
        add(LabelIcons[4]);
        add(LabelIcons[5]);
    }
    void create_buttons() {
        bak.setBounds(620, 500, 150, 50);
        bak.setBackground(new Color(50, 50, 50));
        bak.setOpaque(true);
        bak.setForeground(Color.white);
        bak.setHorizontalAlignment(bak.CENTER);
        bak.setFont(font);
        bak.setBorder(BorderFactory.createLineBorder(Color.black, 5));
        bak.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evt) {
                dispose();
            }
        });
        
        add(bak);
    }
    ImageIcon les7[] = {
               new ImageIcon(getClass().getClassLoader().getResource("U7-1.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-2.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-3.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-4.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-5.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-6.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-7.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-8.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-9.png")),
               new ImageIcon(getClass().getClassLoader().getResource("U7-10.png"))};
    Image be7 = les7[0].getImage();
    Image in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
    ImageIcon af7 = new ImageIcon(in7);
    
    JFrame f7 = new JFrame();
    JLabel lessons7 = new JLabel();
    
    JLabel bak7 = new JLabel("Back");
    JLabel nxt7 = new JLabel("Next");
    JLabel ext7 = new JLabel("Exit");
    
    boolean b71 = true, b72 = true, b73 = true, b74 = true, b75 = true, b76 = true, b77 = true, b78 = true, b79 = true;
    boolean b71f = true, b72f = true, b73f = true, b74f = true, b75f = true, b76f = true, b77f = true, b78f = true, b79f = true;
    
    boolean su7 = false;
    
    void u7() {
        
        f7.setTitle("English");
        f7.setSize(600, 800);
        f7.setLocationRelativeTo(null);
        f7.setResizable(false);
        f7.setLayout(null);
        
        lessons7.setBounds(0, 0, 600, 711);
        
        lessons7.setIcon(af7);
        
        nxt7.setBounds(0, 711, 150, 50);
        nxt7.setBackground(new Color(50, 50, 50));
        nxt7.setOpaque(true);
        nxt7.setForeground(Color.white);
        nxt7.setHorizontalAlignment(nxt7.CENTER);
        nxt7.setFont(font);
        nxt7.setBorder(BorderFactory.createLineBorder(Color.black, 5));
        nxt7.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evt) {
                if(be7.equals(les7[0].getImage()) && b71) {
                    be7 = les7[1].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b71 = false;
                    b71f = true;
                }
                else if(be7.equals(les7[1].getImage()) && b72) {
                    be7 = les7[2].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b72 = false;
                    b72f = true;
                }
                else if(be7.equals(les7[2].getImage()) && b73) {
                    be7 = les7[3].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b73 = false;
                    b73f = true;
                }
                else if(be7.equals(les7[3].getImage()) && b74) {
                    be7 = les7[4].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b74 = false;
                    b74f = true;
                }
                else if(be7.equals(les7[4].getImage()) && b75) {
                    be7 = les7[5].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b75 = false;
                    b75f = true;
                }
                else if(be7.equals(les7[5].getImage()) && b76) {
                    be7 = les7[6].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b76 = false;
                    b76f = true;
                }
                else if(be7.equals(les7[6].getImage()) && b77) {
                    be7 = les7[7].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b77 = false;
                    b77f = true;
                }
                else if(be7.equals(les7[7].getImage()) && b78) {
                    be7 = les7[8].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b78 = false;
                    b78f = true;
                }
                else if(be7.equals(les7[8].getImage()) && b79) {
                    be7 = les7[9].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b79 = false;
                    b79f = true;
                }
            }
        });
        
        bak7.setBounds(434, 711, 150, 50);
        bak7.setBackground(new Color(50, 50, 50));
        bak7.setOpaque(true);
        bak7.setForeground(Color.white);
        bak7.setHorizontalAlignment(bak7.CENTER);
        bak7.setFont(font);
        bak7.setBorder(BorderFactory.createLineBorder(Color.black, 5));
        bak7.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evt) {
                if(be7.equals(les7[1].getImage()) && b71f) {
                    be7 = les7[0].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b71f = false;
                    b71 = true;
                }
                else if(be7.equals(les7[2].getImage()) && b72f) {
                    be7 = les7[1].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b72f = false;
                    b72 = true;
                }
                else if(be7.equals(les7[3].getImage()) && b73f) {
                    be7 = les7[2].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b73f = false;
                    b73 = true;
                }
                else if(be7.equals(les7[4].getImage()) && b74f) {
                    be7 = les7[3].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b74f = false;
                    b74 = true;
                }
                else if(be7.equals(les7[5].getImage()) && b75f) {
                    be7 = les7[4].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b75f = false;
                    b75 = true;
                }
                else if(be7.equals(les7[6].getImage()) && b76f) {
                    be7 = les7[5].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b76f = false;
                    b76 = true;
                }
                else if(be7.equals(les7[7].getImage()) && b77f) {
                    be7 = les7[6].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b77f = false;
                    b77 = true;
                }
                else if(be7.equals(les7[8].getImage()) && b78f) {
                    be7 = les7[7].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b78f = false;
                    b78 = true;
                }
                else if(be7.equals(les7[9].getImage()) && b79f) {
                    be7 = les7[8].getImage();
                    in7 = be7.getScaledInstance(600, 711, be7.SCALE_SMOOTH);
                    af7 = new ImageIcon(in7);
                    lessons7.setIcon(af7);
                    System.out.println("Why");
                    b79f = false;
                    b79 = true;
                }
            }
        });
        
        ext7.setBounds(220, 711, 150, 50);
        ext7.setBackground(new Color(50, 50, 50));
        ext7.setOpaque(true);
        ext7.setForeground(Color.white);
        ext7.setHorizontalAlignment(ext7.CENTER);
        ext7.setFont(font);
        ext7.setBorder(BorderFactory.createLineBorder(Color.black, 5));
        ext7.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evt) {
                ex7(bak7, ext7, nxt7, 434, 220, 0);
            }
        });
        
        f7.add(lessons7);
        f7.add(bak7);
        f7.add(ext7);
        f7.add(nxt7);
        if(!su7) {
            su7 = true;
            f7.setVisible(true);            
        }
        f7.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent evt) {
                su7 = false;
            }
        });
    }
    void ex7(JLabel b , JLabel e, JLabel n, int bx, int ex, int nx) {
        SwingWorker<Void, Void> w = new SwingWorker<Void, Void>() {
            @Override
            public Void doInBackground() throws Exception{
                int speed1 = 0, speed2 = 0;
                for(int y = 711; y > 600;) {
                    b.setLocation(bx, y);
                    n.setLocation(nx, y);
                    y -= speed1;
                    speed1  = 2;
                    Thread.sleep(10);
                    System.out.println(y);
                }
                Thread.sleep(100);
                for(int y = 711; y < 800;) {
                    e.setLocation(ex, y);
                    y  = speed2;
                    speed2  = 2;
                    Thread.sleep(10);
                }
                Thread.sleep(100);
                f7.setVisible(false);
                su7 = false;
                return null;
                
            }
        };
        w.execute();
    }
}
    ```

CodePudding user response:

You're adding a bunch of listeners in the u7() method, and each time this method is called, you re-add the same listeners to the same GUI components, so that they get added twice, and then the next time the method is called, they are again re-added, now 3 times... and so on...

So when a component event is activated, the event can potentially trigger multiple iterations of the listeners.

The solution is to add the listeners only once, likely in a location that is called on that instance only once, such as its constructor, and not in a public method that can and likely will be called multiple times.


Side note 1:
Your variable and method names are quite cryptic, making it hard for others (and quite possibly, your future self) from understanding what these components are doing. Best to give your fields, methods and classes descriptive names that will make your code self-commenting and much easier to understand. Also, you will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.


Side note 2:
You have some variables whose names end with a number or single letter, suggesting that perhaps you want to use an array or ArrayList to replace that variable and its brethren and to reduce needless code redundancy. Also, by using layout managers you could reduce even more needless code redundancy, making your code much easier to debug and to enhance.


Side note 3:
You should avoid use of null layout and use of setBounds(...) for component placement as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain.

  • Related