Java Swing应用程序上的Eclipse崩溃

Java Swing应用程序上的Eclipse崩溃,java,eclipse,Java,Eclipse,我试图创建一个Swing应用程序,以便在大型应用程序的调试/开发阶段查看JSON结构。方法很简单—(1)以深度优先模式遍历JSON树(b)为每个节点创建相应的DefaultMutableTreeNode(c)将这些节点粘贴到JTree中,以及(d)将JTree放入JScrollPane中。适用于大小不超过几KB的JSON文件/字符串 问题是:当在大小为100 KB的JSON文件上使用时,我可以在屏幕上看到GUI,但在那之后,Eclipse崩溃并显示以下消息。我怀疑原因是我创建GUI的方式。这是E

我试图创建一个Swing应用程序,以便在大型应用程序的调试/开发阶段查看JSON结构。方法很简单—(1)以深度优先模式遍历JSON树(b)为每个节点创建相应的DefaultMutableTreeNode(c)将这些节点粘贴到JTree中,以及(d)将JTree放入JScrollPane中。适用于大小不超过几KB的JSON文件/字符串

问题是:当在大小为100 KB的JSON文件上使用时,我可以在屏幕上看到GUI,但在那之后,Eclipse崩溃并显示以下消息。我怀疑原因是我创建GUI的方式。这是EclipseNeon的情况,Java8运行在MacOS10.14.6上。我尝试了一个修改-从Swing invokeLater线程内部使用createGUI()方法(在代码中注释掉的行中注释),但结果是相同的


有人能帮助使用正确的机制来使用SwingWorker线程吗?我已经很久很久没有使用Swing了,可能会错过一些最近的发展。另一个令人困惑的原因是,我可以从eclipse导出项目来创建一个可执行的jar,当从命令行(shell提示符)调用时,应用程序运行良好

下面附上源代码。要将它放在您自己的IDE中并运行它,唯一需要的外部库是“javajson.jar”。谢谢你的帮助

进程:eclipse[50201]路径:
/Applications/Eclipse.app/Contents/MacOS/Eclipse标识符:
org.eclipse.platform.ide版本:4.6.0 (4.6.0.I20160606-1100)代码类型:X86-64(本机)父级 过程:???[1] 负责人:eclipse[50201]用户 身份证号码:1117050623

日期/时间:2019-10-06 11:19:55.970-0700操作系统版本:
Mac OS X 10.14.6(18G95)报告版本:12桥操作系统版本: 3.6(16P6571)匿名UUID:44A65B77-4733-DCB5-94D3-A96597022662

睡眠/唤醒UUID:0653A25E-D236-44CF-86ED-DB63F08A987A

启动后的唤醒时间:560000秒唤醒后的时间:1900 秒

系统完整性保护:已启用

崩溃线程:0调度队列:com.apple.main-Thread

异常类型:EXC\U坏访问(SIGABRT)异常代码:
内核地址在0x000000018e40cf7b处无效异常注意:
EXC_尸体_通知

0x18e40cf7b附近的VM区域: MALLOC_TINY 0000000 18e100000-0000000 18e40000[3072K]rw-/rwx SM=PRV
--> 映射文件0000000 18E464000-0000000 18E46C000[32K]r--/r-x SM=PRV Object_id=3fca339b

特定于应用程序的信息:CGBlt\u copyBytes:缓冲区检查:
调用了abort()

