Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GridLayout

Status
Not open for further replies.

Govnor

Technical User
Sep 3, 2002
55
GB
Hi,

I am new to this forum, so please tell me if I don’t give enough information in the question.

I am making a small java project (as I am a beginner) that shuffles and deals cards,

But I cannot get my GUI to work by using GridLayout (but it works with FlowLayout)

The code is below and I have commented where it is not working


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


public class DeckOfCards extends JFrame {
private Card deck[];
private int currentCard;
private JButton dealButton, shuffleButton;
private JTextField displayCard;
private JLabel status;

public DeckOfCards() {
super( "Card Dealing Program");
String faces[] = {"Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine","Ten", "Jack", "Queen", "King"};
String suits[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
deck = new Card[52];
currentCard = -1;

for (int i = 0; i < deck.length; i++){
deck=new Card(faces[i%13], suits[i/13]);
}
Container c = getContentPane();
c.setLayout(new GridLayout(0,5,10,10));
dealButton = new JButton(&quot;Deal card&quot;);
dealButton.addActionListener(
new ActionListener(){
public void actionPerformed (ActionEvent e){
Card dealt = dealCard();
if (dealt != null){
displayCard.setText(dealt.toString());
status.setText(&quot;Card #: &quot; + currentCard);






}
else {
displayCard.setText(&quot;NO MORE CARDS TO DEAL&quot;);
status.setText(&quot;Shuffle cards to continue&quot;);
}
}
}
);
c.add(GridLayout dealButton(&quot;2&quot;)); //THIS IS NOT WORKING

shuffleButton = new JButton(&quot;Shuffle Cards&quot;);
shuffleButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
displayCard.setText(&quot;SHUFFLING....&quot;);
shuffle();
displayCard.setText(&quot;DECK IS SHUFFLED&quot;);
}
}
);
c.add(shuffleButton);//WANT THIS TO WORK (WORKS USING FLOWLAYOUT)

displayCard = new JTextField(20);

displayCard.setEditable(false);
c.add(displayCard); //WANT THIS TO WORK (SAME)

status = new JLabel();
c.add(status); //WANT THIS TO WORK (SAME)

setSize(275,50); // set the window size
show(); // show the window
}
public void shuffle(){
currentCard = -1;
for(int i = 0; i < deck.length; i++){
int j = (int)(Math.random() * 52);
Card temp = deck; // swap
deck = deck[j]; // the
deck[j] = temp; // cards
}

dealButton.setEnabled(true);
}

public Card dealCard(){
if (++currentCard < deck.length){
return deck[currentCard];
} else {
dealButton.setEnabled(false);
return null;
}
}


public static void main(String[] args) {
DeckOfCards deckOfCards = new DeckOfCards();

deckOfCards.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}
}

//Do not alter any code below this line.
final class Card {
private String face;
private String suit;

public Card(String f, String s){
face = f;
suit = s;
}

public String toString(){
return face + &quot; of &quot; + suit;
}
}
 
I was looking in API and didn't found any constructor of GridLayout where 0 as first argument has sence :)

GridLayout(int rows, int cols)
GridLayout(int rows, int cols, int hgap, int vgap) ???
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top