I would like to create a simple Blackjack GUI in Java. I know the basics of creating JLabel, JPanel etc. However, I cannot find why my JLabel is not displayed on the screen. Here is my code:
//Create and set up the window.
JFrame frame = new JFrame("BlackJack");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//new border panel
JPanel gui = new JPanel(new BorderLayout(2,2));
//create players panel
JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel2.setPreferredSize(new Dimension(600, 200));
panel2.setBackground(Color.ORANGE);
gui.add(panel2, BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setVisible(true);
frame.setTitle("BlackJack!");
//add players name
String name = JOptionPane.showInputDialog(null, "Name?");
JLabel playerName = new JLabel(name);
playerName.setPreferredSize(new Dimension(100, 40));
playerName.setFont(new Font("sansserif", Font.BOLD, 18));
panel2.add(playerName);
When I hit compile, what I get is a dialog for name? and then an empty panel. I do not understand why my JLabel is not in the panel since I have added it to my frame. Am I missing something?