将setVisible()函数放在函数的开头是不同的当我把它放在函数的末尾? [英] Does placing setVisible() function in the beginning of the function be different when I placed it at the end of that function?

查看:247
本文介绍了将setVisible()函数放在函数的开头是不同的当我把它放在函数的末尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触Java GUI编程,当我在 setVisible()函数中放置 setVisible()函数时,我遇到的问题是我的面板中的组件丢失了构造函数调用的函数的开头,但它在结束时工作正常。请参阅下面的代码:

I'm just new to Java GUI Programming and I'm having a problem that the components inside my panel is missing when I place the setVisible()function at the beginning of the function called by the constructor but it works fine when it is at the end. See code below:

public static void main(String[] args) 
{
    new MainClass();
}

public MainClass()
{ 
    setFrame();
}

private void setFrame()
{
    JFrame frame = new JFrame();

    frame.setSize(400,400);
    frame.setResizable(false);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   // Some area where the object of my components inside the panel is created and initialized.
   // If I just place a label and a button, it will appear on the panel. However if I add the JTextArea, all the components in my panel is gone. Just like the code below.

    textArea1 = new JTextArea(20,34);
    textArea1.setWrapStyleWord(true);
    textArea1.setLineWrap(true);
    JScrollPane scroll = 
            new JScrollPane(textArea1, 
                    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    panel.add(scroll);
    frame.add(panel);
    // Works fine when setVisible(true); it placed here.
}

关于放置 setVisible()函数到方法的开头或结尾。

What could the problem possibly be with regard to placing the setVisible() function to the beginning or to the end of the method.

推荐答案

已经在评论和其他答案中指出:

As already pointed out in the comments and the other answer:

你应该在 setVisible(true)在添加完所有组件后结束。

You should call setVisible(true) at the end, after all components have been added.

这不能直接回答您的问题。您的问题的答案是:是的,它会产生影响。如果在添加所有组件之前调用 setVisible ,则可能在某些情况下使用某些程序在某些PC上运行某些Java版本,某些操作系统 - 但始终必须预期它在某些情况下可能无法正常工作。

This does not directly answer your question. The answer to your question is: Yes, it makes a difference. If you call setVisible before all compoents have been added, it may work, in some cases, with some programs, on some PCs, with some Java versions, with some operating systems - but you always have to expect that it may not work as expected in some cases.

你会发现几十个有关stackoverflow和其他地方的相关问题。这些问题的常见症状是某些组件未正确显示,然后在调整窗口大小时突然出现。 (调整窗口大小基本上会触发布局和重新绘制)。

You will find dozens of related questions here on stackoverflow and elsewhere. The usual symptoms of these problems are that some components are not displayed properly, and then suddenly appear when the window is resized. (Resizing a window basically triggers a layout and a repaint).

违反行为时,意外行为的可能性会增加Swing的线程规则。而且,从某种意义上说,你确实违反了Swing的线程规则:你应该总是在Event Dispatch Thread上创建GUI!

The likeliness of unexpected behavior is increased when you violate the threading rules of Swing. And, in some sense, you did violate the threading rules of Swing: You should always create the GUI on the Event Dispatch Thread!

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class SomeSwingGUI
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    // This method may (and will) only be called
    // on the Event Dispatch Thread
    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();

        // Add your components here        

        f.setVisible(true); // Do this last
    }
}






顺便说一下:Timothy Truckle在评论中指出你不应该从构造函数中调用 setVisible 。这是真的。更重要的是:您通常应该直接创建一个扩展JFrame 的类。 (在某些(罕见!)情况下,这是合适的,但一般准则应该是扩展 JFrame


And by the way: Timothy Truckle pointed out in a comment that you should not invoke setVisible from the constructor. This is true. More importantly: You should usually not directly create a class that extends JFrame. (In some (rare!) cases, this is appropriate, but the general guideline should be to not extend JFrame)

这篇关于将setVisible()函数放在函数的开头是不同的当我把它放在函数的末尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