0

We are making a card game and created two JLabel objects to represent the dealer and player cards but both panels appear on top of each other, and we can't figure out how to move them. Also, the background color for the frame was set to green but the panels have a white background. :(

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(1000, 500));

JButton hitButton = new JButton("Hit");
hitButton.setBounds(100,400,50,50);
frame.add(hitButton);
JButton standButton = new JButton("Stand");
standButton.setBounds(400,400,75,50);
frame.add(standButton);
JButton doubleDownButton = new JButton("Double down");
doubleDownButton.setBounds(700,400,175,50);
frame.add(doubleDownButton);

game = new BlackjackGame();

JPanel panel1 = new JPanel();
JLabel dealerCard1 = new JLabel (game.getDealerCard1().getImage());
dealerCard1.setFont(new Font("Serif", Font.PLAIN, 60));
JLabel dealerCard2 = new JLabel ("\ud83c\udca0");
dealerCard2.setFont(new Font("Serif", Font.PLAIN, 60));
panel1.add(dealerCard1);
panel1.add(dealerCard2);
panel1.setLocation(300, 150);
frame.add(panel1);

JPanel panel2 = new JPanel();
JLabel playerCard1 = new JLabel (game.getPlayerCard1().getImage());
playerCard1.setFont(new Font("Serif", Font.PLAIN, 60));
JLabel playerCard2 = new JLabel (game.getPlayerCard2().getImage());
playerCard2.setFont(new Font("Serif", Font.PLAIN, 60));
panel2.add(playerCard1);
panel2.add(playerCard2);
frame.add(panel2);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
bossman
  • 1
  • 1
  • By default the content pane of a JFrame uses a BorderLayout. You are attempting to add both panels to the CENTER of the BorderLayout which won't work. You need to use a different layout manager. As a simple exercise change the layout manager of the frame to be a FlowLayout to see the difference. Read the Swing tutorial on [Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) and use a more appropriate layout manager to achieve your desired layout. – camickr May 23 '22 at 02:56
  • 1) Provide ASCII art or a simple drawing of the intended layout of the GUI. If the frame is resizable, also show it with more width and height - to show how extra space should be assigned. 2) For better help sooner, [edit] to add a [mre]. 3) They layout tutorial linked above is good, but I don't feel it gives enough attention to combining layouts. Almost any GUI beyond the completely trivial combines layouts as needed for each logical section of the GUI. – Andrew Thompson May 23 '22 at 04:17
  • 3) [cont..] See [this example](https://stackoverflow.com/a/5630271/418556) that includes screenshots with titled borders around the panels to document each layout used for the overall effect. – Andrew Thompson May 23 '22 at 04:24
  • Oracle has a helpful tutorial, [Creating a GUI With Swing](https://docs.oracle.com/javase/tutorial/uiswing/index.html). Skip the Learning Swing with the NetBeans IDE section. Generally, you create one drawing `JPanel` and draw cards (`BufferedImage`) on the drawing `JPanel`. Pay close attention to the [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) section. – Gilbert Le Blanc May 23 '22 at 04:25

0 Answers0