2016-08-23 4 views
1

ネットワークポーカーサーバークライアントプログラムを作成しようとしていますが、現在グラフィックス部分を含むクライアント側を作成していますが、コード内のJPanelにコンポーネントを追加しようとするとrunメソッドで特定の条件が満たされていますが、addメソッドが動作していないようですが、同じ条件でJPanelを操作する他のメソッドが機能します。Javaグラフィックスメソッドを追加する

public class PokerClient { 

BufferedReader in; 
PrintWriter out; 
JFrame frame = new JFrame("Poker"); 

JPanel playerHandPanel; 

String serverAddress = "localhost"; 
String playerName; 

Card playerHand1, playerHand2; 


public PokerClient() { 

    // Layout GUI 
    frame.setSize(1100, 700); 
    frame.setResizable(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    playerHandPanel = new JPanel(new GridLayout(1, 2)); 
    playerHandPanel.setPreferredSize(new Dimension(600, 300)); 
    playerHandPanel.add(new CardComponent(new Card(3, Suit.CLUB))); //it works here 
    playerHandPanel.setVisible(true); 

    frame.add(playerHandPanel, BorderLayout.NORTH); 
    frame.setVisible(true); 
} 

/** 
* Prompt for and return the desired screen name. 
*/ 
private String getName() { 

    return JOptionPane.showInputDialog(
     frame, 
     "Choose a screen name:", 
     "Screen name selection", 
     JOptionPane.PLAIN_MESSAGE); 
} 

private Card constructCard(String line){ 
    int seperator = line.indexOf('/'); 
    int cardNum = Integer.parseInt(line.substring(0, seperator)); 
    Card card; 
    if(line.substring(seperator+1).startsWith("S")){ 
     card = new Card(cardNum, Suit.SPADE); 
    } else if(line.substring(seperator+1).startsWith("C")){ 
     card = new Card(cardNum, Suit.CLUB); 
    } else if(line.substring(seperator+1).startsWith("D")){ 
     card = new Card(cardNum, Suit.DIAMOND); 
    } else{ 
     card = new Card(cardNum, Suit.HEART); 
    } 
    System.out.println(card.toString()); 
    return card; 
} 

/** 
* Connects to the server then enters the processing loop. 
*/ 
private void run() throws IOException { 

    Socket socket = new Socket(serverAddress, 9050); 
    in = new BufferedReader(new InputStreamReader(
     socket.getInputStream())); 
    out = new PrintWriter(socket.getOutputStream(), true); 

    // Process all messages from server, according to the protocol. 
    while (true) { 
     String line = in.readLine(); 
     System.out.println(line); 
     if (line.startsWith("SUBMITNAME")) { 
//    String name = getName(); 
//    playerName = name; 
//    out.println(name); 
     } else if (line.startsWith("p1")) { 
      playerHandPanel.add(new CardComponent(new Card(4, Suit.SPADE)));//this doesn't work i can't figure out why 
      playerHandPanel.setBackground(Color.WHITE);//this worked 
      playerHandPanel.add(new JLabel("is this added"));//this doesn't work either 
      playerHandPanel.repaint(); 
     } 
    } 
} 


public static void main(String[] args) throws Exception { 
    PokerClient client = new PokerClient(); 
    client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    client.frame.setVisible(true); 
    client.run(); 
} 
} 

答えて

3

いくつかの問題が飛び出す:

  • あなたがコンポーネントを追加または削除した後playerHandPanelにrevalidate()を呼び出していない - おそらくあなたの問題に大きく貢献。あなたがplayerHandPanelのサイズ人為的に
  • を制約していて、「あなたのコードは、Swingのイベントスレッドのオフ主要なSwingコンポーネントの状態の変更を行うことで、スレッドのルールをスイング誇示かEDT
  • JScrollPaneに
  • にそれを入れていない
  • 制約レイアウトを使用して再、new GridLayout(1, 2)

考えられる解決策:

  • はい、コンポーネントを追加または削除した後、playerHandPanelにrevalidate()を呼び出してください。これは、レイアウトマネージャに自分のことをさせることになります。
  • GridLayoutを使用する場合は、new GridLayout(1, 0)またはnew GridLayout(1, 0)のように、列または行の数を指定するかどうかに応じて柔軟に変更します(0は可変数の列または行を意味します)
  • JListまたはJTableを使用することを検討してください。
  • SwingイベントのスレッドでSwing状態の変更(コンポーネントの追加や削除、背景色の変更など)を行うことだけを含め、Swingのスレッドルールに従ってください。
+0

ありがとうございました。スイングスレッディングルールはどこにありますか?私は自分がコードする方法を教えているので、私が知っていることは、このようなウェブサイトのビットとピースに基づいています。 – Mike

+0

@Mike:[レッスン:同時実行のスイング](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)をご覧ください。 –

関連する問題