4 Replies - 825 Views - Last Post: 08 October 2011 - 09:00 AM Rate Topic: -----

#1 lmharmon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-September 11

BlackJack Game Code Errors

Posted 08 October 2011 - 07:15 AM

I have to make a BlackJack game for a class assignment, and I got most of it right, but there were a few things that came up at errors that I'm not quite sure how to fix...

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package blackjack;

/**
 *
 * @author lauraharmon
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Button;



class BlackJack extends JFrame implements ActionListener {
    
    
    
// these declare the deal, hit, and stay [buttons]
private JButton deal;
private JButton stay;
private JButton hit;

// these declare the dealer card slots [labels]
private JLabel dslot1;
private JLabel dslot2;
private JLabel dslot3;
private JLabel dslot4;
private JLabel dslot5;

// these declare the player card slots [labels]
private JLabel pslot1;
private JLabel pslot2;
private JLabel pslot3;
private JLabel pslot4;
private JLabel pslot5;

// this declares the dealers field
private JTextField dscore;

// this declares the players field
private JTextField pscore;

// this is the background label
private JLabel bg;

private int dealer = 1;

private Deck dk;

// these are the spade cards
ImageIcon bj = new ImageIcon("S01.jpg");
ImageIcon as = new ImageIcon("S02.jpg");
ImageIcon ss = new ImageIcon("S03.jpg");
ImageIcon ths = new ImageIcon("S03.jpg");
ImageIcon fos = new ImageIcon("S04.jpg");
ImageIcon fis = new ImageIcon("S05.jpg");
ImageIcon backofcard = new ImageIcon("back.jpg");

public table() {
    // this is the blackjack table
    dk = new Deck();

Container contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.setBackground(Color.black);

setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Blackjack 21");
setSize(750,700);
setResizable(false);
setLocation(100,25);

// these are the deal, stay, hit buttons
deal = new JButton("Deal");
deal.setBounds(150,600,75,25);
deal.addActionListener(this);
contentPane.add(deal);

stay = new JButton("Stay");
stay.setBounds(300,600,75,25);
stay.addActionListener(this);
contentPane.add(stay);

hit = new JButton("Hit");
hit.setBounds(450,600,75,25);
hit.addActionListener(this);
contentPane.add(hit);

// these are the dealers card slot labels
dslot5 = new JLabel("");
dslot5.setBounds(250,50,107,148);
dslot5.setBorder(BorderFactory.createLineBorder(Color.pink));
contentPane.add(dslot5);

dslot4 = new JLabel("");
dslot4.setBounds(200,50,107,148);
dslot4.setBorder(BorderFactory.createLineBorder(Color.yellow));
contentPane.add(dslot4);

dslot3 = new JLabel("");
dslot3.setBounds(150,50,107,148);
dslot3.setBorder(BorderFactory.createLineBorder(Color.green));
contentPane.add(dslot3);

dslot2 = new JLabel("");
dslot2.setBounds(100,50,107,148);
dslot2.setBorder(BorderFactory.createLineBorder(Color.blue));
contentPane.add(dslot2);

dslot1 = new JLabel("");
dslot1.setBounds(50,50,107,148);
dslot1.setBorder(BorderFactory.createLineBorder(Color.white));
contentPane.add(dslot1);

// these are the player card slot labels
pslot5 = new JLabel("");
pslot5.setBounds(250,300,107,148);
pslot5.setBorder(BorderFactory.createLineBorder(Color.pink));
contentPane.add(pslot5);

pslot4 = new JLabel("");
pslot4.setBounds(200,300,107,148);
pslot4.setBorder(BorderFactory.createLineBorder(Color.yellow));
contentPane.add(pslot4);

pslot3 = new JLabel("");
pslot3.setBounds(150,300,107,148);
pslot3.setBorder(BorderFactory.createLineBorder(Color.green));
contentPane.add(pslot3);

pslot2 = new JLabel("");
pslot2.setBounds(100,300,107,148);
pslot2.setBorder(BorderFactory.createLineBorder(Color.blue));
contentPane.add(pslot2);

pslot1 = new JLabel("");
pslot1.setBounds(50,300,107,148);
pslot1.setBorder(BorderFactory.createLineBorder(Color.white));
contentPane.add(pslot1);

// this shows the dealers score [field]  
dscore = new JTextField("Dealers Score:");
dscore.setBounds(50,250,100,25);
dscore.addActionListener(this);
contentPane.add(dscore);

// this shows the players score [field]
pscore = new JTextField("Players Score:");
pscore.setBounds(50,500,100,25);
pscore.addActionListener(this);
contentPane.add(pscore);

bg = new JLabel();
bg.setBounds(0,0,743,700);
bg.setBorder(BorderFactory.createLineBorder(Color.black));
contentPane.add(bg);
bg.setIcon(new ImageIcon("black.jpg"));
}
public static void main(String args[]) {
    table frame = new table();
frame.setVisible(true);
}
// this will show the dealer and player cards when deal button is pressed
public void actionPerformed(ActionEvent e){
    if(e.getSource() instanceof JButton){
        Button clicked = (JButton)e.getSource();
if(clicked == deal) {
    
    {
        dslot1.setIcon(as);
        pslot1.setIcon(ths);
dslot2.setIcon(backofcard);
pslot2.setIcon(fos);
    }
}


    }

}
}



Errors:
1. In line 063, I'm getting the error it's an "invalid method declaration, return type required." What kind of return type would I use for that, and where would I put it?

2. In line 164, it says that for both "table's", it "cannot find symbol." How do I fix is so that it can?

3. In line 170, "Button clicked = (JButton)e.getSource();" it says that there's "incomparable types, java.awt.Button required, javax.swing.JButton found". I have both. Can I not use them simultaneously?

4. In line 171, "if(clicked == deal) {", it also says that there's "incomparable types: java.awt.Button and javax.swing.JButton". So do I need or not need those things?

:wacko:

This post has been edited by lmharmon: 08 October 2011 - 07:16 AM


Is This A Good Question/Topic? 0
  • +

Replies To: BlackJack Game Code Errors

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: BlackJack Game Code Errors

Posted 08 October 2011 - 07:22 AM

1 - Fairly self explanatory. Methods need a return type: void, int, String, etc. Or is it supposed to be a class?

2 - You can't create an object from the method, table(). Is table() supposed to be a constructor? Did you rename the BlackJack class from Table? No, that doesn't make sense.

3 - Using both AWT and Swing components in the same application is not advised. Why would you want to?

4 - Change all AWT components to Swing components.
Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: BlackJack Game Code Errors

Posted 08 October 2011 - 07:24 AM

Return type is missing in your metho declaration
Was This Post Helpful? 0
  • +
  • -

#4 lmharmon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-September 11

Re: BlackJack Game Code Errors

Posted 08 October 2011 - 08:51 AM

1. I don't know how to right a return statement for this particular type of message.

2. How do change the AWT components to Swing without having to rewrite my entire code?
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: BlackJack Game Code Errors

Posted 08 October 2011 - 09:00 AM

I think you mean the table() method as a constructor, so it should have the same name as the class name, so: public BlackJack(){
Change Button to JButton and so on
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1