0

I am pretty new to this and I am following a tutorial with everything working great. However, when adding a JTextField, the JFrame turns into a plain gray background. I am calling the GUI class that makes the JFrame from the Main.java, so I tried to make the JFrame with just the text field in the Main.java, but it has the same problem. Adding and removing components to the text field didn't do anything either. Putting it in a panel didn't help (the panel worked fine with labels).

public class GUI extends JFrame {

    JButton button;
    
    GUI(){
        this.setTitle("Visualization");
        this.setSize(1200, 800);
        this.setResizable(false);
        this.setLayout(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ImageIcon image = new ImageIcon("Default/WhiteCatPFP.jpg");
        this.setIconImage(image.getImage());
        //this.getContentPane().setBackground(Color.BLACK);

        Border border = BorderFactory.createLineBorder(Color.green, 3);

        JLabel label = new JLabel();
        label.setForeground(Color.GREEN);
        label.setText("Bro, do you even code?");
        label.setBackground(Color.BLACK);
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setOpaque(true);
        label.setBorder(border);
        label.setFont(new Font(this.getFont().getName(), Font.PLAIN, 30));
        //label.setVerticalAlignment(JLabel.TOP);
        //label.setFont(new Font("MV Boli", Font.PLAIN, 20));

        button = new JButton();
        button.setFocusable(false);
        button.setForeground(Color.GREEN);
        button.setBackground(Color.BLACK);
        button.setOpaque(true);
        button.setBorder(border);
        button.setText("Baka");
        button.setFont(new Font(this.getFont().getName(), Font.PLAIN, 20));
        button.setBounds(50, 10, 100, 90);
        button.addActionListener(e -> System.out.println("Epic")); // Called lambda

        JTextField aTextField = new JTextField("Hello"); // <-------------------- Error inducing!
        aTextField.setBounds(0, 400, 400, 200);

        JPanel panelOne = new JPanel();
        panelOne.setBounds(0, 0, 400, 200);
        panelOne.setLayout(new BorderLayout());

        JPanel panelTwo = new JPanel();
        panelTwo.setBounds(0, 200, 400, 200);
        panelTwo.setLayout(new BorderLayout());

        JPanel panelThree = new JPanel();
        panelThree.setBounds(0, 400, 400, 200);
        panelThree.setLayout(new BorderLayout());

        panelOne.add(label);
        panelTwo.add(button);
        panelThree.add(aTextField);
        this.add(panelOne);
        this.add(panelTwo);
        this.add(panelThree);
    }
}

A lot of stuff is there for notes, and I would rather use the null layout thing for this specific project (unless that is the source of the error), and the empty space on the right is for something I will add later.

Note: without the text field, everything else shows up fine. Even just defining the text field with JTextField aTextField = new JTextField(); made the JFrame a gray background. No crashes too.

Any help is greatly appreciated!

Bashir
  • 2,057
  • 5
  • 19
  • 44
  • 2
    *I would rather use the null layout thing for this specific project* I would rather not help people that don't listen to advice. – Gilbert Le Blanc Jan 04 '21 at 08:17
  • Hmm, I said that because a few lessons later there were layouts and I didn't reach that point yet, but it seems I was in the wrong about that! – Piemanray314 Jan 04 '21 at 20:44

1 Answers1

0

I am following a tutorial

If the tutorial is using a null layout then get rid of the tutorial. There is not reason to use a null layout. Swing was designed to be used with layout managers (for too many reasons to list here).

Read the Swing tutorial on Layout Managers for proper demo code. The tutorial code will show you how to properly create a Swing frame. Start with the demo code from the tutorial and then make changes.

However, when adding a JTextField, the JFrame turns into a plain gray background.

The text field has nothing to do with your problem.

The problem is that the components need to be added to the frame BEFORE the frame is made visible. The setVisible( true ) statement should be the last statement in the constructor.

camickr
  • 321,443
  • 19
  • 166
  • 288