开发者

Adding Objects to JFrame from another class?

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 tr开发者_开发知识库ivial to you :(

How my program works: I have three classes so far.

main.java This class builds my frame and runs all the other methods.

cards.java 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.

hits.java 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 int and pointing it to a matching index location on the array.

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:

hits.java

// 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);
}
}

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 main.java as well (constructor and main method):

// 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();
}

Hopefully I have provided enough information to help you understand what's going on here. So to sum it all up: how can i add my jlabel(from another class) holding the randomly selected card to my main frame

Thanks in advance.


The deal.runHits() adds a label to the Main object that deal owns rather than the gui object.

I would suggest the following :

Make your main class have an instance of hits and hits have an instance of the cards object... so you get something like this

public class main {

private hits hits_instance

//constructor

main(){ hits_instance = new hits(); }

//this method will add your cards

public void addCards(){
// frame = whatever frame you are using
frame.add(hits_instance.getCards());

}

}

public class hits {

private cards cards_instance;

hits(){ cards_instance= new cards();}

public JLabel getCards() {return cards_instance.getCard(randomNumber);}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