作为一个刚学习Java的小白,我完成了一个简单的小工具,这篇文章或许能给你带来启发。
也欢迎指正,正在学习阶段。
首先使用的是Java.Swing 个人建议就是把各种英文了解一下就可以使用啦
JLabel userLabel = new JLabel("请拖拽到这里:");
userLabel.setBounds(10, 20, 120, 25);//定义组件位置
panel.add(userLabel);
//文本框
JTextField filePathText = new JTextField(20);
filePathText.setBounds(10, 50, 250, 30);
panel.add(filePathText);
// 创建按钮
JButton openSelect = new JButton("打开");
openSelect.setBounds(260,50,60,20);
JButton installButton = new JButton("点击安装");
installButton.setBounds(10, 100, 120, 20);
panel.add(openSelect);
panel.add(installButton);
拖拽功能的实现 ,这就是要拖拽到filePathText显示拖进去的文件路径。
new DropTarget(filePathText, DnDConstants.ACTION_COPY_OR_MOVE, new DropTargetAdapter() {
@Override
public void drop(DropTargetDropEvent dtde) {
if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { //判断是否支持此文件的格式
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); //接受该文件
try {
@SuppressWarnings("unchecked")
List<File> list = (List<File>) dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
filePathText.setText(filePathText.getText() + list.get(list.size() - 1).getAbsolutePath() + "\r\n");
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
} else {
dtde.rejectDrop(); //拒绝该拖拽文件
}
}
});
实现apk的解析,需要用到数据流
try {
//Process process = Runtime.getRuntime().exec(filePathText.getText());
Runtime rt = Runtime.getRuntime();
//路径符separator 相当于'\'
//需配置好aapt才可以实现解析apk文件
//aapt dump badging < file.apk>查看各种详细信息。
String order = "F:"+File.separator+"aapt-windows"+File.separator+"aapt d badging "+filePathText.getText();
System.out.println(order);
Process process = rt.exec(order);
InputStream stream = process.getInputStream();
InputStreamReader isr = new InputStreamReader(stream);
BufferedReader br = new BufferedReader(isr);
String line = null;
while((line = br.readLine())!=null){
System.out.println(line);
}
//负责建立线程并等待线程结束
process.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
看一下效果图:
附上源码
package com.hollyland.TestInstall;
import javax.swing.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.List;
public class Frist extends JFrame {
//public static final Logger LOGGER = LogManager.getLogger(Log4j2Test);
public static Long count = 0L;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("检索安装工具");
frame.setSize(350,200);
frame.setLocationRelativeTo(null);//设置在屏幕中间
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
//Frist ft = new Frist();
placeComponents(panel);
frame.setResizable(false);
frame.setContentPane(panel);
frame.setVisible(true);
});
}
private static void placeComponents(JPanel panel) {
//拖拽到这里
panel.setLayout(null);
JLabel userLabel = new JLabel("请拖拽到这里:");
userLabel.setBounds(10, 20, 120, 25);//定义组件位置
panel.add(userLabel);
//文本框
JTextField filePathText = new JTextField(20);
filePathText.setBounds(10, 50, 250, 30);
panel.add(filePathText);
// 创建按钮
JButton openSelect = new JButton("打开");
openSelect.setBounds(260,50,60,20);
JButton installButton = new JButton("点击安装");
installButton.setBounds(10, 100, 120, 20);
panel.add(openSelect);
panel.add(installButton);
/*
使用addActionListener(ActionListener l)方法为jButton(按钮)添加监听事件
实现addActionListener里的参数接口ActionListener,重写ActionListener接口的actionPerformed方法
如果发生了点击事件,系统将会回调actionPerformed方法
*/
//监听点击安装按钮
installButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("我点击了安装");
String str = null;
str = filePathText.getText().toString().toLowerCase();
System.out.println(str);
if(str.contains(".apk")){
System.out.println("为.apk文件");
//安装文件
try {
//Process process = Runtime.getRuntime().exec(filePathText.getText());
Runtime rt = Runtime.getRuntime();
//路径符separator 相当于'\'
//需配置好aapt才可以实现解析apk文件
//aapt dump badging < file.apk>查看各种详细信息。
String order = "F:"+File.separator+"aapt-windows"+File.separator+"aapt d badging "+filePathText.getText();
System.out.println(order);
Process process = rt.exec(order);
InputStream stream = process.getInputStream();
InputStreamReader isr = new InputStreamReader(stream);
BufferedReader br = new BufferedReader(isr);
String line = null;
while((line = br.readLine())!=null){
System.out.println(line);
}
//负责建立线程并等待线程结束
process.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
else{
System.out.println("文件不是可执行文件!无法安装!!");}
}
});
JFileChooser jfc = new JFileChooser();
//监听打开按钮
openSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.showDialog(new JLabel(), "选择");
filePathText.setText(jfc.getSelectedFile().toString());
}
});
//拖拽
new DropTarget(filePathText, DnDConstants.ACTION_COPY_OR_MOVE, new DropTargetAdapter() {
@Override
public void drop(DropTargetDropEvent dtde) {
if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { //判断是否支持此文件的格式
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); //接受该文件
try {
@SuppressWarnings("unchecked")
List<File> list = (List<File>) dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
filePathText.setText(filePathText.getText() + list.get(list.size() - 1).getAbsolutePath() + "\r\n");
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
} else {
dtde.rejectDrop(); //拒绝该拖拽文件
}
}
});
}
}