0

My problem is IntelliJ IDE doesn't add JTextField here is the code.

public IntellJ (){

        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setSize(400,600);
        frame.setLocation(400,200);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel=new JPanel();
        setContentPane(panel);
        panel.setLayout(null);
        frame.add(panel);


        JLabel jLabel1=new JLabel("Ogrenci Numaraniz");      
        JTextField jText=new JTextField("Hi");

        jLabel1.setBounds(20,20,150,50);

        jLabel3.setBounds(20,70,150,50);
        panel.add(jLabel1);       
        panel.add(jText);
    }

If I'm delete JTextField and try JLabel jText = new JLabel("Hi"); write this code is working. I don't understand why JTextField is being problem.

Alper C
  • 25
  • 3

1 Answers1

0

I think the main problem in your program is that you try to use absolute positioning of components (using panel.setLayout(null) and using setBounds() on components). Instead, always use layout managers in Swing UIs.

Also, call frame.setVisible(true) at the end, after setting up all UI components.

I've done these changes to your code and it works:
(Here I have simply removed setLayout(null). So, the default layout managers are in action.)

import javax.swing.*;

public class IntellJ {

  public IntellJ (){

    JFrame frame = new JFrame();
    frame.setSize(400,600);
    frame.setLocation(400,200);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel=new JPanel();
    frame.add(panel);

    JLabel jLabel1=new JLabel("Ogrenci Numaraniz");
    JTextField jText=new JTextField("Hi");

    panel.add(jLabel1);
    panel.add(jText);

    frame.setVisible(true);
  }

  public static void main(String[] args) {
    new IntellJ();
  }
}
Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16