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?
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.
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 .
A Swing GUI consists of two key items: components and containers.
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.
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.
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)
.
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");
}
JDialog
or JOptionPane
rather than a JFrame
.GridLayout
setLocation()
might be swapped out for:
setLocationRelativeTo(Component)
. setLocationByPlatform(true)
(1.6+).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With