Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6954947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:40:25+00:00 2026-05-27T14:40:25+00:00

Ok so I have two classes in which they are not really associated with

  • 0

Ok so I have two classes in which they are not really associated with each other.
One is the graphic the other in inputs(by terminal using scanners). I want to replace the scanners with a JTextField but I am having a hard time doing so..

Im a bit lost here….

Here is class GUI

//Constructor to create the UI components
 public UnoGraphics() {

  //JButtons---------------------------- 
  viewCards = new JButton("Move Card"); 
  input = new JTextField(5);

  //Creates a canvas and set the properties 
  canvas = new DrawCanvas();
  canvas.setPreferredSize(new Dimension(CANVAS_WIDTH,CANVAS_HEIGHT));
  this.setContentPane(canvas);
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);


//This is How I was thinking of implementing my input------------HERE---------------->
  input.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
             inputText = input.getText();
         }});//End ActionListener()

  this.pack();
  this.setTitle("Uno!");
  this.setVisible(true);
 }//End Constructor

 //Custom drawing canvas (designed as inner class). This is were we draw/change the cards
  class DrawCanvas extends JPanel {
   // Custom drawing codes--------------
    @Override
   public void paintComponent(Graphics g) {

      //Set the background to black
      super.paintComponent(g);
      setBackground(Color.black);
     //cards being drawn-------------------
     //buttons and text fields
     add(viewCards);
     add(input);
   }//End PaintComponent()
  }//End DrawCavas()
 }//End Program 

Class Two Inputs with scanners:

  public class CommandLinePlayer extends Player 
  {


//private String inputText;
// constructor
public CommandLinePlayer(String aname) 
{
    //super aname is from the super class player;
    super(aname);

}


    // command line player can also say uno.  This uses scanner(reads user in puts from keyboard) and the response should be typed in
    public boolean sayUno()
    {
        System.out.println("Would you like to say uno?  Yes or No");
        Scanner scan = new Scanner(System.in);
        String yes = scan.next();
        // returns response 
        //returns yes if the user types yes and ignores the case
        return yes.equalsIgnoreCase("Yes");
    }

    //this method is the choose card of type int takes int one argument of type Card
    // command line version (normal player on command line)
    //this method takes in the card from your hand and "sends" it to the controller
    protected int chooseCard(Card topCard)
    {
        // display hand
        System.out.println("\nHere is the topCard: " + topCard);

        System.out.println("Your hand has:");

        // loops through the players(commandlineplayer) hand and prints out the players cards.  Index could start at 0, but 1 would be the first card
        for(int index = 0; index < numOfCards; index ++)
        {
            System.out.println("Card # " + index + ": " + hand[index]);

        }
        // choose Card prompts the player to match, or pick a card based on the index, and then press enter.
        // if a card does not match the topcard, a key corresponding to any card can be pressed.  This would automatically add a
        // card to a players hand.
        System.out.println("Play a card #. If you don't have a card to play, choose any card # to draw.");
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        return num;

    }


    // this is the choose color method for the command line player but only if it is a wild card does this method takes place
    // command line player can choose a cards color based on the options displayed on the screen.(System.out.println...statements)  
    public Card.Color chooseColor()
    {
        // choose a color using scanner
        Scanner scanin = new Scanner(System.in);

        System.out.println("Choose a color by pressing a number corresponding to your choice:");

        System.out.println("Your options are 1.Red 2.Green 3.Yellow 4. Blue");

        // the switch corresponds a number (color) to the cases, and returns a chosen card.  
        int color = 0;
        color = scanin.nextInt();
        switch (color) 
        {
            case 1: System.out.println("The color you chose is: Red");
                    return Card.Color.Red;
            case 2: System.out.println("The color you chose is: Green");
                    return Card.Color.Green;
            case 3: System.out.println("The color you chose is: Yellow");
                    return Card.Color.Yellow;
            case 4: System.out.println("The color you chose is: Blue");
                    return Card.Color.Blue;
            default: System.out.println("NONE");
        }

           return Card.Color.None;          
    }

    public class inputListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            //I Was Thinking something like this-----------HERE-----
        }
    }
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:40:25+00:00Added an answer on May 27, 2026 at 2:40 pm

    Here are the relevant chunks of code you need, you should be able to integrate them into your code yourself.

    public class Controller {
        public void startMethod() {
            final UIClass myUI = new UIClass();
            myUI.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    handleUIInformation(myUI);
                }
            }
        }
    
        private void handleUIInformation(UIClass myUI) {
            String textval = myUI.textField.getText();
            // here you do whatever you want with the text
        }
    }
    
    public class UIClass {
        JButton button;
        JTextField textField;
        public UIClass() {
            button = new JButton();
            textField = new JTextField();
            textField.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    if(e.getKeyCode() == KeyEvent.VK_ENTER) {
                        button.doClick();
                    }
                }
            });
        }
    
        public void addActionListener(ActionListener al) {
            button.addActionListener(al);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes one of which inherits from the other. Im trying to
I have a class which has two HashSet<String> collections as private members. Other classes
I have two classes that each need an instance of each other to function.
I have two arraylists, one contains Strings and the other contains Classes that have
I am using Mysql. And I have two tables which are one-to-one related with
I have two related classes which share a common interface and are both stored
I have two Java classes: B, which extends another class A, as follows :
I have a MustInherit Parent class with two Child classes which Inherit from the
I have an abstract class Airplane, and two classes PassengerAirplane and CargoAirplane, which extend
I have two classes, and want to include a static instance of one class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.