GUI shows elements only after dragging a window

frame_ref = new Frame("Login"); mainPanel_ref = new Panel(); buttonPanel_ref = new Panel(); grid_ref = new GridLayout(4,2); frame_ref.setSize(300,120); frame_ref.setVisible(true); email_ref = new TextField(); password_ref = new JPasswordField(); mainPanel_ref.setLayout(grid_ref); mainPanel_ref.add(new Label("E-Mail")); mainPanel_ref.add(email_ref); mainPanel_ref.add(new Label("Passwort")); mainPanel_ref.add(password_ref); mainPanel_ref.add(submitLogin_ref); mainPanel_ref.add(fehlerMeldung_ref); frame_ref.add(mainPanel_ref); 

I created a view in Java as described above. The window is completely empty, but after I dragged its size, all the elements will appear. Does anyone know how to fix this?

+6
source share
2 answers

Call frame_ref.setVisible(true); after frame_ref.add(mainPanel_ref); .

What happens here: you show the frame by calling frame_ref.setVisible(true); , and then add elements to it. This way you get an empty frame. Subsequently, when you drag or resize, it is redrawn, and you can see the elements.

+11
source

Call pack() on the JFrame after adding the components. This will cause the frame to be the smallest size needed to display the components. Finally call ( setLocation() (4) &) setVisible(true) .

Login frame

 import java.awt.GridLayout; import javax.swing.*; import javax.swing.border.EmptyBorder; class FrameTest { public void init() { frame_ref = new JFrame("Login"); frame_ref.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainPanel_ref = new JPanel(new GridLayout(4,2,6,3)); mainPanel_ref.setBorder(new EmptyBorder(5,5,5,5)); email_ref = new JTextField(); password_ref = new JPasswordField(); mainPanel_ref.add(new JLabel("E-Mail")); mainPanel_ref.add(email_ref); mainPanel_ref.add(new JLabel("Passwort")); mainPanel_ref.add(password_ref); mainPanel_ref.add(new JLabel("")); mainPanel_ref.add(new JLabel("")); mainPanel_ref.add(submitLogin_ref); mainPanel_ref.add(fehlerMeldung_ref); frame_ref.add(mainPanel_ref); //frame_ref.setSize(300,120); frame_ref.pack(); frame_ref.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new FrameTest().init(); } }); } private JFrame frame_ref; private JPanel mainPanel_ref; private JTextField email_ref; private JPasswordField password_ref; private JButton submitLogin_ref = new JButton("Submit Login"); private JButton fehlerMeldung_ref = new JButton("Fehler Meldung"); } 

Other tips:

  • Do not mix Swing with AWT. At least not components or not before targeting Java 7 +.
  • The login component is often better suited to input JDialog or JOptionPane rather than JFrame .
  • This may be better suited for a nested layout or other layout than GridLayout
  • setLocation() can be replaced with:
    • If the login has a "parent" component, setLocationRelativeTo(Component) .
    • If the login is the visible first screen, setLocationByPlatform(true) (1.6 +).
  • Check the source for other tips.
  • Publish SSCCE to better help.
+9
source

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


All Articles