这是BulletlinLayout类import java.awt.*;
import java.util.Hashtable;public class BulletinLayout1 implements LayoutManager
{
private Hashtable hash = new Hashtable(); public void removeLayoutComponent (Component comp){}
public void addLayoutComponent(String s,Component comp){} public Dimension preferredLayoutSize(Container target)
{
Insets insets = target.getInsets();
Dimension dim = new Dimension(0,0);
int ncomponents = target.getComponentCount();
Component comp;
Dimension d;
Rectangle size = new Rectangle(0,0); Rectangle compSize; for(int i = 0;i<ncomponents;i++)
{
comp = target.getComponent(i); if(comp.isVisible())
{
d = comp.getSize();
compSize = new Rectangle(comp.getLocation()); compSize.width = d.width;
compSize.height = d.height; size = size.union(compSize);
}
}
dim.width += size.width +insets.right;
dim.height += size.height + insets.bottom; return dim;
}
public Dimension minimumLayoutSize(Container target)
{
Insets insets = target.getInsets();
Dimension dim = new Dimension(0,0);
int ncomponents = target.getComponentCount();
Component comp;
Dimension d;
Rectangle minBounds = new Rectangle(0,0);
Rectangle compMinBounds; for(int i=0;i<ncomponents;i++)
{
comp = target.getComponent(i); if(comp.isVisible())
{
d= comp.getMinimumSize();
compMinBounds= new Rectangle(comp.getLocation());
compMinBounds.setSize(d.width,d.height); minBounds = minBounds.union(compMinBounds);
}
}
dim.width += minBounds.width + insets.right;
dim.height +=minBounds.height+insets.bottom; return dim;
}
public void layoutContainer(Container target)
{
Insets insets = target.getInsets();
int ncomponents = target.getComponentCount();
Component comp;
Dimension sz,ps;
Point Ioc; for(int i = 0;i<ncomponents;i++)
{
comp= target.getComponent(i);
if(comp.isVisible())
{
sz = comp.getSize();
ps= comp.getPreferredSize();
Ioc = getComponentLocation(comp);
if(sz.width<ps.width||sz.height<ps.height)
sz= ps;
comp.setBounds(Ioc.x,Ioc.y,sz.width,sz.height);
}
}
}
private Point getComponentLocation(Component comp)
{
Insets insets = comp.getParent().getInsets();
Point Ioc = comp.getLocation();
if(! hash.containsKey(comp))
{
addComponentToHashtable(comp);
}
else{
Point oldLocation = (Point)hash.get(comp); if(oldLocation.x!=Ioc.x||oldLocation.y!= Ioc.y)
{
addComponentToHashtable(comp); }
}
return comp.getLocation();
}
private void addComponentToHashtable(Component comp)
{
Insets insets = comp.getParent().getInsets();
Point Ioc = comp.getLocation(); comp.setLocation(Ioc.x+ insets.left,Ioc.y+insets.top);
hash.put(comp,comp.getLocation());
}
}
import java.util.Hashtable;public class BulletinLayout1 implements LayoutManager
{
private Hashtable hash = new Hashtable(); public void removeLayoutComponent (Component comp){}
public void addLayoutComponent(String s,Component comp){} public Dimension preferredLayoutSize(Container target)
{
Insets insets = target.getInsets();
Dimension dim = new Dimension(0,0);
int ncomponents = target.getComponentCount();
Component comp;
Dimension d;
Rectangle size = new Rectangle(0,0); Rectangle compSize; for(int i = 0;i<ncomponents;i++)
{
comp = target.getComponent(i); if(comp.isVisible())
{
d = comp.getSize();
compSize = new Rectangle(comp.getLocation()); compSize.width = d.width;
compSize.height = d.height; size = size.union(compSize);
}
}
dim.width += size.width +insets.right;
dim.height += size.height + insets.bottom; return dim;
}
public Dimension minimumLayoutSize(Container target)
{
Insets insets = target.getInsets();
Dimension dim = new Dimension(0,0);
int ncomponents = target.getComponentCount();
Component comp;
Dimension d;
Rectangle minBounds = new Rectangle(0,0);
Rectangle compMinBounds; for(int i=0;i<ncomponents;i++)
{
comp = target.getComponent(i); if(comp.isVisible())
{
d= comp.getMinimumSize();
compMinBounds= new Rectangle(comp.getLocation());
compMinBounds.setSize(d.width,d.height); minBounds = minBounds.union(compMinBounds);
}
}
dim.width += minBounds.width + insets.right;
dim.height +=minBounds.height+insets.bottom; return dim;
}
public void layoutContainer(Container target)
{
Insets insets = target.getInsets();
int ncomponents = target.getComponentCount();
Component comp;
Dimension sz,ps;
Point Ioc; for(int i = 0;i<ncomponents;i++)
{
comp= target.getComponent(i);
if(comp.isVisible())
{
sz = comp.getSize();
ps= comp.getPreferredSize();
Ioc = getComponentLocation(comp);
if(sz.width<ps.width||sz.height<ps.height)
sz= ps;
comp.setBounds(Ioc.x,Ioc.y,sz.width,sz.height);
}
}
}
private Point getComponentLocation(Component comp)
{
Insets insets = comp.getParent().getInsets();
Point Ioc = comp.getLocation();
if(! hash.containsKey(comp))
{
addComponentToHashtable(comp);
}
else{
Point oldLocation = (Point)hash.get(comp); if(oldLocation.x!=Ioc.x||oldLocation.y!= Ioc.y)
{
addComponentToHashtable(comp); }
}
return comp.getLocation();
}
private void addComponentToHashtable(Component comp)
{
Insets insets = comp.getParent().getInsets();
Point Ioc = comp.getLocation(); comp.setLocation(Ioc.x+ insets.left,Ioc.y+insets.top);
hash.put(comp,comp.getLocation());
}
}
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.*;public class ImageSelection extends TransferHandler
implements Transferable { private static final DataFlavor flavors[] =
{DataFlavor.imageFlavor}; private Image image; public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
} public boolean canImport(JComponent comp, DataFlavor
flavor[]) {
if (!(comp instanceof JLabel) ||
(comp instanceof AbstractButton)) {
return false;
}
for (int i=0, n=flavor.length; i<n; i++) {
if (flavor[i].equals(flavors[0])) {
return true;
}
}
return false;
} public Transferable createTransferable(JComponent
comp) {
// Clear
image = null;
Icon icon = null; if (comp instanceof JLabel) {
JLabel label = (JLabel)comp;
icon = label.getIcon();
} else if (comp instanceof AbstractButton) {
AbstractButton button = (AbstractButton)comp;
icon = button.getIcon();
}
if (icon instanceof ImageIcon) {
image = ((ImageIcon)icon).getImage();
return this;
}
return null;
} public boolean importData(JComponent comp,
Transferable t) {
ImageIcon icon = null;
try {
if (t.isDataFlavorSupported(flavors[0])) {
image = (Image)t.getTransferData(flavors[0]);
icon = new ImageIcon(image);
}
if (comp instanceof JLabel) {
JLabel label = (JLabel)comp;
label.setIcon(icon);
return true;
} else if (comp instanceof AbstractButton) {
AbstractButton button = (AbstractButton)comp;
button.setIcon(icon);
return true;
}
} catch (UnsupportedFlavorException ignored) {
} catch (IOException ignored) {
}
return false;
} // Transferable
public Object getTransferData(DataFlavor flavor) {
if (isDataFlavorSupported(flavor)) {
return image;
}
return null;
} public DataFlavor[] getTransferDataFlavors() {
return flavors;
} public boolean isDataFlavorSupported(DataFlavor
flavor) {
return flavor.equals(flavors[0]);
}
}