jar黑窗口无法关闭代码

创建一个类直接复制放入

package com.xxx.receivesignal.run;

import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;

public class GUIPrintStream extends PrintStream {

    private JTextComponent component;
    private StringBuffer sb = new StringBuffer();

    public GUIPrintStream(OutputStream out, JTextComponent component) {
        super(out);
        this.component = component;
    }

    public void write(byte[] buf, int off, int len) {
        final String message = new String(buf, off, len);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                sb.append(message);
                component.setText(sb.toString());
                if (sb.toString().length() > 3000) {
                    sb.replace(0, sb.toString().length() - 2000, "");
                }
            }
        });
    }

}

接着再放入启动类

package com.xxx.receivesignal.run;


import com.xxx.receivesignal.dao.DataDao;
import com.xxx.receivesignal.service.MotorDataService;
import com.xxx.receivesignal.service.PLCDataService;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.channels.FileLock;

public class MainForm extends JFrame {

    static Logger logger = Logger.getLogger(Run.class);
    ApplicationContext context;
    DataDao dataDao;
    PLCDataService plcDataService;
    MotorDataService motorDataService;

    private void initApplication() {
        context = new ClassPathXmlApplicationContext("classpath:conf/applicationContext.xml");
        dataDao = (DataDao) context.getBean("DataDao");
        plcDataService = (PLCDataService) context.getBean("PLCDataService");
        motorDataService = (MotorDataService) context.getBean("MotorDataService");
    }


    //public JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        //region 单例启动检查
        try {
            //获取锁定文件路径
            String lockPath = "/www/ReceiveSignalFlowLock.java";
            //Object obj = PropertyConfigurerUtil.getContextProperty("lockFilePath");
            //if (obj != null) {
            //    String lockValue = obj.toString();
            //    if (lockValue != null && !lockValue.isEmpty()) {
            //        lockPath = lockValue;
            //    }
            //}
            //创建lock.java文件
            File file = new File(lockPath);
            if (!file.exists()) {
                file.createNewFile();
            }
            //判断是否锁定
            FileLock lck = new FileOutputStream(lockPath).getChannel().tryLock();
            if (lck == null) {
                //锁定失败,表示被其他程序实例占用,不再启动
                System.out.println("A previous instance is already running, so this instance will exit.");
                System.exit(1);
                return;
            }
        } /*catch (IOException e) {
            logger.error(e);
            e.printStackTrace();
        }*/ catch (Exception e2) {
            //logger.error(e2.getMessage());
            logger.error(e2);
            e2.printStackTrace();
        }
        //endregion

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainForm window = new MainForm();
                    window.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public MainForm() {
        initialize();
        initApplication();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        //frame = new JFrame();
        this.setBounds(100, 100, 900, 600);
        this.setTitle("窗口名称");
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        JTextArea textArea = new JTextArea();
        this.getContentPane().add(textArea, BorderLayout.CENTER);
        // 文本域不可编辑
        textArea.setEditable(false);
        // 设置文本域背景色
        textArea.setBackground(Color.pink);
        // 设置文本域字体大小
        textArea.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12));
        // 文本域自动换行
        textArea.setLineWrap(true);
        // 文本域设置为有滚动条可滚动框
        textArea.setWrapStyleWord(true);
        // 将输出到控制台的内容输出到文本域中
        System.setOut(new GUIPrintStream(System.out, textArea));
        //
        //this.setVisible(true);
    }

}
原文地址:https://www.cnblogs.com/zhangweibin/p/12712709.html