Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GUI shows elements only after dragging 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 set up a view in Java like above. The window is complete empty, but after I drag and drop its size, all the elements appear. Does somebody know how to fix this?

like image 265
最白目 Avatar asked Aug 09 '11 11:08

最白目


People also ask

What is swing in GUI?

Swing in Java is a lightweight GUI toolkit which has a wide variety of widgets for building optimized window based applications. It is a part of the JFC( Java Foundation Classes). It is build on top of the AWT API and entirely written in java. It is platform independent unlike AWT and has lightweight components.

What are the GUI components in Java?

GUI Component classes, such as Button , TextField , and Label . GUI Container classes, such as Frame and Panel . Layout managers, such as FlowLayout , BorderLayout and GridLayout . Custom graphics classes, such as Graphics , Color and Font .

What are the two key items in Swing GUI?

A Swing GUI consists of two key items: components and containers.

What is GUI in Java with examples?

GUI (Graphical User Interface) in Java is an easy-to-use visual experience builder for Java applications. It is mainly made of graphical components like buttons, labels, windows, etc. through which the user can interact with an application. GUI plays an important role to build easy interfaces for Java applications.


2 Answers

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

What happens here is: You show frame by calling frame_ref.setVisible(true); and then add elements in it. So you get an empty frame. Afterwards when you drag or resize it gets repainted and you can see elements.

like image 186
Harry Joy Avatar answered Sep 30 '22 15:09

Harry Joy


Call pack() on the JFrame after the components have been added. Doing so will cause the frame to assume the smallest size it needs 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:

  1. Don't mix Swing with AWT. At least, not the components, or not before targeting Java 7+.
  2. A log-in component is often better suited to putting in a JDialog or JOptionPane rather than a JFrame.
  3. This might be better suited to a nested layout, or some other layout than GridLayout
  4. setLocation() might be swapped out for:
    • If the log-in has a 'parent' component, setLocationRelativeTo(Component).
    • If the log-in is the first screen visible, setLocationByPlatform(true) (1.6+).
  5. Check the source closely for other tips.
  6. For better help sooner, post an SSCCE.
like image 22
Andrew Thompson Avatar answered Sep 30 '22 14:09

Andrew Thompson