我正在设计一个JPanel,其中有一些按钮具有自己的动作侦听器,并且在单击时会执行某些操作(例如,其中一个按钮将删除该面板)。如果用户关闭程序,则不应删除面板并应检索面板。为此,我序列化面板并在需要时反序列化它们。但问题是,当检索面板时,诸如动作监听器和鼠标监听器之类的监听器将无法工作。有什么方法可以解决这个问题吗?或者有没有其他选择可以做同样的事情没有问题?
我的面板代码,以防万一:
import jdk.internal.org.objectweb.asm.commons.SerialVersionUIDAdder;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.URL;
import java.text.Format;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Random;
public class Download extends JPanel implements Serializable {
private String fileName;
private boolean removed = false;
private double downloadSpeed;
private double downloadedSize;
private double sizeOfFile;
LocalTime downloadTime;
LocalDate downloadDate;
private boolean checked = false;
private static final long serialVersionUID = 1113799434508676095L;
JLabel test = new JLabel("test");
public Download (MainPage c,URL url,LocalTime time,LocalDate date) {
setLayout(null);
downloadDate = date;
downloadTime = time;
fileName = url.getFile();
JCheckBox checkbox = new JCheckBox();
checkbox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED)
setChecked(true);
else
setChecked(false);
}
});
checkbox.setBackground(new Color(186,212,215));
checkbox.setBounds(95,42,17,15);
add(checkbox);
MouseAdapter a = new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
super.mouseEntered(e);
setBackground(new Color(224,235,252));
checkbox.setBackground(new Color(224,235,252));
}
@Override
public void mouseExited(MouseEvent e) {
super.mouseExited(e);
setBackground(new Color(186,212,255));
checkbox.setBackground(new Color(186,212,255));
}
};
UIManager.put("ProgressBar.background", new Color (235,255,255));
UIManager.put("ProgressBar.foreground", new Color (10,109,63));
UIManager.put("ProgressBar.selectionBackground", Color.RED);
UIManager.put("ProgressBar.selectionForeground", Color.GREEN);
setBackground(new Color(186,212,255));
setPreferredSize(new Dimension(600,100));
JPanel file = new JPanel();
/* test fileImg = new test("file.png");
fileImg.setBounds(10,10,40,50);
add(fileImg); */
JLabel fileNameLabel = new JLabel(fileName);
fileNameLabel.setBounds(100,5,400,15);
fileNameLabel.setForeground(new Color(109,111,114));
add(fileNameLabel);
JProgressBar jpb = new JProgressBar(0,100);
Random r = new Random();
jpb.setValue(r.nextInt(100));
jpb.setBounds(100,25,400,10);
JLabel progressBarValue = new JLabel("%" + jpb.getValue() + "");
progressBarValue.setBounds(510,18,25,20);
add(progressBarValue);
this.setBorder(BorderFactory.createMatteBorder(0,0,2,0,new Color(18,42,79)));
addMouseListener(a);
add(jpb);
JButton trash = new JButton(new ImageIcon("trash.png"));
trash.setBounds(130,40,25,25);
/* trash.setContentAreaFilled(false);
trash.setFocusPainted (false);
trash.setBorderPainted(false); */
trash.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("text");
}
});
add(trash);
test.setBounds(180,40,25,25);
test.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
System.out.println("text");
}
});
add(test);
double lower = 0;
double upper = 999.99;
sizeOfFile = Math.random() * (upper - lower) + lower;
downloadSpeed = Math.random() * (upper - lower) + lower;
downloadedSize = jpb.getValue() * sizeOfFile / 100;
JLabel downloadSpeedLabel = new JLabel (String.format ("%.1fKbs",downloadSpeed));
JLabel downloadedSizeLabel = new JLabel (String.format ("%.1fMG / %.2fMG",downloadedSize,sizeOfFile));
downloadedSizeLabel.setForeground(new Color(109,111,114));
downloadSpeedLabel.setForeground(new Color(109,111,114));
downloadSpeedLabel.setBounds(250,35,100,30);
downloadedSizeLabel.setBounds(420,35,150,30);
add(downloadSpeedLabel); add(downloadedSizeLabel);
}
public boolean isRemoved() {
return removed;
}
public void setRemoved(boolean removed) {
this.removed = removed;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public double getDownloadSpeed() {
return downloadSpeed;
}
public void setDownloadSpeed(double downloadSpeed) {
this.downloadSpeed = downloadSpeed;
}
public double getDownloadedSize() {
return downloadedSize;
}
public void setDownloadedSize(double downloadedSize) {
this.downloadedSize = downloadedSize;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public double getSizeOfFile() {
return sizeOfFile;
}
public void setSizeOfFile(double size) {
this.sizeOfFile = size;
}
}