Java多个拖放面板不起作用

时间:2012-11-28 15:13:58

标签: java swing drag-and-drop

我在Jpanel上有多个DnD面板,分层布局。每个DnD面板都有一个Text字段设置为放置目标。只有最后一个DnD面板才能获得焦点。其他人不作为下降目标。这是DnD面板的代码

public class DnDPanel extends javax.swing.JPanel {
/** Creates new form DnDPanel */

public DnDPanel() {
    initComponents();        
    dndTextField.setDropTarget(new DropTarget() {

        @Override
        public synchronized void drop(DropTargetDropEvent evt) {
            try {
                dndTextField.setBackground(Color.WHITE);
                evt.acceptDrop(DnDConstants.ACTION_COPY);
                List<File> droppedFiles = (List<File>) evt.getTransferable().getTransferData(
                        DataFlavor.javaFileListFlavor);
                for (File file : droppedFiles) {
                    dndTextField.setText(file.getAbsolutePath());
                }
                dndTextField.setBackground(Color.getHSBColor(0.56f, 1.0f, 0.9f));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public synchronized void dragEnter(DropTargetDragEvent dtde) {
            //Change cursor...
            Cursor cursor = new Cursor(Cursor.HAND_CURSOR);
            setCursor(cursor);
            dndTextField.setText("Drop Here !!");
            dndTextField.setBackground(Color.getHSBColor(0.56f, 1.0f, 0.9f));
        }

        @Override
        public synchronized void dragExit(DropTargetEvent dtde) {
            Cursor cursor = new Cursor(Cursor.DEFAULT_CURSOR);
            setCursor(cursor);
            dndTextField.setBackground(Color.WHITE);
            dndTextField.setText("Drag n drop a Folder");
        }
    });
}

public static void setText(String text)
{
    dndTextField.setText(text);
}

public static String getPath()
{
    return dndTextField.getText();
}


/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    dndTextField = new javax.swing.JTextField();

    setPreferredSize(new java.awt.Dimension(250, 250));

    dndTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    dndTextField.setText("DnD");
    dndTextField.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            dndTextFieldActionPerformed(evt);
        }
    });

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(layout.createSequentialGroup()
            .add(dndTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(layout.createSequentialGroup()
            .add(dndTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
            .addContainerGap())
    );
}// </editor-fold>

private void dndTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                             
    // TODO add your handling code here:
}                                            

// Variables declaration - do not modify
private static javax.swing.JTextField dndTextField;
// End of variables declaration
}

1 个答案:

答案 0 :(得分:3)

一个主要问题是您正在为JTextField使用静态变量:

private static javax.swing.JTextField dndTextField;

解决方案很简单:不要这样做!如果您希望每个JPanel都有自己独立于另一个的JTextField,那么它们应该是非静态的。事实上,除非有非常强烈的指示,否则你应该尽量减少程序中静态内容的使用。