Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need to add this stuff into the Java code that is pasted in bold . Textbox to al

ID: 3806758 • Letter: N

Question

Need to add this stuff into the Java code that is pasted in bold .

Textbox to allow the user to enter the number of cards they want to display (1-54)

Button to generate random cards based on the number entered by user. Can be clicked multiple times within the same session to regenerate the cards.

Base the number of rows/columns on how many cards are shown

Place the textbox/button at the top of the frame and have the cards show below them.

package Cards;

import javax.swing.*;

import java.awt.*;

import java.util.*;

import java.awt.event.*;

public class Cards {

public static void main (String [] args)

{

   int min = 1, max = 54;

      

   JFrame frame = new JFrame ("Cards");

   frame.setTitle("Cards");

   frame.setSize(310,150);

   frame.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));

   Random rand = new Random();

   for (int x = 0; x < 3;x++)

      {

       int randomNum = rand.nextInt((max - min) + 1) + min;

       frame.add(new JLabel(new ImageIcon("card/" + randomNum + ".png")));

      }

   frame.pack();

   frame.setVisible(true);

   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   }

}

Explanation / Answer

This class has been changed such that it does what you want.Thankyou

Cards.java


package Cards;


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

public class Cards extends JFrame implements ActionListener{
   //assuming each card image width = 50; I used this to base number of columns for the frame; set this value according to the width of card images
   // assuming each card image height = 500;hoping that this image can fit in this frame without any problem.
   //all I need too do is change width of frame based on number given in text field.
   // frame width = number in textfield * Image width,i.e 50, in this case;
  
   int imageWidth = 50;
   int imageHeight = 500;
   JPanel upper;
   JPanel lower;
   static JTextField noOfCards;
   static JButton generageNewCards;
   static Cards frame;
   int min = 1, max = 54;
  
   public Cards(String name){
   super(name);
   }

   public static void main (String [] args)
   {

       frame = new Cards ("Cards");

       frame.setTitle("Cards");

       frame.setSize(310,150);
      
       Container frontPanel = frame.getContentPane();
       JPanel jp = new JPanel();
       jp.setLayout(new BoxLayout(jp,BoxLayout.Y_AXIS));
          
       frame.upper = new JPanel(new FlowLayout(FlowLayout.CENTER,2,2));
       noOfCards = new JTextField(10);
       generageNewCards = new JButton("Regenerage Cards!");
       generageNewCards.addActionListener(frame);
      
       frame.upper.add(noOfCards);
       frame.upper.add(generageNewCards);
      
       jp.add(frame.upper);
      
       frame.lower = new JPanel(new FlowLayout(FlowLayout.LEFT,2,2));
      
       jp.add(frame.lower);
      
       frontPanel.add(jp);
       frame.pack();
       frame.setVisible(true);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   }

   @Override
   public void actionPerformed(ActionEvent e) {
       if(!e.getActionCommand().equalsIgnoreCase("")){
           frame.setSize(Integer.parseInt(noOfCards.getText())*imageWidth+310, imageHeight);
           frame.setVisible(true);
          
           addCardsIntoLayout();
           frame.setVisible(true);
       }
   }
  
   public void addCardsIntoLayout(){
       ThreadCards tc = new ThreadCards(frame);
       Thread t = new Thread(tc);
       t.start();
   }

}
class ThreadCards implements Runnable{
   Cards frame;
   public ThreadCards(Cards frame){
       this.frame = frame;
   }
   public void run(){
       Random rand = new Random();
       frame.lower.removeAll();
       for (int x = 0; x < Integer.parseInt(frame.noOfCards.getText());x++)
       {

           int randomNum = rand.nextInt((frame.max - frame.min) + 1) + frame.min;

           frame.lower.add(new JLabel(new ImageIcon(randomNum + ".png")));
           //frame.lower.add(new JLabel("card/" + randomNum + ".png"));
          
           frame.lower.setVisible(true);
           frame.setVisible(true);
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote