Java Swing笔记

时间:2022-07-27 01:47:28

想到了解一下GUI主要是想用来做点小工具,记录一些笔记。

文本框自动换行和滚动条

private static JTextArea addJTextArea(JPanel panel, int x, int y, int width, int height, String text) {
JTextArea jsText = new JTextArea(text);
jsText.setLineWrap(true);
jsText.setWrapStyleWord(true);
JScrollPane jsScrollPane = new JScrollPane(jsText);
jsScrollPane.setBounds(x, y, width, height);
//分别设置水平和垂直滚动条自动出现
jsScrollPane.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
panel.add(jsScrollPane);
return jsText;
}

用yuicompressor做个js压缩工具

pom

<dependency>
<groupId>com.yahoo.platform.yui</groupId>
<artifactId>yuicompressor</artifactId>
<version>2.4.8</version>
</dependency>

执行压缩并显示在文本框:

compressJsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
ByteArrayInputStream stream = new ByteArrayInputStream(jsText.getText().getBytes()); JavaScriptCompressor jscompressor = new JavaScriptCompressor(new BufferedReader(new InputStreamReader(stream)), new ErrorReporter() {
@Override
public void warning(String s, String s1, int i, String s2, int i1) {
jsResultText.setText(String.format("warning: %s,%s,%s,%s,%s", s, s1, i, s2, i1));
} @Override
public void error(String s, String s1, int i, String s2, int i1) {
jsResultText.setText(String.format("error: %s,%s,%s,%s,%s", s, s1, i, s2, i1));
} @Override
public EvaluatorException runtimeError(String s, String s1, int i, String s2, int i1) {
jsResultText.setText(String.format("runtimeError: %s,%s,%s,%s,%s", s, s1, i, s2, i1));
return null;
}
}); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); int linebreakpos = -1;
boolean munge = true;
boolean verbose = false;
boolean preserveAllSemiColons = false;
boolean disableOptimizations = false; jscompressor.compress(bufferedWriter, linebreakpos, munge, verbose, preserveAllSemiColons, disableOptimizations);
bufferedWriter.flush(); jsResultText.setText(new String(outputStream.toByteArray())); } catch (IOException e1) {
jsResultText.setText(e1.toString());
}
}
});

打包jar

<build>
<plugins> <plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!-- 此处指定main方法入口的class -->
<mainClass>com.xxx.xxx</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin> </plugins>
</build>

参考

  1. java中TextArea和JTextArea的自动换行和滚动条

  2. [Maven]Maven构建可执行的jar包(包含依赖jar包)

  3. swing教程

  4. Java Swing 介绍

  5. 容器(JFrame); 其他