JTextField / JTextArea未显示

时间:2016-09-10 18:45:26

标签: java swing awt jtextfield jtextarea

当我启动JTextField和/或JTextArea时,它们没有显示。仅显示JButtonJLabel

我错过了什么?请给我一些解决方案。

我正在使用JFrameJPanel

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`**

1 个答案:

答案 0 :(得分:1)

尝试此修改:

    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.
    }