记一次jframe小工具

重要类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package com.swing;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class MsgLoadFrame1 extends JFrame implements ActionListener {
/**
* @author Roy_70
* @date
*/
private static final long serialVersionUID = -1189035634361220261L;
JFrame mainframe;
JPanel panel;
//创建相关的Label标签
JLabel infilepath_label = new JLabel("请输入xxxxxx多个以分号;隔开(或者每行一个版本,前边加个;分号):");
//创建相关的文本域
//创建滚动条以及输出文本域
JScrollPane jscrollPane;
JScrollPane jscrollPaneIn;
JTextArea intext_textarea = new JTextArea();
//JTextArea outtext_textarea = new JTextArea();
JTextPane outtext_textarea = new JTextPane();
//创建按钮
JButton start_button = new JButton("开始查询");

public void show(){
mainframe = new JFrame("根据xxxxxx版本查询下载路径ver-1.0");
// Setting the width and height of frame
mainframe.setSize(575, 770);
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.setResizable(false);//固定窗体大小

Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包
Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸
int screenWidth = screenSize.width/2; // 获取屏幕的宽
int screenHeight = screenSize.height/2; // 获取屏幕的高
int height = mainframe.getHeight(); //获取窗口高度
int width = mainframe.getWidth(); //获取窗口宽度
mainframe.setLocation(screenWidth-width/2, screenHeight-height/2);//将窗口设置到屏幕的中部
//窗体居中,c是Component类的父窗口
//mainframe.setLocationRelativeTo(c);
Image myimage=kit.getImage("resourse/hxlogo.gif"); //由tool获取图像
mainframe.setIconImage(myimage);
initPanel();//初始化面板
mainframe.add(panel);
mainframe.setVisible(true);
}
/* 创建面板,这个类似于 HTML 的 div 标签
* 我们可以创建多个面板并在 JFrame 中指定位置
* 面板中我们可以添加文本字段,按钮及其他组件。
*/
public void initPanel(){
LayoutManager lm = new FlowLayout(FlowLayout.LEFT);
this.panel = new JPanel();
panel.setLayout(null);
//this.panel = new JPanel(new GridLayout(3,2)); //创建3行3列的容器
/* 这个方法定义了组件的位置。
* setBounds(x, y, width, height)
* x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。
*/
infilepath_label.setBounds(10,20,550,25);
this.panel.add(infilepath_label);

intext_textarea.setEditable(true);
intext_textarea.setFont(new Font("标楷体", Font.BOLD, 12));
intext_textarea.setLineWrap(true);
jscrollPaneIn = new JScrollPane(intext_textarea);
jscrollPaneIn.setBounds(10,40, 550, 200);
jscrollPaneIn.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jscrollPaneIn.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.panel.add(jscrollPaneIn);



start_button.setBounds(10,240,160,25);
this.panel.add(start_button);

outtext_textarea.setEditable(true);
outtext_textarea.setFont(new Font("标楷体", Font.BOLD, 12));
outtext_textarea.setContentType("text/html");
outtext_textarea.setEditable(false);
outtext_textarea.addHyperlinkListener(new HyperlinkListener() {

public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
String command = "explorer.exe "
+ e.getURL().toString();
Runtime.getRuntime().exec(command);
} catch (Exception ex) {
ex.printStackTrace();
System.err.println("connection error");
}
}

}

});
// outtext_textarea.setLineWrap(true);
// outtext_textarea.setWrapStyleWord(true);
jscrollPane = new JScrollPane(outtext_textarea);
jscrollPane.setBounds(10,290, 550, 440);
this.panel.add(jscrollPane);
//增加动作监听
start_button.addActionListener(this);
}
/**
* 单击动作触发方法
* @param event
*/
@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
System.out.println(event.getActionCommand());
if(event.getSource() == start_button){
//确认对话框弹出
try {
String text = intext_textarea.getText();
CloseableHttpClient client = HttpClients.createDefault();
String url = "http://xxxxxx/otapublish/getOtaPath";
String token = "Basic xxxxxx";
String token1 = "Basic xxxxxx";
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Authorization", token1);
httpPost.setHeader("Content-type",
"application/json;charset=UTF-8");
httpPost.setHeader("User-Agent",
"//Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
JSONObject requestParam = new JSONObject();
String[] split = text.split(";");
java.util.List<String> otaVersions = Arrays.asList(split);
java.util.List<String> otaVersions0 = new ArrayList<>();
otaVersions.forEach(e->{otaVersions0.add(replaceBlank(e));});
requestParam.put("xxxxxx",otaVersions0);
String s = JSON.toJSONString(requestParam);
System.out.println(s);
StringEntity stringEntity = new StringEntity(s,
"utf-8");
stringEntity.setContentEncoding("utf-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
//执行请求操作,并拿到结果(同步阻塞)
CloseableHttpResponse response;
JSONObject jso = new JSONObject();

response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
String body = EntityUtils.toString(entity);
if(StringUtils.isNotEmpty(body)){
com.alibaba.fastjson.JSONObject json=(com.alibaba.fastjson.JSONObject)JSON.parse(body);
Object result = json.get("result");
if(result instanceof String){
Object message = json.get("message");
Object code = json.get("code");
outtext_textarea.setText("出错。。请检查参数。。。"+"<br/>"+result+"<br/>"+code+"<br/>"+message);
return;
}
StringBuffer out = new StringBuffer();

out.append("<html><body>");
out.append("<div>"+new Date().toLocaleString()+"</div>");
JSONArray jsonArray = (JSONArray)result;
jsonArray.stream().forEach(e->{
com.alibaba.fastjson.JSONObject e0 = (com.alibaba.fastjson.JSONObject) e;
out.append("<div>"+"源版本:"+"</div>");
out.append("<div >"+e0.getString("xxxxxx")+"</div>");
out.append("<div>"+"下载路径:"+"</div>");
String otaPath = e0.getString("xxxxxx");
if(StringUtils.isEmpty(otaPath)){
out.append("<div></div>");
}else {
out.append("<div>" + "<a href='" + otaPath + "'>" + otaPath + "</a>" + "</div>");
//out.append(e0.getString("otaPath"));
}
});
out.append("</body></html>");
outtext_textarea.setText(out.toString());
}
} catch (IOException e) {
outtext_textarea.setText("查询失败。。。");
}








}else{
}
}
public static String replaceBlank(String str) {
String dest = "";
if (str!=null) {
Pattern p = Pattern.compile("\t|\r|\n");
Matcher m = p.matcher(str);
dest = m.replaceAll("");
}
return dest;
}
/**
* 将字符串复制到剪切板。
*/
public static void setSysClipboardText(String writeMe) {
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable tText = new StringSelection(writeMe);
clip.setContents(tText, null);
}
public void windowClosed(WindowEvent arg0) {
System.exit(0);
}
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
}

