I really can’t figure out why this JPanel “p” isn’t appearing?
I thought I coded it right for the JPanel p to be in the middle of the Jframe and should make the whole JFrame RED but it doesn’t seem to do that and the buttons and JPanel aren’t appearing. Sorry. I know I am probably stupid but please help. 😕
Here is the code.
package com.gorillalogic.henry;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Notepad {
private JFrame f; // creates all GUI components
private JPanel p;
private JButton b1;
public Notepad() {
gui();
}
public void gui() {
f = new JFrame("Notepad");
p = new JPanel();
b1 = new JButton("Quit");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
f.setSize(600, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
p.setBackground(Color.RED);
p.add(b1);
f.add(p, BorderLayout.CENTER);
}
public static void main(String[] args) {
new Notepad();
}
}
Thanks in advance. 🙂
You need to do that.