日期:2014-05-20 浏览次数:20900 次
import java.awt.*; import java.awt.dnd.*; import java.awt.event.*; import java.util.Locale; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; public class DesktopTools extends JFrame implements DropTargetListener { private JScrollPane scrollPane; private JPanel panel; DropTarget dropTarget; private int i, j; public DesktopTools() { setTitle("Tools"); setSize(370, 200); setLocation(100, 100); this.setAlwaysOnTop(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); dropTarget = new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, this, true, null); panel = new JPanel(); scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); this.getContentPane().add(scrollPane); setVisible(true); setResizable(false); this.repaint(); } public static void main(String args[]) { DesktopTools dt = new DesktopTools(); } @Override public void dragEnter(DropTargetDragEvent dtde) { // throw new UnsupportedOperationException("Not supported yet."); } @Override public void dragOver(DropTargetDragEvent dtde) { // throw new UnsupportedOperationException("Not supported yet."); } @Override public void dropActionChanged(DropTargetDragEvent dtde) { // throw new UnsupportedOperationException("Not supported yet."); } @Override public void dragExit(DropTargetEvent dte) { // throw new UnsupportedOperationException("Not supported yet."); } @Override public void drop(DropTargetDropEvent dtde) { // throw new UnsupportedOperationException("Not supported yet."); int bWidth = 80; int bHeight = 80; if ((dtde.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0) { System.out.println("test"); JButton jb = new JButton("test" + i); panel.add(jb); jb.setSize(bWidth, bHeight); jb.setLocation(5 + (i % 4) * (bWidth + 2), 20 + (j * (bHeight + 2))); if (((i + 1) % 4) == 0) { j++; } i++; [color=#FFFF00]//panel.updateUI();[/color] [color=#FF0000]panel.repaint();[/color] this.repaint(); } } }
@Override public void drop(DropTargetDropEvent dtde) { // throw new UnsupportedOperationException("Not supported yet."); int bWidth = 80; int bHeight = 80; if ((dtde.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0) { System.out.println("test"); JButton jb = new JButton("test" + i); panel.add(jb); Dimension d1 = new Dimension(bWidth, bHeight); jb.setPreferredSize(d1); jb .setLocation(5 + (i % 4) * (bWidth + 2), 20 + (j * (bHeight + 2))); if (((i + 1) % 4) == 0) { Dimension d = new Dimension(this.getWidth(), this.getHeight() + j * bHeight); panel.setPreferredSize(d); j++; } i++; panel.updateUI(); panel.repaint(); this.repaint(); } }