线程0崩溃::调度队列:com.apple.main-Thread 0
libsystem\u kernel.dylib 0x00007fff607b82c6\u pthread\u kill+10 1 libsystem_pthread.dylib 0x00007fff60873bf1 pthread_kill+ 284 2 libsystem_c.dylib 0x00007fff607226a6中止+127 3 libjvm.dylib 0x0000000108460181操作系统::中止(bool) +25 4 libjvm.dylib 0x000000010858479e VMError::report_and_die()+2304 5 libjvm.dylib
0x0000000108461dfe JVM_句柄_bsd_信号+1131 6 libjvm.dylib
0x000000010845e00b信号处理器(int,siginfo*,void*)+47 7
libsystem_platform.dylib 0x00007fff60868b5d_sigtramp+29 8
??? 0x00007fff8906e670 sp3\U gfx10\U a\U操作数 +818512 9 com.apple.CoreGraphics 0x00007fff34b95391 CGBlt_copyBytes+385 10 com.apple.CoreGraphics
0x00007fff34b722b4 argb32_图像+5324 11 com.apple.CoreGraphics
0x00007fff34b70ddf ripl_标记+22 12 com.apple.CoreGraphics
0x00007fff34b70db4 riplayerBlImage+1018 13 com.apple.CoreGraphics 0x00007fff34b706f6 ripc_渲染+228 14 com.apple.CoreGraphics 0x00007fff34b64371 ripc_DrawImage+812 15 com.apple.CoreGraphics
0x00007fff34b64036 CGContextDelegateDrawImage+41 16 com.apple.AppKit 0x00007fff31f25397 __backing_store_DrawImage_block_invoke+55 17 com.apple.AppKit 0x00007fff31f24260 backing_store_delegate+893 18 com.apple.AppKit 0x00007fff32240665背衬_存储_DrawImage.llvm.2329424099933660171+ 509 19 com.apple.CoreGraphics 0x00007fff34b64036 CGContextDelegateDrawImage+41 20 com.apple.AppKit
0x00007fff322414e6 backing_store_DrawWindowContents.llvm.2329424099933660171+1075 21 com.apple.CoreGraphics 0x00007fff34be1e52 CGContextDelegateDrawWindowContents+59 22 com.apple.SkyLight
0x00007fff59a64507 SLContextCopyWindowContentsToRect+163 23 com.apple.AppKit 0x00007fff32153858\u NXScroll+1463 24 libswt-pi-cocoa-4623.jnilib 0x0000000159937139 Java_组织_eclipse_swt_内部_cocoa_OS_NSCopyBits+18725???
0x0000000120018427 0+4831937575 26???
0x0000000120007ffd 0+4831870973 27???
0x0000000120007ffd 0+4831870973 28???
0x0000000120007ffd 0+4831870973 29???
0x0000000120007ffd 0+4831870973 30???
0x0000000120007ffd 0+4831870973 31???
0x0000000120008042 0+4831871042 32???
0x0000000120008042 0+4831871042 33???
0x0000000120007ffd 0+4831870973 34???
0x0000000120007ffd 0+4831870973 35???
0x0000000120007ffd 0+4831870973 36???
0x0000000120008042 0+4831871042 37?
0x0000000120007d80 0+4831870336 38???
0x0000000120a39674 0+4842559092 39???
0x0000000120008042 0+4831871042 40?
0x0000000120007ffd 0+4831870973 41???
0x0000000120007e54 0+4831870548 42???
0x0000000120007ffd 0+4831870973 43???
0x0000000120008042 0+4831871042 44?
0x000000012
package com.efe.fqa;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonView extends JSplitPane {

    private static final long serialVersionUID = 0xEFECAFE2BL;
    private JSONObject job;
    private String rootName;

    public JsonView (String title, String jsonAsString) throws JSONException {
        this (title, new JSONObject(jsonAsString));
    }

    public JsonView (String rootNodeName, JSONObject job)  throws JSONException {
        super (HORIZONTAL_SPLIT);
        rootName = rootNodeName;
        this.job = job;
//  }
//  
//  public void createGUI () throws JSONException {
        add (getTextPane (job), LEFT);
        add (getTreePane (rootName, job), RIGHT);
    }

    private JScrollPane getTextPane (JSONObject job) throws JSONException {
        JTextArea ta = new JTextArea(job.toString(4));
        JScrollPane sp = new JScrollPane (ta);
        sp.setPreferredSize(new Dimension(720,400));
        return sp;
    }

    private JScrollPane getTreePane (String title, JSONObject job) throws JSONException {
        int n = job.length();
        DefaultMutableTreeNode jsonRoot=new DefaultMutableTreeNode(title + " (" + n + ")");
        expandUnitTree (jsonRoot, job);
        JTree jsonTree = new JTree(jsonRoot);
        JScrollPane sp = new JScrollPane (jsonTree);
        sp.setPreferredSize(new Dimension(720,400));
        return sp;
    }

    private void expandArrayTree (DefaultMutableTreeNode parent, JSONArray jarray) throws JSONException {
        int n = jarray.length();
        for (int i = 0 ; i < n ; i++) {
            DefaultMutableTreeNode jsonNode = new DefaultMutableTreeNode("[" + i + "]");
            Object child = jarray.get(i);
            System.out.println ("Child class: " + child.getClass().getName());
            if (child instanceof JSONArray) {
                expandArrayTree(jsonNode, jarray.getJSONArray(i));
            } else if (child instanceof JSONObject) {
                expandUnitTree(jsonNode, jarray.getJSONObject(i));
            } else if (child instanceof String) {
                jsonNode.add (new DefaultMutableTreeNode ("\"" + child + "\""));
            } else {
                jsonNode.add (new DefaultMutableTreeNode (child.toString()));
            }
            parent.add(jsonNode);
        }
    }

    private void expandUnitTree (DefaultMutableTreeNode parent, JSONObject job) throws JSONException {
        //JSONArray names = job.names();
        //System.out.println ("[expandUnitTree] names has " + names.length() + " elements");
        //for (int i = 0 ; i < names.length() ; i++)
        System.out.println (job);
        Iterator<?> keys = job.keys();
        List<String> jobKeys = new ArrayList<String>();
        while (keys.hasNext()) {
            jobKeys.add(keys.next().toString());
            //System.out.println (keys.next());
        }
        Collections.sort(jobKeys);
        for (String jsonKey : jobKeys) {
            Object child = job.get(jsonKey);
            Class<?> cls = child.getClass();
            System.out.println ("\t" + jsonKey + " -> " + cls.getName());
            DefaultMutableTreeNode jsonNode = null;
            if (job.isNull(jsonKey)) {
                jsonNode = new DefaultMutableTreeNode("\"" + jsonKey + "\" : null");
            } else if (child instanceof JSONArray) {
                JSONArray jsonChild = job.getJSONArray(jsonKey);
                int n = jsonChild.length();
                jsonNode = new DefaultMutableTreeNode("\"" + jsonKey + "\" [" + n + "]");
                expandArrayTree (jsonNode, jsonChild);
            } else if (child instanceof JSONObject) {
                JSONObject jsonChild = job.getJSONObject(jsonKey);
                int n = jsonChild.length();
                jsonNode = new DefaultMutableTreeNode("\"" + jsonKey + "\" (" + n + ")");
                expandUnitTree (jsonNode, jsonChild);
            } else if (child instanceof Boolean) {
                boolean bool = job.getBoolean(jsonKey);
                jsonNode = new DefaultMutableTreeNode("\"" + jsonKey + "\" : " + bool);
            } else if (child instanceof Integer) {
                int iNumber = job.getInt(jsonKey);
                jsonNode = new DefaultMutableTreeNode("\"" + jsonKey + "\" : " + iNumber); 
            } else if (child instanceof Double) {
                double dNumber = job.getDouble(jsonKey);
                jsonNode = new DefaultMutableTreeNode("\"" + jsonKey + "\" : " + dNumber);
            } else {
                String str = job.getString(jsonKey);
                jsonNode = new DefaultMutableTreeNode("\"" + jsonKey + "\" : \"" + str + "\"");
            }
            parent.add(jsonNode);
        }
    }

    public static JSONObject readJsonFile (File file) throws IOException, JSONException {
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader (new FileReader (file));
        for (String s = br.readLine() ; s != null ; s = br.readLine())
            sb.append(s);
        br.close();
        return new JSONObject(sb.toString());
    }

    public static void main(String[] args) {
        JFileChooser fc = new JFileChooser(System.getProperty("user.home"));
        if (fc.showOpenDialog(new JFrame()) == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            try {
                JSONObject job = readJsonFile (file);
                String fileName = file.getName();
                JFrame fr = new JFrame ("JsonView: " + file.getAbsolutePath());
                JsonView jv = new JsonView ("[JsonView] File: " + fileName, job);
                fr.getContentPane().add(jv);
                fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
//                      try {
//                          jv.createGUI();
                            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                            int height = screenSize.height;
                            int width = screenSize.width;
                            fr.pack();
                            Dimension pfSize = fr.getSize();
                            fr.setLocation((width - pfSize.width)/2, (height - pfSize.height)/2 - 200);
                            fr.setVisible(true);
//                      } catch (JSONException jex) {
//                          jex.printStackTrace();
//                      }
                    }
                });

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}