자바에서 최종 프로젝트를위한 블랙 잭 프로그램을 만들려고합니다. 나는 아직도 자바 아주 새로운 해요 및 OOD 그래서 나는 내 문제는 :(아주 사소한 것 같다면 사과다른 클래스의 JFrame에 객체 추가 중?
어떻게 내 프로그램이 작동합니다. 나는 세 가지 클래스까지
main.java 이 있습니다 클래스 내 프레임을 구축하고 다른 모든 방법을 실행합니다. cards.java 이 클래스는 카드 값과 그림을 위치를 유지하는 배열을 생성
. 내가 거기에 그게 자동으로 채워에서 루프를 가지고있다.
hits.java 이 클래스는 선택한 카드를 나타내는 숫자를 "임의로 생성"하기위한 클래스입니다. 이 작동 방식은 무작위로 생성 된 을으로 가져 와서 배열의 일치하는 인덱스 위치로 지정하는 것입니다.
값을 jlabel에 추가하려고하는 문자열 객체에 할당 한 다음 해당 jlabel을 기본 프레임에 추가합니다. 코드는 다음과 같다 :
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);
}
}
내가 "안녕하세요"나는 내 배열 채우기 아니었지만, 심지어는 아무튼 아마 있는지 확인하고 싶었 기 때문에 레이블의 텍스트로이 일하지 마라. 그것은 여기에 도움이된다면뿐만 아니라 (생성자와 주요 방법) 내 main.java입니다 :
// 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();
}
다행스럽게도 필자가 제공 한 충분한 정보가 여기에 무슨 일이 일어나고 있는지 이해하는 데 도움이. 그래서 모두 다 합쳐서 : 어떻게 내 메인 프레임에 무작위로 선택한 카드를 들고 내 jlabel (다른 클래스에서)을 추가 할 수
미리 감사드립니다.
같은 것이 무엇을 얻을 'a' 속성을 다음과 같이 참조하십시오 :'sasetText ("hello");'? 내가 놓친 게 있니? 내 JLabel의 이름 인 –
... 죄송합니다. 나는 그것을 나의 메인 클래스에서 선언했다. – lemonpole
자바 명명 규칙을 배우고 그들에게 붙인다. – kleopatra