luckyblank
3 years ago
17 changed files with 629 additions and 19 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,123 @@ |
|||
/* |
|||
* Copyright (C), 2022-2022, Freedom Person |
|||
* FileName: DragTestUtil.java |
|||
* Author: lucky |
|||
* Date: 2022/1/16 5:21 |
|||
* Description: //模块目的、功能描述
|
|||
* History: //修改记录
|
|||
* <author> <time> <version> <desc> |
|||
* 修改人姓名 修改时间 版本号 描述 |
|||
*/ |
|||
package com.wuzf.swing.utils; |
|||
|
|||
import com.wuzf.swing.demos.SwingSet2; |
|||
|
|||
import javax.swing.*; |
|||
import javax.swing.text.JTextComponent; |
|||
import java.awt.*; |
|||
import java.awt.datatransfer.DataFlavor; |
|||
import java.awt.dnd.DnDConstants; |
|||
import java.awt.dnd.DropTarget; |
|||
import java.awt.dnd.DropTargetAdapter; |
|||
import java.awt.dnd.DropTargetDropEvent; |
|||
import java.io.File; |
|||
import java.util.ArrayList; |
|||
import java.util.Arrays; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* 功能描述:<br> |
|||
* |
|||
* @author lucky |
|||
* @see [相关类/方法](可选) |
|||
* @since [产品/模块版本] (可选) |
|||
*/ |
|||
|
|||
public class DragTest extends JFrame{ |
|||
|
|||
JPanel panel;//要接受拖拽的面板
|
|||
public DragTest() |
|||
{ |
|||
|
|||
panel = new JPanel(); |
|||
panel.setBackground(Color.WHITE); |
|||
getContentPane().add(panel, BorderLayout.CENTER); |
|||
setSize(500, 400); |
|||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|||
setLocation(400, 200); |
|||
|
|||
panel.setLayout(null);// 将父JPanel的布局设置为绝对布局
|
|||
setTitle("最简单的拖拽示例:拖拽文件到下面"); |
|||
|
|||
List<String> allowedFileTypeList = Arrays.asList(".jpg",".xlsx",".doc",".docx",".xls",".txt"); |
|||
JTextArea jta = new JTextArea(); |
|||
//在文本框上添加滚动条
|
|||
JScrollPane jsp = new JScrollPane(jta); |
|||
|
|||
//设置矩形大小.参数依次为(矩形左上角横坐标x,矩形左上角纵坐标y,矩形长度,矩形宽度)
|
|||
jsp.setBounds(30,30,400,250); |
|||
|
|||
//默认的设置是超过文本框才会显示滚动条,以下设置让滚动条一直显示
|
|||
// jsp.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
|
|||
|
|||
//把滚动条添加到容器里面
|
|||
panel.add(jsp); |
|||
openDrag(jta,jta, allowedFileTypeList);//启用拖拽
|
|||
} |
|||
public static void main(String[] args) throws Exception |
|||
{ |
|||
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");//设置皮肤
|
|||
SwingSet2.initBeautyStyle(); |
|||
new DragTest().setVisible(true);; |
|||
} |
|||
public static void openDrag(JComponent allowDropComponent, JTextComponent showPathComponent, List<String> allowedFileTypeList)//定义的拖拽方法
|
|||
{ |
|||
DropTargetAdapter dropTargetAdapter = new DropTargetAdapter(){ |
|||
List<File> fileList = new ArrayList<>(); |
|||
@Override |
|||
public void drop(DropTargetDropEvent dtde)//重写适配器的drop方法
|
|||
{ |
|||
try |
|||
{ |
|||
List<String> failfileList = new ArrayList<>(); |
|||
if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor))//如果拖入的文件格式受支持
|
|||
{ |
|||
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);//接收拖拽来的数据
|
|||
fileList = (List<File>) (dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor)); |
|||
String temp=""; |
|||
for(File file : fileList){ |
|||
|
|||
String fileAbsolutePath = file.getAbsolutePath(); |
|||
String fileName = file.getName(); |
|||
String fileSuffix = fileName.substring(fileName.lastIndexOf(".")); |
|||
if (!allowedFileTypeList.contains(fileSuffix.toLowerCase()) && allowedFileTypeList.size() > 0){ |
|||
failfileList.add(file.getAbsolutePath()); |
|||
System.out.println(failfileList); |
|||
JOptionPane.showMessageDialog(null, "仅支持word/xlsx/jpg格式文件"); |
|||
return ; |
|||
} |
|||
|
|||
temp+= fileAbsolutePath +";\n"; |
|||
} |
|||
//输入框中显示 文件路径
|
|||
showPathComponent.setText(fileList.get(0).getAbsolutePath()); |
|||
JOptionPane.showMessageDialog(null, temp); |
|||
dtde.dropComplete(true);//指示拖拽操作已完成
|
|||
} |
|||
else |
|||
{ |
|||
JOptionPane.showMessageDialog(null, "仅支持文件格式"); |
|||
dtde.rejectDrop();//否则拒绝拖拽来的数据
|
|||
} |
|||
} |
|||
catch (Exception e) |
|||
{ |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
}; |
|||
|
|||
//panel表示要接受拖拽的控件
|
|||
DropTarget target = new DropTarget(allowDropComponent, DnDConstants.ACTION_COPY_OR_MOVE, dropTargetAdapter); |
|||
} |
|||
} |
@ -0,0 +1,121 @@ |
|||
/* |
|||
* Copyright (C), 2022-2022, Freedom Person |
|||
* FileName: JavaFileDragDrop.java |
|||
* Author: lucky |
|||
* Date: 2022/1/16 13:57 |
|||
* Description: //模块目的、功能描述
|
|||
* History: //修改记录
|
|||
* <author> <time> <version> <desc> |
|||
* 修改人姓名 修改时间 版本号 描述 |
|||
*/ |
|||
package com.wuzf.swing.utils; |
|||
|
|||
import javax.swing.*; |
|||
import java.awt.datatransfer.DataFlavor; |
|||
import java.awt.datatransfer.Transferable; |
|||
import java.awt.dnd.DnDConstants; |
|||
import java.awt.dnd.DropTarget; |
|||
import java.awt.dnd.DropTargetAdapter; |
|||
import java.awt.dnd.DropTargetDropEvent; |
|||
import java.io.File; |
|||
import java.util.Iterator; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* 功能描述:<br> |
|||
* |
|||
* @author lucky |
|||
* @see [相关类/方法](可选) |
|||
* @since [产品/模块版本] (可选) |
|||
*/ |
|||
|
|||
public class JavaFileDragDrop extends JFrame |
|||
|
|||
{ |
|||
|
|||
JTextArea jta; |
|||
|
|||
public JavaFileDragDrop() |
|||
|
|||
{ |
|||
|
|||
this.setTitle("java file drag and drop test"); |
|||
|
|||
this.setBounds(150,150,300,300); |
|||
|
|||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|||
|
|||
this.setLayout(null); |
|||
|
|||
jta=new JTextArea(); |
|||
|
|||
DropTargetAdapter kgd=new DropTargetAdapter() { |
|||
|
|||
public void drop(DropTargetDropEvent dtde) { |
|||
try |
|||
{ |
|||
|
|||
Transferable tf=dtde.getTransferable(); |
|||
|
|||
if(tf.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) |
|||
|
|||
{ |
|||
|
|||
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); |
|||
|
|||
List lt=(List)tf.getTransferData(DataFlavor.javaFileListFlavor); |
|||
|
|||
Iterator itor=lt.iterator(); |
|||
|
|||
while(itor.hasNext()) |
|||
|
|||
{ |
|||
File f=(File)itor.next(); |
|||
jta.setText(jta.getText()+"n"+f.getAbsolutePath()); |
|||
} |
|||
dtde.dropComplete(true); |
|||
} else { |
|||
dtde.rejectDrop(); |
|||
} |
|||
|
|||
} |
|||
|
|||
catch(Exception e) |
|||
|
|||
{ |
|||
|
|||
e.printStackTrace(); |
|||
|
|||
} |
|||
|
|||
} |
|||
|
|||
}; |
|||
|
|||
new DropTarget(jta, DnDConstants.ACTION_COPY_OR_MOVE,kgd); |
|||
|
|||
//在文本框上添加滚动条
|
|||
JScrollPane jsp = new JScrollPane(jta); |
|||
|
|||
//设置矩形大小.参数依次为(矩形左上角横坐标x,矩形左上角纵坐标y,矩形长度,矩形宽度)
|
|||
jsp.setBounds(30,30,250,250); |
|||
|
|||
//默认的设置是超过文本框才会显示滚动条,以下设置让滚动条一直显示
|
|||
// jsp.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
|
|||
|
|||
//把滚动条添加到容器里面
|
|||
this.add(jsp); |
|||
|
|||
this.setVisible(true); |
|||
|
|||
} |
|||
|
|||
public static void main(String[] args) |
|||
|
|||
{ |
|||
|
|||
new JavaFileDragDrop(); |
|||
|
|||
} |
|||
|
|||
} |
@ -0,0 +1,125 @@ |
|||
/* |
|||
* Copyright (C), 2022-2022, Freedom Person |
|||
* FileName: UploadFile.java |
|||
* Author: lucky |
|||
* Date: 2022/1/16 4:50 |
|||
* Description: //模块目的、功能描述
|
|||
* History: //修改记录
|
|||
* <author> <time> <version> <desc> |
|||
* 修改人姓名 修改时间 版本号 描述 |
|||
*/ |
|||
package com.wuzf.swing.utils; |
|||
|
|||
import com.wuzf.swing.demos.SwingSet2; |
|||
|
|||
import javax.swing.*; |
|||
import javax.swing.filechooser.FileNameExtensionFilter; |
|||
import java.awt.event.MouseAdapter; |
|||
import java.awt.event.MouseEvent; |
|||
import java.io.*; |
|||
import java.util.HashSet; |
|||
|
|||
/** |
|||
* 功能描述:<br> |
|||
* |
|||
* @author lucky |
|||
* @see [相关类/方法](可选) |
|||
* @since [产品/模块版本] (可选) |
|||
*/ |
|||
|
|||
public class UploadFileUtil { |
|||
|
|||
public static void createUploadFrame(String title,Object selectedValue) { |
|||
JFrame jframe = new JFrame(title);// 实例化一个JFrame
|
|||
JPanel jPanel = new JPanel(); // 创建一个轻量级容器
|
|||
JToolBar jToolBar = new JToolBar(); // 提供了一个用来显示常用的 Action 或控件的组件
|
|||
jframe.setVisible(true);// 可见
|
|||
jframe.setSize(500, 500);// 窗体大小
|
|||
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);// close的方式
|
|||
jframe.setContentPane(jPanel); // 设置 contentPane 属性。
|
|||
JLabel jl = new JLabel("请选择:");// 创建一个Label标签
|
|||
jl.setHorizontalAlignment(SwingConstants.LEFT);// 样式,让文字居中
|
|||
jPanel.add("North", jl);// 将标签添加到容器中
|
|||
jPanel.add("North", jToolBar); |
|||
JButton developer = new JButton("上传文件"); |
|||
developer.setHorizontalAlignment(SwingConstants.CENTER); |
|||
jToolBar.add(developer);// 上传文件按钮添加到容器
|
|||
jPanel.add("North", jToolBar); |
|||
developer.addMouseListener(new MouseAdapter() { // 添加鼠标点击事件
|
|||
public void mouseClicked(MouseEvent event) { |
|||
eventOnImport(new JButton()); |
|||
} |
|||
}); // 文件上传功能
|
|||
} |
|||
|
|||
/** |
|||
* 文件上传功能 |
|||
* |
|||
* @param developer |
|||
* 按钮控件名称 |
|||
*/ |
|||
public static void eventOnImport(JButton developer) { |
|||
JFileChooser chooser = new JFileChooser(); |
|||
chooser.setMultiSelectionEnabled(true); |
|||
/** 过滤文件类型 * */ |
|||
FileNameExtensionFilter filter = new FileNameExtensionFilter("war", |
|||
"xml", "txt", "doc", "docx"); |
|||
chooser.setFileFilter(filter); |
|||
int returnVal = chooser.showOpenDialog(developer); |
|||
if (returnVal == JFileChooser.APPROVE_OPTION) { |
|||
/** 得到选择的文件* */ |
|||
File[] arrfiles = chooser.getSelectedFiles(); |
|||
if (arrfiles == null || arrfiles.length == 0) { |
|||
return; |
|||
} |
|||
FileInputStream input = null; |
|||
FileOutputStream out = null; |
|||
String path = "./"; |
|||
try { |
|||
for (File f : arrfiles) { |
|||
File dir = new File(path); |
|||
/** 目标文件夹 * */ |
|||
File[] fs = dir.listFiles(); |
|||
HashSet<String> set = new HashSet<String>(); |
|||
for (File file : fs) { |
|||
set.add(file.getName()); |
|||
} |
|||
/** 判断是否已有该文件* */ |
|||
if (set.contains(f.getName())) { |
|||
JOptionPane.showMessageDialog(new JDialog(), |
|||
f.getName() + ":该文件已存在!"); |
|||
return; |
|||
} |
|||
input = new FileInputStream(f); |
|||
byte[] buffer = new byte[1024]; |
|||
File des = new File(path, f.getName()); |
|||
out = new FileOutputStream(des); |
|||
int len = 0; |
|||
while (-1 != (len = input.read(buffer))) { |
|||
out.write(buffer, 0, len); |
|||
} |
|||
out.close(); |
|||
input.close(); |
|||
} |
|||
JOptionPane.showMessageDialog(null, "上传成功!", "提示", |
|||
JOptionPane.INFORMATION_MESSAGE); |
|||
|
|||
} catch (FileNotFoundException e1) { |
|||
JOptionPane.showMessageDialog(null, "上传失败!", "提示", |
|||
JOptionPane.ERROR_MESSAGE); |
|||
e1.printStackTrace(); |
|||
} catch (IOException e1) { |
|||
JOptionPane.showMessageDialog(null, "上传失败!", "提示", |
|||
JOptionPane.ERROR_MESSAGE); |
|||
e1.printStackTrace(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public static void main(String[] args) { |
|||
//初始化ui
|
|||
// SwingSet2.initBeautyStyle();
|
|||
createUploadFrame("上传文件","管理员"); |
|||
|
|||
} |
|||
} |
@ -0,0 +1,200 @@ |
|||
/* |
|||
* Copyright (C), 2022-2022, Freedom Person |
|||
* FileName: JwebBrowser.java |
|||
* Author: lucky |
|||
* Date: 2022/1/16 16:11 |
|||
* Description: //模块目的、功能描述
|
|||
* History: //修改记录
|
|||
* <author> <time> <version> <desc> |
|||
* 修改人姓名 修改时间 版本号 描述 |
|||
*/ |
|||
package com.wuzf.swing.utils; |
|||
|
|||
|
|||
import chrriis.dj.nativeswing.common.UIUtils; |
|||
import chrriis.dj.nativeswing.swtimpl.NativeInterface; |
|||
import chrriis.dj.nativeswing.swtimpl.components.*; |
|||
import com.wuzf.swing.demos.SwingSet2; |
|||
|
|||
import javax.swing.*; |
|||
import java.awt.*; |
|||
import java.beans.PropertyVetoException; |
|||
import java.util.Hashtable; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* 功能描述:<br> |
|||
* |
|||
* @author lucky |
|||
* @see [相关类/方法](可选) |
|||
* @since [产品/模块版本] (可选) |
|||
*/ |
|||
|
|||
public class WzfJwebFrame extends JInternalFrame { |
|||
|
|||
public static JWebBrowser webBrowser ; //浏览器模型
|
|||
|
|||
public static Map<String,JWebBrowser> t_jwbrowsers=new Hashtable<String, JWebBrowser>(); |
|||
|
|||
// private static final String URL = "https://www.luckyblank.cn";
|
|||
// private static final String URL = "file:///D:/project/recent/jdk-test/Demo/summer/index.html";
|
|||
|
|||
public static JInternalFrame createInternalFrame(String url) throws PropertyVetoException { |
|||
JInternalFrame internalFrame = new JInternalFrame( |
|||
"WzfJwebBrowser", // title
|
|||
true, // resizable
|
|||
true, // closable
|
|||
true, // maximizable
|
|||
true // iconifiable
|
|||
); |
|||
|
|||
internalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|||
|
|||
internalFrame.setSize(1183, 700); |
|||
// 设置窗口的显示位置
|
|||
internalFrame.setLocation(0, 0); |
|||
internalFrame.setLayout(null); |
|||
|
|||
webBrowser = new JWebBrowser(); |
|||
webBrowser .navigate(url); |
|||
webBrowser .setPreferredSize(new Dimension(800,400)); |
|||
webBrowser .setBarsVisible(false); |
|||
webBrowser .setMenuBarVisible(false); |
|||
webBrowser .setButtonBarVisible(false); |
|||
webBrowser .setStatusBarVisible(false); |
|||
|
|||
//添加事件监听
|
|||
webBrowser.addWebBrowserListener(new WebBrowserListener() { |
|||
|
|||
@Override |
|||
public void windowWillOpen(WebBrowserWindowWillOpenEvent arg0) { |
|||
JWebBrowser jwb=arg0.getNewWebBrowser(); |
|||
String location=jwb.getResourceLocation(); |
|||
System.out.println("windowWillOpen: "+location); |
|||
} |
|||
|
|||
@Override |
|||
public void windowOpening(WebBrowserWindowOpeningEvent arg0) { |
|||
// JWebBrowser jwb=arg0.getNewWebBrowser();
|
|||
// String location=jwb.getResourceLocation();
|
|||
// System.out.println(location);
|
|||
} |
|||
|
|||
@Override |
|||
public void windowClosing(WebBrowserEvent arg0) { |
|||
// TODO Auto-generated method stub
|
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void titleChanged(WebBrowserEvent arg0) { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void statusChanged(WebBrowserEvent arg0) { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void locationChanging(WebBrowserNavigationEvent arg0) { |
|||
String location=arg0.getNewResourceLocation(); |
|||
System.out.println("locationChanging: "+location); |
|||
} |
|||
|
|||
@Override |
|||
public void locationChanged(WebBrowserNavigationEvent arg0) { |
|||
// TODO Auto-generated method stub
|
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void locationChangeCanceled(WebBrowserNavigationEvent arg0) { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void loadingProgressChanged(WebBrowserEvent arg0) { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void commandReceived(WebBrowserCommandEvent arg0) { |
|||
|
|||
} |
|||
}); |
|||
|
|||
//存储URL和webrowser关系
|
|||
t_jwbrowsers.put(url, webBrowser); |
|||
|
|||
// 创建内容面板
|
|||
JPanel panel = new JPanel(new BorderLayout()); |
|||
|
|||
// 添加组件到面板
|
|||
panel.add(webBrowser,BorderLayout.CENTER); |
|||
|
|||
// 设置内部窗口的内容面板
|
|||
internalFrame.setContentPane(panel); |
|||
// 显示内部窗口
|
|||
internalFrame.setVisible(true); |
|||
|
|||
return internalFrame; |
|||
} |
|||
|
|||
|
|||
public static void main(String[] args) { |
|||
//ui
|
|||
SwingSet2.initBeautyStyle(); |
|||
|
|||
//初始化Native Swing
|
|||
UIUtils.setPreferredLookAndFeel(); |
|||
|
|||
if(!NativeInterface.isOpen()){ |
|||
NativeInterface.initialize(); |
|||
NativeInterface.open(); |
|||
} |
|||
|
|||
SwingUtilities.invokeLater(new Runnable() { |
|||
public void run() { |
|||
try { |
|||
|
|||
creatGUI(); |
|||
|
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
}); |
|||
//介绍网站https://sourceforge.net/p/djproject/discussion/671154/thread/e813001e/
|
|||
//NativeInterface.runEventPump();
|
|||
|
|||
} |
|||
public static JWebBrowser getJWebBrowser(String url){ |
|||
return t_jwbrowsers.get(url); |
|||
} |
|||
|
|||
public static JFrame creatGUI() throws PropertyVetoException { |
|||
|
|||
JFrame jf = new JFrame("测试窗口"); |
|||
jf.setSize(1200, 800); |
|||
jf.setLocationRelativeTo(null); |
|||
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
|||
//创建桌面面板
|
|||
JDesktopPane desktopPane = new JDesktopPane(); //桌面面板
|
|||
|
|||
// 创建 内部窗口
|
|||
String url = "https://www.luckyblank.cn"; |
|||
url = "https://www.baidu.com"; |
|||
JInternalFrame internalFrame = createInternalFrame(url); |
|||
|
|||
// 添加 内部窗口 到 桌面面板
|
|||
desktopPane.add(internalFrame); |
|||
|
|||
desktopPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); //设置内部窗体拖动模式
|
|||
// jf.getContentPane().add(desktopPane,BorderLayout.CENTER);; // 设置 contentPane 属性。
|
|||
jf.setContentPane(desktopPane); |
|||
jf.setVisible(true); |
|||
return jf; |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue