Jump to content

Help with java swing please!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
boburob

boburob

    Newbie

  • Members
  • Pip
  • 3 posts
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:

//Blackjack

//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

	//hearts

	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");

	//clubs

	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");

	//spades

	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");

	//diamonds

	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");

	//

	//constructor

	public blackjack() {

		super("Blackjack");

		setSize(600, 400);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		GridLayout cards = new GridLayout(1, 2, 10, 10);

		JPanel panel = new JPanel();

		JPanel panel2 = new JPanel();

		panel.setLayout(cards);

		panel2.setLayout(cards);

		JFrame frame = new JFrame();

		panel.add(cardOne);

		panel.add(cardTwo);

		panel2.add(pc_cardOne);

		panel2.add(pc_cardTwo);

		frame.add(panel);

		frame.add(panel2);

                add(frame);

		setVisible(true);

	}




	//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!

#2
whitey6993

whitey6993

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 437 posts
I would try getting rid of the frame and just adding the two panels directly to the window.

#3
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

whitey6993 said:

I would try getting rid of the frame and just adding the two panels directly to the window.

Uhm could you show a code strip of it, so he could understand better since I didn't understand either what you meant !
Posted Image

#4
whitey6993

whitey6993

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 437 posts
Sure no problem.

In boburob's code, he is using this code to add his panels to the window through a frame.

JFrame frame = new JFrame();
		panel.add(cardOne);
		panel.add(cardTwo);
		panel2.add(pc_cardOne);
		panel2.add(pc_cardTwo);
		frame.add(panel);
		frame.add(panel2);
                add(frame);

What I am suggesting he do is not code the frame and just add the panels directly to the window.

		panel.add(cardOne);
		panel.add(cardTwo);
		panel2.add(pc_cardOne);
		panel2.add(pc_cardTwo);;
                add(panel1);
                add(panel2);

Sorry for being unclear :)

#5
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

whitey6993 said:

Sure no problem.

In boburob's code, he is using this code to add his panels to the window through a frame.

JFrame frame = new JFrame();
		panel.add(cardOne);
		panel.add(cardTwo);
		panel2.add(pc_cardOne);
		panel2.add(pc_cardTwo);
		frame.add(panel);
		frame.add(panel2);
                add(frame);

What I am suggesting he do is not code the frame and just add the panels directly to the window.

		panel.add(cardOne);
		panel.add(cardTwo);
		panel2.add(pc_cardOne);
		panel2.add(pc_cardTwo);;
                add(panel1);
                add(panel2);

Sorry for being unclear :)

Good you found one bad line !
add(frame) in his point isn't even doing anything special !



Also try this !

public blackjack() {
		super("Blackjack");
		setSize(600, 400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		GridLayout cards = new GridLayout(1, 2, 10, 10);
		JPanel panel = new JPanel();
		JPanel panel2 = new JPanel();
		panel.setLayout(cards);
		panel2.setLayout(cards);
		panel.add(cardOne);
		panel.add(cardTwo);
		panel2.add(pc_cardOne);
		panel2.add(pc_cardTwo);
		setVisible(true);
	}

Posted Image