user3655667
user3655667

Reputation: 9

JTextField / JTextArea are not showing

When I launch the JTextField and or JTextArea, but they are not showing. Only JButton and JLabel are showing.

What am I missing? Please give me some solutions for this.

I am using JFrame and JPanel.

package student_management;
import java.awt.*;
import javax.swing.*;

public class Login extends JFrame{

    JFrame window_login =new JFrame();

    public Login(){
        //creating frame
        window_login.setVisible(true);
        window_login.setSize(500, 500);
        window_login.setTitle("Login Screen");
        window_login.setLocationRelativeTo(null);
        window_login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        createView();
    }
    private void createView(){
        //create panel
        JPanel top_title = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        //JLabel b1=new JLabel("Submit");
        JButton b2=new JButton("Reset");
        JTextField b1=new JTextField("text",10);

        c.insets = new Insets(5,5,5,5);
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        top_title.add(b1, c);

        c.gridx = 1;
        c.gridy = 0;
        c.anchor = GridBagConstraints.LAST_LINE_START;
        top_title.add(b2, c);

        window_login.add(top_title);
    }

    public static void main(String[] args) {
        new Login();
    }
}**`strong text`**

Upvotes: 0

Views: 90

Answers (1)

Young Emil
Young Emil

Reputation: 2266

Try this edit:

    public Login(){
        //creating frame

        window_login.setSize(500, 500);
        window_login.setTitle("Login Screen");
        window_login.setLocationRelativeTo(null);
        window_login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        createView();
        window_login.setVisible(true);//this statement has been move to last in the constructor.
    }

Upvotes: 1

Related Questions