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!