Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding Objects to JFrame from another class?
    primarykey
    data
    text
    <p>I'm trying to create a blackjack program for my final project in Java. I'm still very new to Java and OOD so I apologize if my problem seems very trivial to you :(</p> <p>How my program works: I have three classes so far.</p> <p><strong>main.java</strong> This class builds my frame and runs all the other methods.</p> <p><strong>cards.java</strong> This class creates an array that holds the card values and location to picture. I have a for loop in there that auto-populates it.</p> <p><strong>hits.java</strong> This class is meant to "randomly" generate a number that will represent the chosen card. The way this works is by taking the randomly created <em>int</em> and pointing it to a matching index location on the array.</p> <p>I assign the value to string objects that I then try to add to a jlabel and then add that jlabel to my main frame. The code is as follows:</p> <p><strong>hits.java</strong></p> <pre><code>// Import necessary classes. import java.util.Random; public class hits { // Create random object. Random rand = new Random(); // Declare variables. int card; String cardVal, cardPic; // Instantiate the needed classes. main s = new main(); cards t = new cards(); // Constructor for the class. public hits() { // Randomly generate a number (0 - 9). card = rand.nextInt(10); // Populate the array. t.runCards(); // Assign the cards according to the num. generated. cardVal = t.deck[card][0]; cardPic = t.deck[card][1]; } // Run Method public void runHits() { // Add the card chosen to the GUI. s.a.setText("hello"); s.dealerCards.add(s.a); } } </code></pre> <p>I have "hello" as the text for the label because I wanted to see if perhaps my array was not populating, but even that doesn't work. If it helps here is my <strong>main.java</strong> as well (constructor and main method):</p> <pre><code>// Constructor for the main class. public main() { // Setup the MAIN container. f1.getContentPane().setLayout(new GridLayout(0, 1)); f1.setSize(200, 200); f1.add(dealerName); f1.add(dealerCards); f1.add(userCards); f1.add(userName); // Setup the inner panels. dealerCards.setLayout(new GridLayout(1, 2)); dealerCards.add(b); userCards.setLayout(new GridLayout(1, 6)); userCards.add(c); userCards.add(d); } // Build the frame. public void GUILaunch() { // Display Frame f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f1.setVisible(true); } // Main method. public static void main(String args[]) { // Distribute the dealer's/player's starting hands. hits deal = new hits(); deal.runHits(); // Launch the GUI main gui = new main(); gui.GUILaunch(); } </code></pre> <p>Hopefully I have provided enough information to help you understand what's going on here. So to sum it all up: <strong>how can i add my jlabel(from another class) holding the randomly selected card to my main frame</strong></p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload