简体   繁体   English

Swing应用程序窗口未显示更改

[英]Swing Application window not showing changes

I'm making a java proyect using eclipse and the windowbuilder plugin. 我正在使用eclipse和windowbuilder插件制作Java proyect。 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. 我也想知道这是制作gui应用程序的最佳方法(使用“应用程序窗口”)还是有更好的方法。

EDIT: I add some images to show which is the problem 编辑:我添加一些图像以显示这是问题

Window when i execute: 我执行时的窗口:

我执行时的窗口

Window in windowbuilder: windowbuilder中的窗口:

windowbuilder中的窗口

You can set title using two ways: 您可以使用两种方式设置标题:

1.Set title using JFrame setTitle() method : frame.setTitle("My Application"); 1.使用JFrame setTitle()方法设置标题: frame.setTitle("My Application");

2.Set title using JFrame(String Title) constructor: frame = new JFrame("My Application"); 2.使用JFrame(String Title)构造函数设置标题: 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() 设置更改后,转到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. 编辑:从初始化方法中删除setTitle()。

Edit: If you are beginner you can try JavaFX. 编辑:如果您是初学者,可以尝试JavaFX。 It is also simple as Java Swing but has some more potential, better design options and stuff. 它与Java Swing一样简单,但是具有更多潜力,更好的设计选项和功能。 Its something I have tryed with when I was started Java learning. 开始学习Java时,我曾尝试过这种方法。

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. 只需尝试重新安装swing。

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

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