当我尝试保存然后从文件加载(并且经常添加到JPanel)时,通过用鼠标拖动来扩展可移动的JLabel的对象,它被放置在第一个setBounds()设置的位置。我甚至尝试在mouseDragged中调用setBounds(),但它不起作用。有没有更聪明的方法来实现它,而不是保存最后的坐标,然后当我从文件加载时再次调用setBounds()? 我需要保存的对象与此类似:
编辑:这是否适用于SSCCE?
package prove;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class AppletProve extends JPanel implements MouseListener, MouseMotionListener{
private JLabel a;
public AppletProve(){
super();
setLayout(null);
Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
dim.width=(int) (dim.width*0.66);
dim.height=(int) (dim.height*0.75);
setPreferredSize(dim);
addMouseListener(this);
addMouseMotionListener(this);
}
public void crea(){
a=new JLabel("PROVA");
a.setBounds(40,40, 40, 40);
add(a);
File f=new File("prova.dat");
ObjectOutputStream oos = null;
try {
oos=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
oos.writeObject(a);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("eccezione salvataggio");
} finally{
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void collega(){
File f=new File("prova.dat");
a=new JLabel("prova2");
ObjectInputStream ois = null;
try {
ois=new ObjectInputStream(new BufferedInputStream(new FileInputStream(f)));
a=(JLabel)ois.readObject();
} catch (Exception e) {
System.out.println("eccezione caricamento");
e.printStackTrace();
} finally{
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
add(a);
this.validate();
}
@Override
public void mouseClicked(MouseEvent arg0) {
System.out.println("chiamato");
collega();
repaint();
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent arg0) {
a.setLocation(arg0.getPoint());
a.repaint();
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
JFrame frame=new JFrame("Prova");
AppletProve ap=new AppletProve();
ap.setOpaque(true);
frame.setContentPane(ap);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ap.crea();
}
}