Java GUI compiles without errors, but sometimes shows nothing

I use a custom class that extends JFrame, but sometimes it doesn't show anything. I never get any errors, so I'm curious if there is a java command that can help me print something. I was looking for other questions, but found nothing. This is actually not too crazy, but it is curious why this is happening. I would like to fix the problem in order to avoid future problems.


Blank
enter image description here
GUI
enter image description here

public MemberPanel(int i) throws IOException { Container contentPane = getContentPane(); GridLayout layout = new GridLayout(2, 1); contentPane.setLayout(layout); setVisible(true); setLocation(0, 0); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 170); setResizable(false); greenStatus = new JButton("Non-Critical"); yellowStatus = new JButton("Important"); redStatus = new JButton("Mission Critical"); greenStatus.setFont(fontTextOne); yellowStatus.setFont(fontTextOne); redStatus.setFont(fontTextOne); greenStatus.addActionListener(this); yellowStatus.addActionListener(this); redStatus.addActionListener(this); buttonPanel.add(greenStatus); buttonPanel.add(yellowStatus); buttonPanel.add(redStatus); statusLabel = new JLabel("In 75 letters or less... What are you working on?"); statusLabel.setVerticalAlignment(JLabel.CENTER); statusLabel.setHorizontalAlignment(JLabel.CENTER); statusLabel.setFont(fontTextTwo); textFieldPanel.add(statusLabel); textFieldPanel.add(statusMessage); contentPane.add(buttonPanel); contentPane.add(textFieldPanel); } 
+6
source share
1 answer

You add a bunch of components after calling setVisible(true) in the JFrame:

 public MemberPanel(int i) throws IOException { Container contentPane = getContentPane(); GridLayout layout = new GridLayout(2, 1); contentPane.setLayout(layout); setVisible(true); // ****** here // ..... // only now do you add components... contentPane.add(buttonPanel); contentPane.add(textFieldPanel); } 

Thus, components may or may not be displayed depending on whether the GUI is changed or not (see what happens when you resize an empty gui). Fix: call setVisible(true) only after adding everything.

+10
source

Source: https://habr.com/ru/post/912097/


All Articles