[java]代码库
package Test;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.TransferHandler;
public class Test1 extends JFrame
{
public static void main(String[] args)
{
EventQueue.invokeLater(() ->
{
JFrame frame = new Test1();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // 设置窗体出现位置居中
frame.setVisible(true);
});
}
public Test1()
{
JTabbedPane tabbedPane = new JTabbedPane();
JFileChooser fileChooser = new JFileChooser();
tabbedPane.addTab("File Chooser", fileChooser);
JColorChooser colorChooser = new JColorChooser();
tabbedPane.addTab("Color Chooser", colorChooser);
JTextArea textArea = new JTextArea(10, 40);
JScrollPane jScrollPane = new JScrollPane(textArea);
// 允许成为拖拽源(从这里开始拖组件)
fileChooser.setDragEnabled(true);
colorChooser.setDragEnabled(true);
// 让textField允许成为颜色的拖放目标(将组件放到这里)
JTextField textField = new JTextField("Drag color here");
textField.setTransferHandler(new TransferHandler("background"));
add(tabbedPane, BorderLayout.NORTH);
add(jScrollPane, BorderLayout.CENTER);
add(textField, BorderLayout.SOUTH);
pack();
}
}