防止主 UI 从子线程崩溃

发布于 2024-12-06 19:08:59 字数 2863 浏览 0 评论 0 原文

我有这样的代码:

   class FinalUI1 extends javax.swing.JFrame 
     {
       //do something 
          Thread t;
        try {
            t = new Thread(new PcapTool(null));
            t.start();
           } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           }
       // do something

     }

     class PcapTool extends ApplicationFrame implements Runnable
  {
     //do something
     public void run() {
        Display Graph based on above classes input
         }
   }

有一个主 UI 窗口,每当用户单击按钮时,都会在单独的窗口中生成新图表。

我想在用户单击 FinalUI1 类中的按钮时显示图形,但是当我关闭任何生成的图形时,整个 UI 崩溃,一切都消失了。我想保留主用户界面并关闭用户选择关闭的特定图表。我想到,由于 UI 位于主线程上,并且我可以在新线程中生成新图形,因此如果我执行此操作并关闭子线程之一,则主 UI 应该仍在运行。


附加代码:

public class PcapTool extends ApplicationFrame { 
public static String domainChoice, domainConcatenate="";;
public static XYSeries series1;
public static XYSeriesCollection dataset=null;
public static XYSeries series2;
public static PacketInfo resPacketObject;
public static Hashtable<String, Object> DomainNameTable=new Hashtable<String, Object>();
public static String[] hasArray=new String[100];
public static JFreeChart chart;
public static String customTitle = " ";
public  ArrayList<Double> dataNumberList=new ArrayList<Double>(); 
public static String[]dataUsage;
public static String[]timeArrival,txRxTag;
private static final long serialVersionUID = 1L;
public PcapTool(final String title) throws InterruptedException {
    super(title);
    IntervalXYDataset dataset = createDataset();
    JFreeChart chart = createChart(dataset);
    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(2000,1000));//(width,height) of display
    setContentPane(chartPanel);
}
public IntervalXYDataset createDataset() throws InterruptedException {
      // add Series 1 and Series 2
    }
    dataset= new XYSeriesCollection(series1);
    dataset.addSeries(series2);
    dataset.setIntervalWidth(0.05);//set width here
    return dataset;
}
private JFreeChart createChart(IntervalXYDataset dataset) {
    final JFreeChart chart = ChartFactory.createXYBarChart(
            "Pcap Analysis Tool\n Domain: "+domainConcatenate,
            "Time (Seconds)", 
            false,
            "Data Usage (bytes)", 
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
            );
    return chart;    
}
public static void main(final String[] args) throws InterruptedException {

    final PcapTool demo = new PcapTool("PCAP Analysis");
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);
    System.out.println("domain: "+dropBoxUserValue);
}
}

I have this code:

   class FinalUI1 extends javax.swing.JFrame 
     {
       //do something 
          Thread t;
        try {
            t = new Thread(new PcapTool(null));
            t.start();
           } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           }
       // do something

     }

     class PcapTool extends ApplicationFrame implements Runnable
  {
     //do something
     public void run() {
        Display Graph based on above classes input
         }
   }

There is a Main UI window and new graphs are generated in separate window whenever user clicks a button.

I want to display graphs, when user clicks button in FinalUI1 class, but when I close any of generated graphs, the whole UI collapses, everything is gone. I want to retain the main UI and shut down the particular graph which user chose to close. It came to my mind that, since UI is on main thread and I can spawn new graphs in new thread, if I do this and close one of child threads, the main UI should still be running.


Additional code:

public class PcapTool extends ApplicationFrame { 
public static String domainChoice, domainConcatenate="";;
public static XYSeries series1;
public static XYSeriesCollection dataset=null;
public static XYSeries series2;
public static PacketInfo resPacketObject;
public static Hashtable<String, Object> DomainNameTable=new Hashtable<String, Object>();
public static String[] hasArray=new String[100];
public static JFreeChart chart;
public static String customTitle = " ";
public  ArrayList<Double> dataNumberList=new ArrayList<Double>(); 
public static String[]dataUsage;
public static String[]timeArrival,txRxTag;
private static final long serialVersionUID = 1L;
public PcapTool(final String title) throws InterruptedException {
    super(title);
    IntervalXYDataset dataset = createDataset();
    JFreeChart chart = createChart(dataset);
    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(2000,1000));//(width,height) of display
    setContentPane(chartPanel);
}
public IntervalXYDataset createDataset() throws InterruptedException {
      // add Series 1 and Series 2
    }
    dataset= new XYSeriesCollection(series1);
    dataset.addSeries(series2);
    dataset.setIntervalWidth(0.05);//set width here
    return dataset;
}
private JFreeChart createChart(IntervalXYDataset dataset) {
    final JFreeChart chart = ChartFactory.createXYBarChart(
            "Pcap Analysis Tool\n Domain: "+domainConcatenate,
            "Time (Seconds)", 
            false,
            "Data Usage (bytes)", 
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
            );
    return chart;    
}
public static void main(final String[] args) throws InterruptedException {

    final PcapTool demo = new PcapTool("PCAP Analysis");
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);
    System.out.println("domain: "+dropBoxUserValue);
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑着哭最痛 2024-12-13 19:08:59

我猜测此行为是由于您使用 JFrame 或类似的内容来显示子窗口,并且 JFrame 的 setDefaultCloseOperation 属性已设置为 JFrame.EXIT_ON_CLOSE ,其中当任何窗口关闭时,将导致 JVM 退出。

我认为您应该在对话框窗口(例如 JDialog)中显示它们,而不是在 JFrame 或 ApplicationFrame 中。另外,我还得担心你对线程的使用。所有 Swing 代码都需要在一个线程(EDT)上调用,而不是像上面那样在单独的线程上调用。当然,可以在后台线程中进行长时间运行的计算,但图表的实际显示和任何其他 Swing 调用必须在 EDT 上(除非您确定这些调用是线程安全的)。另一个选项是将 JFrame setDefaultCloseOperation 设置为 JFrame.DISPOSE_ON_CLOSE,但这些家伙仍然表现为对话框,在我看来,应该显示为对话框,JDialogs。

如果这没有帮助,请考虑发布一个最小的可编译和可运行的示例,该示例非常小,没有与当前问题无关的无关代码,并且可以演示您的问题,SSCCE

I'm guessing that this behavior is due to your using JFrames or something similar to display your child windows, and that the JFrame's setDefaultCloseOperation properties have been set to JFrame.EXIT_ON_CLOSE which will cause the JVM to exit when any of the windows close.

I think that you should show them in dialog windows such as a JDialog, not in a JFrame or ApplicationFrame. Also, I have to worry about your use of threading. All Swing code needs to be called on one single thread, the EDT, not separate threads as you may be doing above. Sure, do long-running calculations in a background thread, but the actual display of the chart and any other Swing calls must be on the EDT (unless you know for certain that the calls are thread-safe). The other option is to set the JFrame setDefaultCloseOperation to JFrame.DISPOSE_ON_CLOSE, but still these guys are behaving as dialogs and in my mind, should be shown as dialogs, JDialogs.

If this doesn't help, consider posting a minimal compilable and runnable example that is very small, has no extraneous code unrelated to the problem at hand, and that demonstrates your problem, an SSCCE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文