简体   繁体   English

涉及GUI时,程序立即终止

[英]Program terminates immediately when GUI's are involved

When trying to run the following code in Eclipse, it terminates almost immediately with no message (only that the exit value is -1073740940), yet any java code that doesn't contain GUI elements runs fine. 尝试在Eclipse中运行以下代码时,它几乎立即终止,没有消息(仅退出值为-1073740940),但是任何不包含GUI元素的Java代码都可以正常运行。 When run with the debugger it reaches the 'new Runnable' line and then terminates, but the GUI window never shows up. 使用调试器运行时,它到达“新的Runnable”行,然后终止,但GUI窗口从不显示。 GUIs were working fine a while ago but they stopped working at some point and I have no idea why. GUI之前工作得很好,但是它们在某个时候停止工作了,我不知道为什么。

import java.awt.*;
import javax.swing.*;

public class Test {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Wat wat = new Wat("Test");
                wat.init();
                System.out.println("wat");
            }
        });
    }
}

@SuppressWarnings("serial")
class Wat extends JFrame {
    public Wat(String title) {
        super(title);
    }

    public void init() {
        JPanel p = new JPanel();
        this.setContentPane(p);

        p.add(new JLabel("Why?"));

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600, 500);
        this.setVisible(true);
    }
}

The code for main method should look like this main方法的代码应如下所示

public class Test {
    public static void main(String[] args) {
        Wat wat = new Wat("Test");
        wat.init();
        System.out.println("wat");
    }
}

Simply, run the UI code on the main thread. 只需在主线程上运行UI代码。 Use separate threads for long running operations started from UI. 使用单独的线程进行从UI开始的长时间运行的操作。 See tutorial for SwingWorker . 请参阅SwingWorker教程。

I switched a couple of lines around in the Wat class init method and added a serialVersionUID to the Wat class. 我在Wat类的init方法中切换了几行,并向Wat类添加了serialVersionUID。

This code brings up a GUI every time I run it. 每次我运行此代码时,都会启动一个GUI。

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Test {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Wat wat = new Wat("Test");
                wat.init();
                System.out.println("wat");
            }
        });
    }
}

class Wat extends JFrame {

    private static final long   serialVersionUID    = 
            8993350484858673399L;

    public Wat(String title) {
        super(title);
    }

    public void init() {
        JPanel p = new JPanel();
        p.add(new JLabel("Why?"));

        this.setContentPane(p);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600, 500);
        this.setVisible(true);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Eclipse Mars中的Java程序是否立即终止而没有输出? - Java program in Eclipse Mars, immediately terminates with no output? 程序在 main 函数内部调用时终止 - Program terminates when called inside main function 程序终止时,用`exec`终止进程运行 - Terminate process run with `exec` when program terminates 批处理文件在执行Java程序后立即终止(如果从另一个Java程序调用了批处理文件) - Batch file immediately terminates after executing a Java program (if batch file is called from another Java program) 如何修复无限弹出多个窗口然后突然终止程序的 GUI 应用程序的输出? - How to fix the output of a GUI application where multiple windows pop up infinitely and then abruptly terminates program? Java程序终止时无法关闭文件是否有任何危害? - Is there any harm in failing to close a file when a Java program terminates? 使用 delayElements 时程序在 Flux 完成之前终止 - Program terminates before Flux would finish when using delayElements Java读取/写入双精度数组以在程序终止时进行保存 - Java Read/Write a double array to save when program terminates 启动GUI程序时无界面 - No interface when starting GUI program 程序在eclipse中终止 - Program terminates in eclipse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM