Does placing setVisible() function in the beginning of the function be different when I placed it at the end of that function?

前端 未结 2 1832
春和景丽
春和景丽 2021-01-25 00:56

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 beginnin

相关标签:
2条回答
  • 2021-01-25 01:45

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

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


    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.

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


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

    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)

    0 讨论(0)
  • 2021-01-25 01:56

    The components cannot be shown, because you add them after you call the setVisible() method of the Frame.

    The Componet's add() method changes layout-related information, and invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to display the added component, as pointed here .

    So in order to show the items, you should either call the revalidate() method of the frame or call setVisible() after all your components are added.

    Unless there is no special need, you should call setVisible() after you have added every other component.

    public class TestMain extends JFrame {
    
    public static void main(String[] args) {
       JFrame test = new TestMain();
       //if the setVisible() is called too early, you have to revalidate
       test.revalidate();
    }
    
    public TestMain() { 
        setFrame();
    }
    
    private void setFrame() {
        setSize(400,400);
        setResizable(false);
        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new FlowLayout());
        setVisible(true);
        JTextArea textArea1 = new JTextArea(25,15);
        textArea1.setWrapStyleWord(true);
        textArea1.setLineWrap(true);
        panel.add(textArea1);
        JScrollPane scroll = 
                new JScrollPane(panel, 
                        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
                        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        // this method invalidates the component hierarchy.
        getContentPane().add(scroll);
    
    }
    

    }

    0 讨论(0)
提交回复
热议问题