我的应用由JFrame
组成,其JPanel
位于中心,另一个JPanel
位于南方。中心JPanel
是进一步JPanel
的网格}。南JPanel
是Jpanels
的水平数组。我在南面板中有多个图像,我想移动到中央网格。我已经浏览了docs.oracle.com上的DnD教程,但我找不到出路。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class DragMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent e) {
JComponent c = (JComponent) e.getSource();
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag(c, e, TransferHandler.COPY);
}
}
public class IQ {
JFrame frame;
JPanel centerPanel;
JPanel bottomPanel;
JPanel cPanel1;
JPanel cPanel2;
JPanel cPanel3;
JPanel cPanel4;
JPanel cPanel5;
JPanel cPanel6;
JPanel cPanel7;
JPanel cPanel8;
JPanel cPanel9;
JLabel clabel1;
JLabel clabel2;
JLabel clabel3;
JLabel clabel4;
JLabel clabel5;
JLabel clabel6;
JLabel clabel7;
JLabel clabel8;
JLabel clabel9;
JPanel bPanel1;
JPanel bPanel2;
JPanel bPanel3;
JPanel bPanel4;
JPanel bPanel5;
JPanel bPanel6;
JPanel bPanel7;
JPanel bPanel8;
JPanel bPanel9;
JLabel blabel1;
JLabel blabel2;
JLabel blabel3;
JLabel blabel4;
JLabel blabel5;
JLabel blabel6;
JLabel blabel7;
JLabel blabel8;
JLabel blabel9;
ImageIcon img;
public void go() {
frame = new JFrame();
centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(3, 2));
cPanel1 = new JPanel();
clabel1 = new JLabel();
cPanel1.add(clabel1);
cPanel1.setBackground(Color.red);
cPanel2 = new JPanel();
clabel2 = new JLabel();
cPanel2.add(clabel2);
cPanel2.setBackground(Color.cyan);
cPanel3 = new JPanel();
clabel3 = new JLabel();
cPanel3.add(clabel3);
cPanel3.setBackground(Color.yellow);
cPanel4 = new JPanel();
clabel4 = new JLabel();
cPanel4.add(clabel4);
cPanel4.setBackground(Color.blue);
cPanel5 = new JPanel();
clabel5 = new JLabel();
cPanel5.add(clabel5);
cPanel5.setBackground(Color.magenta);
cPanel6 = new JPanel();
clabel6 = new JLabel();
cPanel6.add(clabel6);
cPanel6.setBackground(Color.orange);
cPanel7 = new JPanel();
clabel7 = new JLabel();
cPanel7.add(clabel7);
cPanel7.setBackground(Color.gray);
cPanel8 = new JPanel();
clabel8 = new JLabel();
cPanel8.add(clabel8);
cPanel8.setBackground(Color.black);
cPanel9 = new JPanel();
clabel9 = new JLabel();
cPanel9.add(clabel9);
cPanel9.setBackground(Color.green);
centerPanel.add(cPanel1);
centerPanel.add(cPanel2);
centerPanel.add(cPanel3);
centerPanel.add(cPanel4);
centerPanel.add(cPanel5);
centerPanel.add(cPanel6);
centerPanel.add(cPanel7);
centerPanel.add(cPanel8);
centerPanel.add(cPanel9);
bottomPanel = new JPanel();
bPanel1 = new JPanel();
blabel1 = new JLabel();
img = new ImageIcon("1.png");
blabel1.setIcon(img);
bPanel1.add(blabel1);
bPanel2 = new JPanel();
blabel2 = new JLabel();
img = new ImageIcon("2.png");
blabel2.setIcon(img);
bPanel2.add(blabel2);
bPanel3 = new JPanel();
blabel3 = new JLabel();
img = new ImageIcon("3.png");
blabel3.setIcon(img);
bPanel3.add(blabel3);
bPanel4 = new JPanel();
blabel4 = new JLabel();
img = new ImageIcon("4.png");
blabel4.setIcon(img);
bPanel4.add(blabel4);
bPanel5 = new JPanel();
blabel5 = new JLabel();
img = new ImageIcon("5.png");
blabel5.setIcon(img);
bPanel5.add(blabel5);
bPanel6 = new JPanel();
blabel6 = new JLabel();
img = new ImageIcon("6.png");
blabel6.setIcon(img);
bPanel6.add(blabel6);
bPanel7 = new JPanel();
blabel7 = new JLabel();
img = new ImageIcon("7.png");
blabel7.setIcon(img);
bPanel7.add(blabel7);
bPanel8 = new JPanel();
blabel8 = new JLabel();
img = new ImageIcon("8.png");
blabel8.setIcon(img);
bPanel8.add(blabel8);
bPanel9 = new JPanel();
blabel9 = new JLabel();
img = new ImageIcon("9.png");
blabel9.setIcon(img);
bPanel9.add(blabel9);
bottomPanel.add(bPanel1);
bottomPanel.add(bPanel2);
bottomPanel.add(bPanel3);
bottomPanel.add(bPanel4);
bottomPanel.add(bPanel5);
bottomPanel.add(bPanel6);
bottomPanel.add(bPanel7);
bottomPanel.add(bPanel8);
bottomPanel.add(bPanel9);
MouseListener listener = new DragMouseAdapter();
blabel1.addMouseListener(listener);
blabel2.addMouseListener(listener);
blabel3.addMouseListener(listener);
blabel4.addMouseListener(listener);
blabel5.addMouseListener(listener);
blabel6.addMouseListener(listener);
blabel7.addMouseListener(listener);
blabel8.addMouseListener(listener);
blabel9.addMouseListener(listener);
blabel1.setTransferHandler(new TransferHandler("icon"));
blabel2.setTransferHandler(new TransferHandler("icon"));
blabel3.setTransferHandler(new TransferHandler("icon"));
blabel4.setTransferHandler(new TransferHandler("icon"));
blabel5.setTransferHandler(new TransferHandler("icon"));
blabel6.setTransferHandler(new TransferHandler("icon"));
blabel7.setTransferHandler(new TransferHandler("icon"));
blabel8.setTransferHandler(new TransferHandler("icon"));
blabel9.setTransferHandler(new TransferHandler("icon"));
frame.setSize(800, 600);
frame.getContentPane().add(BorderLayout.SOUTH, bottomPanel);
frame.getContentPane().add(BorderLayout.CENTER, centerPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) throws InterruptedException {
IQ s = new IQ();
s.go();
}
}