一旦我关闭框架并打开它,标签就会做同样的事情标签会做两次然后三次,很好
the label do the same thing once the i closed the frame and open it the label do twice then three times and well
当我打开 f7 [JFrame] 时,它会自动运行 ,但是 当我关闭并打开同一个框架时 它执行相同的操作,但是两次,当我打开它并关闭它时,它再次执行它的操作,但是三次我该如何解决这个问题
这是函数“u7”中的代码,变量在函数外
我从与其中的函数和其中的主框架相同的脚本调用它
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();
}
此处所有脚本
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();
}
}
```
您在 u7()
方法中添加了一堆侦听器,每次调用此方法时,您 re-add 相同的侦听器相同的 GUI 组件,以便它们被添加两次,然后在下次调用该方法时,它们再次被 re-added,现在是 3 次...等等...
因此当组件事件被激活时,该事件可能会触发监听器的多次迭代。
解决方案是只添加监听器一次,很可能是在只调用该实例一次的位置,例如它的构造函数,而不是在public 可以并且很可能会被多次调用的方法。
旁注 1:
您的变量和方法名称非常隐蔽,使其他人(很可能还有您未来的自己)很难理解这些组件在做什么。最好为您的字段、方法和 classes 提供描述性名称,这将使您的代码 self-commenting 更易于理解。此外,您将想要学习和使用 Java naming conventions。变量名称应全部以小写字母开头,而 class 名称应以大写字母开头。了解这一点并遵循这一点将使我们能够更好地理解您的代码,并使您能够更好地理解其他人的代码。
旁注 2:
您有一些名称以数字或单个字母结尾的变量,这表明您可能想要使用数组或 ArrayList 来替换该变量及其兄弟并减少不必要的代码冗余。此外,通过使用布局管理器,您可以减少更多不必要的代码冗余,使您的代码 更 更易于调试和增强。
旁注 3:
您应该避免使用空布局和使用 setBounds(...)
来放置组件,因为这会导致 GUI 非常不灵活,虽然它们在一个平台上看起来不错,但在大多数其他平台或屏幕分辨率上看起来很糟糕,而且很难更新并保持。
当我打开 f7 [JFrame] 时,它会自动运行 ,但是 当我关闭并打开同一个框架时 它执行相同的操作,但是两次,当我打开它并关闭它时,它再次执行它的操作,但是三次我该如何解决这个问题
这是函数“u7”中的代码,变量在函数外
我从与其中的函数和其中的主框架相同的脚本调用它
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();
}
此处所有脚本
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();
}
}
```
您在 u7()
方法中添加了一堆侦听器,每次调用此方法时,您 re-add 相同的侦听器相同的 GUI 组件,以便它们被添加两次,然后在下次调用该方法时,它们再次被 re-added,现在是 3 次...等等...
因此当组件事件被激活时,该事件可能会触发监听器的多次迭代。
解决方案是只添加监听器一次,很可能是在只调用该实例一次的位置,例如它的构造函数,而不是在public 可以并且很可能会被多次调用的方法。
旁注 1:
您的变量和方法名称非常隐蔽,使其他人(很可能还有您未来的自己)很难理解这些组件在做什么。最好为您的字段、方法和 classes 提供描述性名称,这将使您的代码 self-commenting 更易于理解。此外,您将想要学习和使用 Java naming conventions。变量名称应全部以小写字母开头,而 class 名称应以大写字母开头。了解这一点并遵循这一点将使我们能够更好地理解您的代码,并使您能够更好地理解其他人的代码。
旁注 2:
您有一些名称以数字或单个字母结尾的变量,这表明您可能想要使用数组或 ArrayList 来替换该变量及其兄弟并减少不必要的代码冗余。此外,通过使用布局管理器,您可以减少更多不必要的代码冗余,使您的代码 更 更易于调试和增强。
旁注 3:
您应该避免使用空布局和使用 setBounds(...)
来放置组件,因为这会导致 GUI 非常不灵活,虽然它们在一个平台上看起来不错,但在大多数其他平台或屏幕分辨率上看起来很糟糕,而且很难更新并保持。