CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2008
    Posts
    24

    Java swing help!

    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:

    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:

    Code:
    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. #2
    Join Date
    Oct 2008
    Posts
    77

    Re: Java swing help!

    so you have this

    Code:
     public class blackjack extends JFrame
    and then

    Code:
     JFrame frame = new JFrame();
                    add(frame);
    which means you are adding frame to a frame, can't do that. You can't add JFrame as a component because it is window, a top level container.
    You really don't need 2 JFrame-s. The one from extended class itself is sufficient.
    Last edited by postmortem; January 1st, 2009 at 10:10 PM.

  3. #3
    Join Date
    Feb 2008
    Posts
    24

    Re: Java swing help!

    Yes you were right, i was kinda trying to copy something straight out of my java book without thinking about it! Instead I just added one panel to another panel

    Sorry but I have another question, is it possible to output a variable in a message box, e.g.:
    Code:
    		if (source == menuStats)
    		{
    			JOptionPane.showMessageDialog(null, "Statistics\nGames won:", winCount);
    		}
    winCount is just an int, thanks for any help guys! and thanks postmortem

  4. #4
    Join Date
    Feb 2008
    Posts
    24

    Re: Java swing help!

    Lol ignore my question! Once again im being stupid and forgetting basics!
    Code:
    		if (source == menuStats)
    		{
    			JOptionPane.showMessageDialog(null, "Statistics\nGames won:" + winCount);
    		}
    Incase anyone wondered:P

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured