The Artima Developer Community
Sponsored Link

Java Answers Forum
help with blackjack game GUI

9 replies on 1 page. Most recent reply: Jun 4, 2002 1:27 AM by jake

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

help with blackjack game GUI Posted: Jun 3, 2002 2:48 PM
Reply to this message Reply
Advertisement
I am new at making gui's so this is my first, The assignment was to make a gui for a blackjack game and I am having event handling problems. "myhand" is a list box field in the gui, and "hit" is a button that just draws a card and then adds it to the hand() which is a vector and puts it in the list box. Can anyone help???
_______________________________________________________ Here are the errors.

--------------------Configuration: JDK version 1.3 <Default>------------------------------------------------
C:\PROGRA~1\XIN OXS~1\JCreator\MYPROJ~1\BLACKJ~1\BJcontrols.java:106: cannot resolve symbol
symbol : variable hit
location: class BJcontrols.gameActionListener
if(source==hit)
^
C:\PROGRA~1\XINOXS~1\JCreator\MYPROJ~1\BLACKJ~1\BJcontrols.java:111: cannot resolve symbol
symbol : variable myhand
location: class BJcontrols.gameActionListener
myhand.append(c1+"\n",0);
^
2 errors
Process completed.
_______________________________________________________
Here is the Jframe code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class BJcontrols extends JFrame
{
/*This is the class where I declare and instantiate my buttons
any other interface Components.*/

public BJcontrols()
{
super("BLACKJACK");
setSize(500,400);
setLocation(200,200);

//sets up the score label.
JLabel score=new JLabel("Score",SwingConstants.CENTER);
score.setFont (new Font ("Times New Roman", Font.BOLD, 25));

//sets up playing field labels
JLabel mhandlabel=new JLabel("My Hand",SwingConstants.LEFT);
mhandlabel.setFont (new Font ("Times New Roman", Font.BOLD, 12));
JLabel comphandlabel=new JLabel("Computers Hand",SwingConstants.RIGHT);
comphandlabel.setFont (new Font ("Times New Roman", Font.BOLD, 12));


//My Buttons& fields
JButton hit=new JButton("Hit");
JButton stay=new JButton("Stay");
JButton deal=new JButton("DEAL");
JButton exit=new JButton("EXIT");
List myhand=new List(13);
List computerhand=new List(13);
List score_box=new List(1);


//Creates a new container.
Container contentPane = getContentPane();
contentPane.setLayout(null);

//Adds buttons, fields, labels to the container.
contentPane.add(hit);
contentPane.add(stay);
contentPane.add(deal);
contentPane.add(exit);

contentPane.add(score);
contentPane.add(mhandlabel);
contentPane.add(comphandlabel);

contentPane.add(myhand);
//myhand.add("AceOfSpades",0);
contentPane.add(computerhand);
contentPane.add(score_box);
//score_box.add("21",0);

Insets insets = contentPane.getInsets();

//These are the buttons.
hit.setBounds(155 + insets.left, 300 + insets.top, 90, 30);
stay.setBounds(255 + insets.left, 300+ insets.top, 90, 30);
exit.setBounds(200 + insets.left, 220 + insets.top, 90, 30);
deal.setBounds(200 + insets.left, 140+ insets.top, 90, 30);

//Scoring.
score.setBounds(157 + insets.left, 3+ insets.top, 90, 30);
score_box.setBounds(236 + insets.left, 3+ insets.top, 40, 30);

//Playing field labels.
mhandlabel.setBounds(70 + insets.left,12+ insets.top,90,130);
comphandlabel.setBounds(348 + insets.left,12+ insets.top,90,130);

//These are the text fields.
myhand.setBounds(10 + insets.left,90+ insets.top,175,200);
computerhand.setBounds(305 + insets.left,90+ insets.top,175,200);


//Action events.
gameActionListener actionListener = new gameActionListener();
hit.addActionListener (actionListener);
//hit.addActionListener(this);
/*deal.addActionListener (actionListener);
exit.addActionListener (actionListener);
stay.addActionListener (actionListener);*/




}

//Caries out all the actions of the game.
private class gameActionListener implements ActionListener
{

public void actionPerformed(ActionEvent event)
{

DeckOfCards deck=new DeckOfCards();
Hand hand=new Hand();
Object source=event.getSource();

if(source==hit)
{
Card c1=deck.getTopCard();
hand.addCard(c1);

myhand.append(c1+"\n",0);

}
}
}



}


abhijeet

Posts: 21
Nickname: sony
Registered: May, 2002

Re: help with blackjack game GUI Posted: Jun 3, 2002 10:00 PM
Reply to this message Reply
Hi Jake,

You have declared both, the JButtton "hit" and the List "myhand" inside the constructor BJcontrols().
You have to declare these globally because you are accessing these components outside the constructor.
Try with declaring these components globally and let me know the result.

abhijeet.

jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Re: help with blackjack game GUI Posted: Jun 3, 2002 10:58 PM
Reply to this message Reply
when I declare the jbutton hit, and the text field myhand out side the field I get a deprication error.

here it is:
--------------------Configuration: JDK version 1.3 <Default>------------------------------------------------
Note: C:\PROGRA~1\XINOXS~1\JCreator\MYPROJ~1\BLACKJ~1\BJcontrols.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
Process completed.

abhijeet

Posts: 21
Nickname: sony
Registered: May, 2002

Re: help with blackjack game GUI Posted: Jun 3, 2002 11:38 PM
Reply to this message Reply
Hi Jake,

You have declared "myhand" as List. You can use JList instead of List and i think there is no append() method for the List Component. Check the code!!

abhijeet.

jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Re: help with blackjack game GUI Posted: Jun 3, 2002 11:47 PM
Reply to this message Reply
hey, I tried making List---->JList and it didn't like it. I don't think there is a JList. Right now I press the hit button and the first card in the deck appears "AceOfClubs" and I will keep pressing hit and it will keep adding AceOfClubs to the list as many times as I press hit. Do you think this has to do with the deprication.
Thanks,
jake.

jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Re: help with blackjack game GUI Posted: Jun 4, 2002 12:00 AM
Reply to this message Reply
hey nevermind the last post, I figuared out why I was always getting "AceOfClubs". It was because everytime an action happened I was making a new deck and hand. But it works fine, there is still a deprication error though. Do you think it matters???
jake.

abhijeet

Posts: 21
Nickname: sony
Registered: May, 2002

Re: help with blackjack game GUI Posted: Jun 4, 2002 12:27 AM
Reply to this message Reply
Hi Jake,

Actually the components which start with the letter "J" are all Swing components and the rest are AWT components. When you are trying to develop the GUI in Swings then use only Swing Components. That's why you will have JList in Swing instead of List.

abhijeet.

jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Re: help with blackjack game GUI Posted: Jun 4, 2002 12:57 AM
Reply to this message Reply
when I put JList in my code it said it could not resolve symbol.

abhijeet

Posts: 21
Nickname: sony
Registered: May, 2002

Re: help with blackjack game GUI Posted: Jun 4, 2002 1:23 AM
Reply to this message Reply
you should import javax.swing.*;

abhijeet.

jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Re: help with blackjack game GUI Posted: Jun 4, 2002 1:27 AM
Reply to this message Reply
yeah I finaly found out what I was doing wrong, the JList constructor just needed a parameter. Now I just have to find out how to add a card to the JList. Thank you for all your help though.
jake.

Flat View: This topic has 9 replies on 1 page
Topic: bubblesort Previous Topic   Next Topic Topic: swing gui with c++ server

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use