Hey everyone, im writing a blackjack program with java swing and im having problems with setting the layout with panels and frames.

I want to apply a different layout to two different panels, add them to a frame and then display the frame, heres my code:

//By Thomas Bean

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class blackjack extends JFrame {
	//create icons used in toolbar
	ImageIcon two_hearts = new ImageIcon("image/51.png");
	ImageIcon three_hearts = new ImageIcon("image/47.png");
	ImageIcon four_hearts = new ImageIcon("image/43.png");
	ImageIcon five_hearts = new ImageIcon("image/39.png");
	ImageIcon six_hearts = new ImageIcon("image/35.png");
	ImageIcon seven_hearts = new ImageIcon("image/31.png");
	ImageIcon eight_hearts = new ImageIcon("image/27.png");
	ImageIcon nine_hearts = new ImageIcon("image/23.png");
	ImageIcon ten_hearts = new ImageIcon("image/19.png");
	ImageIcon jack_hearts = new ImageIcon("image/15.png");
	ImageIcon queen_hearts = new ImageIcon("image/11.png");
	ImageIcon king_hearts = new ImageIcon("image/7.png");
	ImageIcon ace_hearts = new ImageIcon("image/3.png");
	ImageIcon two_clubs = new ImageIcon("image/49.png");
	ImageIcon three_clubs = new ImageIcon("image/45.png");
	ImageIcon four_clubs = new ImageIcon("image/41.png");
	ImageIcon five_clubs = new ImageIcon("image/37.png");
	ImageIcon six_clubs = new ImageIcon("image/33.png");
	ImageIcon seven_clubs = new ImageIcon("image/29.png");
	ImageIcon eight_clubs = new ImageIcon("image/25.png");
	ImageIcon nine_clubs = new ImageIcon("image/21.png");
	ImageIcon ten_clubs = new ImageIcon("image/17.png");
	ImageIcon jack_clubs = new ImageIcon("image/13.png");
	ImageIcon queen_clubs = new ImageIcon("image/9.png");
	ImageIcon king_clubs = new ImageIcon("image/5.png");
	ImageIcon ace_clubs = new ImageIcon("image/1.png");
	ImageIcon two_spades = new ImageIcon("image/50.png");
	ImageIcon three_spades = new ImageIcon("image/46.png");
	ImageIcon four_spades = new ImageIcon("image/42.png");
	ImageIcon five_spades = new ImageIcon("image/38.png");
	ImageIcon six_spades = new ImageIcon("image/34.png");
	ImageIcon seven_spades = new ImageIcon("image/30.png");
	ImageIcon eight_spades = new ImageIcon("image/26.png");
	ImageIcon nine_spades = new ImageIcon("image/22.png");
	ImageIcon ten_spades = new ImageIcon("image/18.png");
	ImageIcon jack_spades = new ImageIcon("image/14.png");
	ImageIcon queen_spades = new ImageIcon("image/10.png");
	ImageIcon king_spades = new ImageIcon("image/6.png");
	ImageIcon ace_spades = new ImageIcon("image/2.png");
	ImageIcon two_diamonds = new ImageIcon("image/52.png");
	ImageIcon three_diamonds = new ImageIcon("image/48.png");
	ImageIcon four_diamonds = new ImageIcon("image/44.png");
	ImageIcon five_diamonds = new ImageIcon("image/40.png");
	ImageIcon six_diamonds = new ImageIcon("image/36.png");
	ImageIcon seven_diamonds = new ImageIcon("image/32.png");
	ImageIcon eight_diamonds = new ImageIcon("image/28.png");
	ImageIcon nine_diamonds = new ImageIcon("image/24.png");
	ImageIcon ten_diamonds = new ImageIcon("image/20.png");
	ImageIcon jack_diamonds = new ImageIcon("image/16.png");
	ImageIcon queen_diamonds = new ImageIcon("image/12.png");
	ImageIcon king_diamonds = new ImageIcon("image/8.png");
	ImageIcon ace_diamonds = new ImageIcon("image/4.png");
	//card tops
	ImageIcon cardIcon = new ImageIcon("image/b1fv.png");
	ImageIcon pc_cardIcon = new ImageIcon("image/b2fv.png");
	//create buttons
	JButton cardOne = new JButton("", cardIcon);
	JButton cardTwo = new JButton("", cardIcon);
	JButton pc_cardOne = new JButton("", pc_cardIcon);
	JButton pc_cardTwo = new JButton("", pc_cardIcon);
	JButton new_game = new JButton("New Game");
	public blackjack() {
		setSize(600, 400);
		GridLayout cards = new GridLayout(1, 2, 10, 10);
		JPanel panel = new JPanel();
		JPanel panel2 = new JPanel();
		JFrame frame = new JFrame();

	//main function
	public static void main(String[] arguments) {
		blackjack main = new blackjack();

This gives me these errors when run:

thomas@thomas-laptop:~/Documents/Work/Programming/Blackjack$ java blackjackException in thread "main" java.lang.IllegalArgumentException: adding a window to a container
	at java.awt.Container.addImpl(Container.java:1061)
	at java.awt.Container.add(Container.java:974)
	at javax.swing.JFrame.addImpl(JFrame.java:562)
	at java.awt.Container.add(Container.java:377)
	at blackjack.<init>(blackjack.java:95)
	at blackjack.main(blackjack.java:103)

Thank you for any help!

Recommended Answers

All 2 Replies

I think this is what is causing the problem :-


To quote the Java Tutorial on frames here :-

A Frame is a top-level window with a title and a border.

So basically frames cannot be added into any other component.
What you could do is add the components directly to your main frame, or you could change your "frame" variable and make it a JPanel instead.

Also when adding components to a frame you cannot add the components directly to a frame, you need to add them to its content pane like this:-


You can learn more on swing if you go through this tutorial here.

Another thing, that isn't the source of your problem... you might need to have a different Object for each layout. You're using the same 'cards' GridLayout for both. I'm not really sure about that but I typically see a different one for each panel. (Someone confirm or deny that?)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.