0

I am trying to position something using absolute positioning, and for some reason when I run my code the window that previously worked, shows nothing.

This is the code that I have which works:

public LoginScreen() {
        window = new JFrame();
        login = new JPanel();

        username = new JTextField();

        login.add(username);
        window.add(login);

This is the code which does not work:

public LoginScreen() {
        window = new JFrame();

        login = new JPanel();
        login.setLayout(null);

        username = new JTextField();
        username.setBounds(50, 50, 100, 50);

        login.add(username);
        window.add(login);

I am not sure why the latter code is not working, someone please help!!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    Like a Rocky and Bullwinkle magic act, this trick never works. Use a layout manager. – markspace Jun 15 '20 at 15:29
  • 1
    When you create the JTextField you should use: `new JTextField(10)`, so the component can determine its preferred size to display at least 10 characters before scrolling is required. – camickr Jun 15 '20 at 15:33
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. – Andrew Thompson Jun 15 '20 at 15:58

0 Answers0