主函数

1
2
3
4
5
6
7
8
9
10
11
12
package com.swing;

/**
* Created by xxxxxx on 2019/11/29.
*/
public class Msgloadtest {
public static void main(String []args){
MsgLoadFrame1 f = new MsgLoadFrame1();
f.show();
}
}

maven配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.xxxxxx.xxxxxx.tools</groupId>
<artifactId>soup</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>new</name>
<description>Spring Cloud Service Demo</description>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>

<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
<!--json-lib-->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.swing.Msgloadtest</mainClass> <!-- 此处为主入口-->
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.swing.Msgloadtest</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>

exe4j配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="UTF-8"?>
<exe4j version="5.1" transformSequenceNumber="2">
<directoryPresets config="C:/Users/xxxxxx/Pictures" />
<application name="transTool" distributionSourceDir="C:/Users/xxxxxx/Desktop">
<languages>
<principalLanguage id="en" customLocalizationFile="" />
</languages>
</application>
<executable name="otaVersionDownloader" type="2" iconSet="true" iconFile="C:/Users/xxxxxx/Pictures/2f7c0e0928381f30511224e3a4014c086f06f0c2.ico" executableDir="." redirectStderr="true" stderrFile="error.log" stderrMode="overwrite" redirectStdout="false" stdoutFile="output.log" stdoutMode="overwrite" failOnStderrOutput="true" executableMode="1" changeWorkingDirectory="true" workingDirectory="." singleInstance="false" serviceStartType="2" serviceDependencies="" serviceDescription="" jreLocation="" executionLevel="asInvoker" checkConsoleParameter="false" globalSingleInstance="false" singleInstanceActivate="true" dpiAware="false" amd64="true">
<messageSet />
<versionInfo include="true" fileVersion="1.0.0.0" fileDescription="by xxxxxx" legalCopyright="Copyright (C) 2019-11-30 Free Software Foundation ftq.co" internalName="" productName="" companyName="ftq.co" productVersion="v1.0" />
</executable>
<splashScreen show="false" width="0" height="0" bitmapFile="" windowsNative="false" textOverlay="false">
<text>
<statusLine x="20" y="20" text="" fontSize="8" fontColor="0,0,0" bold="false" />
<versionLine x="20" y="40" text="version %VERSION%" fontSize="8" fontColor="0,0,0" bold="false" />
</text>
</splashScreen>
<java mainClass="com.swing.Msgloadtest" vmParameters="" arguments="" allowVMPassthroughParameters="true" preferredVM="" bundleRuntime="true" minVersion="1.8" maxVersion="1.8" allowBetaVM="false" jdkOnly="false">
<searchSequence>
<registry />
<envVar name="JAVA_HOME" />
<envVar name="JDK_HOME" />
</searchSequence>
<classPath>
<archive location="C:/Users/xxxxxx/git/SaveTheWorld/jframe/target/soup-1.0.0-jar-with-dependencies.jar" failOnError="false" />
</classPath>
<nativeLibraryDirectories />
<vmOptions />
</java>
<includedFiles />
<unextractableFiles />
</exe4j>

exe4j激活码

1
2
3
4
5
exe4j
前两个随便填
激活码
L-g782dn2d-1f1yqxx1rv1sqd