2016-09-30 5 views
1

私は、JFrameを使用して基本的なBlackJackウィンドウプログラムを作成し、先生が提供するカードイメージを割り当てました。私は、私がそれをコンパイルするときには全く現れないと思うものからすべてを書きました。私は何かが欠けていますか?BlackJackウィンドウプログラムが表示されない

プログラムをコンパイルすると、正常にコンパイルされますが、ライブラリBlackJack.javaでファイルを実行しても何も実行されず、プログラムが表示されません。コマンドプロンプトでは、実行されたことを示していますが、BlackJack/JFrameは画面に表示されません。

BlackJackクラスとMyJFrameクラスを調整して、JFrameがすぐに実行できるようにしましたが、動作していないようです。 以前の割り当てでは、このBlackJackクラス用のJFrameを作成する必要がありました。 BlackJackの実装が問題を引き起こしているような気がします。

注:

はあなたのコードの出発点、手動であなたのコードの実行を開始することができます:我々は

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.Iterator; 
import java.util.Scanner; 
import java.util.Vector; 
public class BlackJack extends JFrame implements ActionListener 
{ 
private JLabel[] lblPlayer = new JLabel[8]; 
private JLabel[] lblDealer = new JLabel[8]; 

private JPanel pnlPlayDeal = new JPanel(new GridLayout(2,8)); 

private DeckOfCards deck = new DeckOfCards(); 

private Vector <String> playerCardNames = new Vector<String>(); 
private Vector <String> dealerCardNames = new Vector<String>(); 

private ImageIcon[] icoPlayerHand = new ImageIcon[8]; 
private ImageIcon[] icoDealerHand = new ImageIcon[8]; 

private JButton btnDeal = new JButton("Deal"); 
private JButton btnPlayer = new JButton("Player"); 
private JButton btnDealer = new JButton("Dealer"); 
private JButton btnNew = new JButton("New"); 
private JButton btnAuthor = new JButton("Author"); 

private JPanel pnlButtons = new JPanel(new FlowLayout()); 

private int count = 1; 
private int playerval = 0; 
private int dealerval = 0; 

public void MyJFrame() 
{ 
    for(int i=0;i<8;i++) 
    { 
     lblPlayer[i] = new JLabel("Player"); 
     lblDealer[i] = new JLabel("Dealer"); 
    } 
    this.setLocationRelativeTo(null); 
    this.setSize(800, 350); 
    this.setVisible(true); 
    add(pnlPlayDeal,BorderLayout.CENTER); 
    add(pnlButtons,BorderLayout.SOUTH); 
    this.addLabels(); 
    this.addButtons(); 
    registerListeners(); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
}  
public void addLabels() 
{ 
    for(int i= 0;i<8;i++) 
    { 
     pnlPlayDeal.add(lblPlayer[i]); 
    } 
    for(int i= 0;i<8;i++) 
    pnlPlayDeal.add(lblDealer[i]);   
}  
public void addButtons() 
{ 
    pnlButtons.add(btnDeal); 
    pnlButtons.add(btnPlayer); 
    btnPlayer.setEnabled(false); 
    pnlButtons.add(btnDealer); 
    btnDealer.setEnabled(false); 
    pnlButtons.add(btnNew); 
    btnNew.setEnabled(false); 
    pnlButtons.add(btnAuthor); 
}  
public void registerListeners() 
{ 
    btnDeal.addActionListener(this); 
    btnPlayer.addActionListener(this); 
    btnDealer.addActionListener(this); 
    btnNew.addActionListener(this); 
    btnAuthor.addActionListener(this); 
}  
public int findRank(String x) 
{ 
    int result=0; 
    if(x.startsWith("Two")) 
     result=2; 
    else if(x.startsWith("Three")) 
     result=3; 
    else if(x.startsWith("Four")) 
     result=4; 
    else if(x.startsWith("Five")) 
     result=5; 
    else if(x.startsWith("Six")) 
     result=6; 
    else if(x.startsWith("Seven")) 
     result=7; 
    else if(x.startsWith("Eight")) 
     result=8; 
    else if(x.startsWith("Nine")) 
     result=9; 
    else if(x.startsWith("Ten")) 
     result=10; 
    else if(x.startsWith("Jack")) 
     result=10; 
    else if(x.startsWith("Queen")) 
     result=10; 
    else if(x.startsWith("King")) 
     result=10; 
    else if(x.startsWith("Ace")) 
     result=11; 
    return result; 
} 
public int getVal (Vector<String> v) 
{ 
    int total=0; 
    Iterator <String> iter = v.iterator(); 
    while(iter.hasNext()) 
    { 
     total+= findRank(iter.next()); 
    } 
    return total; 
} 
public void deal() 
{ 
    deck.shuffle(); 
    playerCardNames.add(deck.deal()); 
    playerCardNames.add(deck.deal()); 
    dealerCardNames.add(deck.deal()); 
    dealerCardNames.add(deck.deal()); 

    icoPlayerHand[0] = new ImageIcon("cardImages/" 
      +playerCardNames.get(0)+".gif"); 
    icoPlayerHand[1] = new ImageIcon("cardImages/" 
      +playerCardNames.get(1)+".gif"); 
    lblPlayer[0].setIcon(icoPlayerHand[0]); 
    lblPlayer[1].setIcon(icoPlayerHand[1]); 

    icoDealerHand[0] = new ImageIcon("cardImages/"+ 
      dealerCardNames.get(0)+".gif"); 
    icoDealerHand[1] = new ImageIcon("cardImages/card.gif"); 
    lblDealer[0].setIcon(icoDealerHand[0]); 
    lblDealer[1].setIcon(new ImageIcon("cardImages/card.gif")); 

    btnDeal.setEnabled(false); 
    btnPlayer.setEnabled(true); 
    count ++; 
}  
public void player() 
{ 
    while(getVal(playerCardNames)<22) 
    { 
     String choice = JOptionPane.showInputDialog(null, "you have " 
       +getVal(playerCardNames)+" h/s",null); 
     if(choice.equalsIgnoreCase("h")) 
     { 
      playerCardNames.add(deck.deal()); 
      icoPlayerHand[count] = new ImageIcon("cardImages/" 
        +playerCardNames.lastElement()+".gif"); 
      lblPlayer[count].setIcon(icoPlayerHand[count]); 
      count++; 
     } 
     else 
     break; 
    } 
    if(getVal(playerCardNames)>21) 
    { 
     JOptionPane.showMessageDialog(this, "You Busted"); 
     btnPlayer.setEnabled(false); 
     btnNew.setEnabled(true); 
    } 
    else 
    { 
     btnPlayer.setEnabled(false); 
     btnDealer.setEnabled(true); 
    } 
}  
public void dealer() 
{ 
    btnDealer.setEnabled(false); 
    count=1; 
    icoDealerHand[count] = new ImageIcon("cardImages/" 
      +dealerCardNames.lastElement()+".gif"); 
    lblDealer[1].setIcon(icoDealerHand[count]); 
    count ++; 
    while(getVal(dealerCardNames)<17) 
    { 
     dealerCardNames.add(deck.deal()); 
     icoDealerHand[count] = new ImageIcon("cardImages/" 
       +dealerCardNames.lastElement()+".gif"); 
     lblDealer[count].setIcon(icoDealerHand[count]); 
     count ++; 
    } 
    if(getVal(dealerCardNames)>21) 
     JOptionPane.showMessageDialog(this, "Dealer bust you won"); 
    else 
    whoWon(); 
    btnNew.setEnabled(true); 
}    
public void whoWon() 
{ 
    if(getVal(playerCardNames)>getVal(dealerCardNames)) 
     JOptionPane.showMessageDialog(this,"You won"); 
    else 
     JOptionPane.showMessageDialog(this,"You lost"); 
}  
public void newGame() 
{ 
    this.setVisible(false); 
    BlackJack game = new BlackJack(); 
} 
public static void main(String[]args) 
{ 
    MyJFrame table= new MyJFrame(); 
    BlackJack first = new BlackJack(); 
} 
public void actionPerformed(ActionEvent e) 
{ 
    if(e.getSource()==btnAuthor) 
     ((JButton)e.getSource()).setText("Conner M"); 
    if(e.getSource()==btnDeal) 
     deal(); 
    if(e.getSource()==btnPlayer) 
     player(); 
    if(e.getSource()==btnDealer) 
     dealer(); 
    if(e.getSource()==btnNew) 
    { 
     newGame();    
    }   
} 
} 
+0

