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("Deal card"
;
dealButton.addActionListener(
new ActionListener(){
public void actionPerformed (ActionEvent e){
Card dealt = dealCard();
if (dealt != null){
displayCard.setText(dealt.toString());
status.setText("Card #: " + currentCard);
}
else {
displayCard.setText("NO MORE CARDS TO DEAL"
;
status.setText("Shuffle cards to continue"
;
}
}
}
);
c.add(GridLayout dealButton("2"
); //THIS IS NOT WORKING
shuffleButton = new JButton("Shuffle Cards"
;
shuffleButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
displayCard.setText("SHUFFLING...."
;
shuffle();
displayCard.setText("DECK IS SHUFFLED"
;
}
}
);
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 + " of " + suit;
}
}
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("Deal card"
dealButton.addActionListener(
new ActionListener(){
public void actionPerformed (ActionEvent e){
Card dealt = dealCard();
if (dealt != null){
displayCard.setText(dealt.toString());
status.setText("Card #: " + currentCard);
}
else {
displayCard.setText("NO MORE CARDS TO DEAL"
status.setText("Shuffle cards to continue"
}
}
}
);
c.add(GridLayout dealButton("2"
shuffleButton = new JButton("Shuffle Cards"
shuffleButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
displayCard.setText("SHUFFLING...."
shuffle();
displayCard.setText("DECK IS SHUFFLED"
}
}
);
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 + " of " + suit;
}
}