• Java Swing笔记


    想到了解一下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); 其他

  • 相关阅读:
    啥是IOC ?啥是DI ?
    Spring是什么?
    Javaweb实训-宠物医院-社区宠物医院登陆页面
    Javaweb实训-宠物医院-社区宠物医院的页面样式
    Bootstrap基础学习(二)
    Bootstrap基础学习(一)
    常用的几种清除float浮动的方法
    jquery的each遍历方法
    正则总结RegExp
    OpenWrt编译到底脚本
  • 原文地址:https://www.cnblogs.com/liqipeng/p/7739840.html
Copyright © 2020-2023  润新知