こんにちは、Conner、StackOverflowへようこそ。あなたの質問には、私たちがあなたを助けるための正しい情報を持っていません。例えば私は、「コンパイルするときに何も表示されません」ということを意味していません。これまでに問題を解決しようとしたことを教えてくれませんでした。 http:// stackoverflowを読んでください。あなたが達成しようとしていること、それを解決しようとしたこと、それがうまくいかないことを知る方法** – Tim

+0

Simplify JFrameを表示しようとしている場所へのあなたの例。 JFramesをWeb上に表示する簡単な例を以下に示します:http://alvinalexander.com/java/jframe-example setVisible(true )、そして例がそれを設定する場所と場所。 –

+0

ここでお手伝いできる[デバッグのヒント](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)をご覧ください。あなたが確信していない特定の問題を引き起こした場合は、その問題について尋ねてください。 –

答えて

0

A.分析を使用するカードイメージのためDeckOfCardsとgifファイルと呼ばれるJavaファイルを与えられていますMyFrameのオブジェクトが初期化されているのを見ることができるstatic void mainです。 BlackJackのオブジェクトが初期化されています。

B.問題:

  1. あなたのコード内で宣言している何をMyFrameクラスがありません。
  2. ブラックジャックはJFrameを拡張しているので、BlackJackのコンストラクタではなく、MyJFrame()メソッドで行われるsetVisible()メソッドを介して参照を表示する必要があります。
  3. メソッドであるnewGame()がもう一度新しいBlackJackオブジェクトを作成していますが、独自のコンストラクタを作成していません。メソッドMyJFrameをBlackJackコンストラクタに変換する必要があります。

C.考えられる解決策:

  1. がメインで「最初の」ローカルブラックジャックの変数を作成する
  2. 代わりにブラックジャックのコンストラクタに静的な無効メイン()
  3. 変換方法void MyJFrame()からMyJFrame table= new MyJFrame();を削除します()メソッドを作成し、それをクラスメンバ(main()の外側で宣言)にし、main()で初期化します。
  4. ブラックジャックの変数ではなく、「最初の」新しいローカル変数を作成する、方法newGame()で、今クラスのメンバーであるため、代わりにこれを行う:first = new BlackJack();

注:あなたのコードはより多くのバグを持っていることを。上記の解決策は、なぜフレームを表示していないのかという問題を解決する可能性があります。それを修正するには1つ以上の方法がありますが、私は今のところ最良の方法しか提供していません。

関連する問題