开发者

Why my Swing based GUI application is not responding?

I am trying to create my first GUI application using (Java + Eclipse + Swing). 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 bor开发者_开发百科der.
            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). 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).

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. 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.

I would check to ensure that you're using the expected compiler and runtime. On 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. If you're not using the latest JDK, upgrade to the latest. If you're already at the latest, it could be a display driver. Do other JAVA swing programs work on this computer? I'm sure you could find some on the net, download an app already packaged as a jar and try running it.


did you try using the eventdispatcherthread to view the 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.

hope it helped, although im just guessing...

Update: as commenters pointed you i f**ed up the invokeLater() call. I just edited this post to correct that. Thanx go to yishai & willcodejavaforfood for pointing it out!

frank


You need to catch the exit event and respond with a System.exit( 0 );

You should be able to find that in most swing examples online.

wrong stuff... sorry... coffee... argh....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