简体   繁体   中英

Swing Application window not showing changes

I'm making a java proyect using eclipse and the windowbuilder plugin. After finishing the logic I've created an Application Window and execute the project, everything is fine. But now i want to modify the window, so i change the title for example. In the visual view, i can see these changes, but when i execute the application, there are no changes, it's always the same window. This is the code:

public class Application {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Application window = new Application();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Application() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("My Application");
    }

}

I've just added one line so I don't know what could be wrong. I also want to know if this is the best way to make a gui application (using Application Window) or if there are better ways.

EDIT: I add some images to show which is the problem

Window when i execute:

我执行时的窗口

Window in windowbuilder:

windowbuilder中的窗口

You can set title using two ways:

1.Set title using JFrame setTitle() method : frame.setTitle("My Application");

2.Set title using JFrame(String Title) constructor: frame = new JFrame("My Application");

According to my opinion both ways work to set title but still if you are struggling with first option, you can try second option. May be this code works for you.

private void initialize() {
    frame = new JFrame("My Application");
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

After setting changes, go frame.dispose()

and after that You can

frame.repaint();
frame.revalidate();

Please try this:

               try {
                    Application window = new Application();
                    window.frame.setVisible(true);
                    window.frame.setTitle("My Application");
                } catch (Exception e) {
                    e.printStackTrace();

And let me know the result.

Edit: remove setTitle() from initialize method.

Edit: If you are beginner you can try JavaFX. It is also simple as Java Swing but has some more potential, better design options and stuff. Its something I have tryed with when I was started Java learning.

I think that you should delete your swing and install it again. Because any solution is not working and there is no other reason not to. Just try to reinstall swing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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