简体   繁体   English

为什么我的基于Swing的GUI应用程序没有响应?

[英]Why my Swing based GUI application is not responding?

I am trying to create my first GUI application using (Java + Eclipse + Swing). 我正在尝试使用(Java + Eclipse + Swing)创建我的第一个GUI应用程序。 This is my code: 这是我的代码:

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


public class HelloWorldSwing extends JFrame {

        JTextArea m_resultArea = new JTextArea(6, 30);

        //====================================================== constructor
        public HelloWorldSwing() {
            //... Set initial text, scrolling, and border.
            m_resultArea.setText("Enter more text to see scrollbars");
            JScrollPane scrollingArea = new JScrollPane(m_resultArea);
            scrollingArea.setBorder(BorderFactory.createEmptyBorder(10,5,10,5));

            // Get the content pane, set layout, add to center
            Container content = this.getContentPane();
            content.setLayout(new BorderLayout());
            content.add(scrollingArea, BorderLayout.CENTER);
            this.pack();
        }

        //============================================================= main
        public static void main(String[] args) {
            JFrame win = new HelloWorldSwing();
            win.setTitle("TextAreaDemo");
            win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            win.setVisible(true);
        }


}

The code was taken from here . 该代码是从这里获取的

When I run the application from Eclipse the expected window appears (So, it's good. I see what I want to see). 当我从Eclipse运行应用程序时,将出现预期的窗口(很好,我看到了我想要看到的)。 However, when I try to close the window or try to write something in the text area the program freezes. 但是,当我尝试关闭窗口或尝试在文本区域中写入内容时,程序将冻结。 The OS writes me that program is not responding (I try it on Ubuntu). 操作系统告诉我程序没有响应(我在Ubuntu上尝试了)。

Can anybody help me to find the reason of the problem? 有人可以帮助我找到问题的原因吗?

Thank you in advance for any help. 预先感谢您的任何帮助。

I'm sure this doesn't have to do with the code, as others have found the code runs just fine on their machines - which points to a machine specific issue. 我确信这与代码无关,因为其他人发现代码在他们的机器上运行得很好-这表明了特定于机器的问题。 From within Eclipse, make sure it is setup to use the expected JDK/JRE. 在Eclipse中,确保已设置为使用预期的JDK / JRE。 However, before worrying about how Eclipse is handling your situation, I'd run things by hand first - especially since you've got a very simple class. 但是,在担心Eclipse如何处理您的情况之前,我会先手动运行-尤其是因为您有一个非常简单的类。

I would check to ensure that you're using the expected compiler and runtime. 我将检查以确保您使用的是预期的编译器和运行时。 On Linux: 在Linux上:

which javac
which java

If they're both what you expect, do the following: 如果它们都是您所期望的,请执行以下操作:

javac HelloWorldSwing.java
java HelloWorldSwing 

If you get a similar problem, then you know it's not the Eclipse configuration and it's something else. 如果您遇到类似的问题,那么您就知道它不是Eclipse配置,而是其他配置。 If you're not using the latest JDK, upgrade to the latest. 如果您没有使用最新的JDK,请升级到最新的。 If you're already at the latest, it could be a display driver. 如果您已经是最新的,则可能是显示驱动程序。 Do other JAVA swing programs work on this computer? 其他JAVA swing程序在这台计算机上可以工作吗? I'm sure you could find some on the net, download an app already packaged as a jar and try running it. 我相信您可以在网上找到一些内容,下载已经打包为jar的应用程序,然后尝试运行它。

did you try using the eventdispatcherthread to view the JFrame? 您是否尝试使用eventdispatcherthread查看JFrame?

something like: 就像是:

public static void main(String[] args){
    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            createAndViewJFrame();
        }
    });
}

public void createAndViewJFrame(){
    JFrame win = new HelloWorldSwing();
    win.setTitle("TextAreaDemo");
    win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    win.setVisible(true);
}

then your frame would be shown by the swing dispatcher thread. 那么您的框架将由swing调度程序线程显示。

hope it helped, although im just guessing... 希望它有所帮助,尽管我只是在猜测...

Update: as commenters pointed you if**ed up the invokeLater() call. 更新:评论者指出您是否**调用invokeLater()。 I just edited this post to correct that. 我只是编辑了这篇文章以纠正它。 Thanx go to yishai & willcodejavaforfood for pointing it out! 谢谢去yishai&willcodejavaforfood指出这一点!

frank 坦率

You need to catch the exit event and respond with a System.exit( 0 ); 您需要捕获退出事件并使用System.exit( 0 );响应System.exit( 0 );

You should be able to find that in most swing examples online. 您应该能够在大多数在线示例中找到它。

wrong stuff... sorry... coffee... argh.... 错误的东西...对不起...咖啡...啊...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM