2011-05-19 108 views
1

我正在用Java創建一個三卡撲克遊戲。 我基本上已經完成了編碼工作,但是我遇到了一些GUI問題。 這裏是我的顯卡部分代碼:需要關於java的幫助GUI

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.awt.image.*; 
import java.util.Scanner; 

import javax.*; 

public class Board extends dealer{ 
    final static boolean shouldFill = true; 
    final static boolean shouldWeightX = true; 
    final static boolean RIGHT_TO_LEFT = false; 
    public static Image cardback; 
    public static Image comp1; 
    public static Image comp2; 
    public static Image comp3; 
    public static Image play1; 
    public static Image play2; 
    public static Image play3; 
    public static int x,y; 
    public static int numgames; 
    public static int bet; 
    public static int wlt=7; 
    public static int dealorfold=1; 
    public static int showcard=1; 
    static int playermoney; 
    static int compmoney; 
    protected static hand player; 
    protected static hand computer; 
    static class deal implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      dealorfold=2; 
     } 
    } 

    static class fold implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      dealorfold=3; 
     } 
    } 

    static class allin implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      bet=playermoney; 
     } 
    } 

    static class ten implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      if(playermoney<10) 
       wlt=9;   
      else 
       bet=10; 
     } 
    } 

    static class fifty implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      if(playermoney<50) 
       wlt=9;   
      else 
       bet=50; 
     } 
    } 

    static class hundred implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      if(playermoney<100) 
       wlt=9;   
      else 
       bet=100; 
     } 
    } 

    static class fivehund implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      if(playermoney<500) 
       wlt=9;   
      else 
       bet=500; 
     } 
    } 

    static class thaud implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      if(playermoney<1000) 
       wlt=9;   
      else 
       bet=1000; 
     } 
    } 

    static class fivethaud implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      if(playermoney<5000) 
       wlt=9;   
      else 
       bet=5000; 
     } 
    } 

    static class tenthaud implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      if(playermoney<10000) 
       wlt=9;   
      else 
       bet=10000; 
     } 
    } 

    private static void createAndShowGUI() { 
     setcards(); 
     JFrame frame = new JFrame("Three Card Poker"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setPreferredSize(new Dimension(1366,768)); 
     addComponentsToPane(frame.getContentPane()); 
     frame.pack(); 
     frame.setVisible(true); 

     int p1 = checkpattern(player); 
     int c1 = checkpattern(computer); 
     while(dealorfold==1){ 
      if(dealorfold==2){ 
       if(p1>c1){ 
        compmoney = compmoney - bet; 
        playermoney = playermoney + bet; 
        showcard=2; 
        wlt=1; 
       } 
       else if (p1<c1){ 
        compmoney = compmoney + bet; 
        playermoney = playermoney - bet; 
        showcard=2; 
        wlt=2; 
       } 
       else if(p1==0 && c1==0){ 
        if(checkhighercardinhand(player, computer)==0){ 
         compmoney = compmoney - bet; 
         playermoney = playermoney + bet; 
         showcard=2; 
         wlt=1; 

        } 
        else if(checkhighercardinhand(player, computer)==1){ 
         compmoney = compmoney + bet; 
         playermoney = playermoney - bet; 
         showcard=2; 
         wlt=2; 
        } 
        else if(checkhighercardinhand(player, computer)==2){ 
         showcard=2; 
         wlt=3; 
        } 
       } 
       else if(p1==1 && c1==1){ 
        if(checkhigherpair(player, computer)==0){ 
         compmoney = compmoney - bet; 
         playermoney = playermoney + bet; 
         showcard=2; 
         wlt=1; 
        } 
        else if(checkhigherpair(player, computer)==1){ 
         compmoney = compmoney + bet; 
         playermoney = playermoney - bet; 
         showcard=2; 
         wlt=2; 
        } 
        else if(checkhigherpair(player, computer)==2){ 
         showcard=2; 
         wlt=3; 
        } 
       } 
       else if(p1 == c1 && p1 >= 3){ 
        int check = checkhigherthanstraight(player, computer); 

        if(check == 0){ 
         compmoney = compmoney - bet; 
         playermoney = playermoney + bet; 
         showcard=2; 
         wlt=1; 
        } 
        else if (check == 1){ 
         compmoney = compmoney + bet; 
         playermoney = playermoney - bet; 
         showcard=2; 
         wlt=2; 
        } 
        else{ 
         showcard=2; 
         wlt=3; 
        } 
       } 
       else if(p1 == c1 && p1 == 2){ 
        showcard=2; 
        wlt=3; 
       } 
      } 

      else{ 
       playermoney=playermoney-50; 
       showcard=2; 
       wlt=4; 
      }     
     } 
    } 

    public static void addComponentsToPane(Container pane) { 
     if (RIGHT_TO_LEFT) { 
      pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
     } 

     pane.setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 

     JLabel comp = new JLabel("Computer:"); 
     if (shouldWeightX) { 
      c.weightx = 0.5; 
     } 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 0; 
     c.insets=new Insets(0,0,100,0); 
     pane.add(comp, c); 

     JLabel c1 = new JLabel(computer.get(0)+""); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 1; 
     c.gridy = 0; 
     pane.add(c1, c); 

     JLabel c2 = new JLabel(computer.get(1)+""); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 2; 
     c.gridy = 0; 
     pane.add(c2, c); 

     JLabel c3= new JLabel(computer.get(2)+""); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 3; 
     c.gridy = 0; 
     pane.add(c3, c); 

     JLabel compcash=new JLabel("Dealer has: "+compmoney); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 6; 
     c.gridy = 1; 
     pane.add(compcash, c); 

     JLabel player1 = new JLabel("Player:"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx=.5; 
     c.gridx = 0; 
     c.gridy = 2; 
     pane.add(player1, c); 

     JLabel p1 = new JLabel(player.get(0)+""); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 1; 
     c.gridy = 2; 
     pane.add(p1, c); 

     JLabel p2 = new JLabel(player.get(1)+""); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 2; 
     c.gridy = 2; 
     pane.add(p2, c); 

     JLabel p3 = new JLabel(player.get(2)+""); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 3; 
     c.gridy = 2; 
     pane.add(p3, c); 

     JLabel playcash=new JLabel("You have: "+playermoney); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 6; 
     c.gridy = 2; 
     pane.add(playcash, c); 

     JLabel pattern=new JLabel("Your hand is: "); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 6; 
     c.gridy = 3; 
     pane.add(pattern, c); 

     JButton deal = new JButton("Deal"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 0; 
     c.gridy = 5; 
     pane.add(deal, c); 
     deal.addActionListener(new deal()); 

     JButton fold = new JButton("Fold(Penalty: minus $50)"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 1; 
     c.gridy = 5; 
     pane.add(fold, c); 
     fold.addActionListener(new fold()); 

     JLabel bet = new JLabel("    Bet:"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 2; 
     c.gridy = 5; 
     pane.add(bet, c); 

     JButton allin = new JButton("All IN"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 3; 
     c.gridy = 5; 
     pane.add(allin, c); 
     allin.addActionListener(new allin()); 

     JButton ten= new JButton("10"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 4; 
     c.gridy = 5; 
     pane.add(ten, c); 
     ten.addActionListener(new ten()); 

     JButton fifty = new JButton("50"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 5; 
     c.gridy = 5; 
     pane.add(fifty, c); 
     fifty.addActionListener(new fifty()); 

     JButton hundred = new JButton("100"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 6; 
     c.gridy = 5; 
     pane.add(hundred, c); 
     hundred.addActionListener(new hundred()); 

     JButton fivehund = new JButton("500"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 7; 
     c.gridy = 5; 
     pane.add(fivehund, c); 
     fivehund.addActionListener(new fivehund()); 

     JButton thaud = new JButton("1000"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 8; 
     c.gridy = 5; 
     pane.add(thaud, c); 
     thaud.addActionListener(new thaud()); 

     JButton fivethaud = new JButton("5000"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 9; 
     c.gridy = 5; 
     pane.add(fivethaud, c); 
     fivethaud.addActionListener(new fivethaud()); 

     JButton tenthaud = new JButton("10000"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 10; 
     c.gridy = 5; 
     pane.add(tenthaud, c); 
     tenthaud.addActionListener(new tenthaud()); 

     String mess; 
     if(wlt==1) 
      mess="You Win The Round!"; 
     else if(wlt==2) 
      mess="Dealer Wins The Round!"; 
     else if(wlt==3) 
      mess="You And Dealer Tie The Round"; 
     else if(wlt==4) 
      mess="You Folded. You Recieve Penalty of Minus $50"; 
     else if(wlt==5) 
      mess="Dealer Loses All Of Its Money, YOU WIN THE WHOLE GAME!"; 
     else if(wlt==6) 
      mess="You Lose All Of You Money, You Lose The Whole Game!"; 
     else if(wlt==7) 
      mess="Please Click Deal Or Fold"; 
     else if(wlt==8) 
      mess="Please Place Your Bet"; 
     else 
      mess="Please Select Another Bet."; 

     JLabel message = new JLabel(mess); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0; 
     c.gridx = 6; 
     c.gridy = 4; 
     pane.add(message, c); 

     JLabel label = new JLabel(""); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 40; 
     c.weightx = 0.0; 
     c.gridwidth = 3; 
     c.gridx = 0; 
     c.gridy = 2; 
     pane.add(label, c); 

     label = new JLabel(""); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 0;  
     c.weighty = 1.0; 
     c.anchor = GridBagConstraints.PAGE_END; 
     c.insets = new Insets(10,0,0,0); 
     c.gridx = 1;  
     c.gridwidth = 2; 
     c.gridy = 2;  
     pane.add(label, c); 
    } 

    public static void setmoney(){ 
     compmoney=10000000; 
     playermoney=10000000; 
    } 

    public static void setcards(){ 
     player = new hand(); 
     computer= new hand(); 
     player.addCard(new cards()); 
     player.addCard(new cards()); 
     player.addCard(new cards()); 
     computer.addCard(new cards()); 
     computer.addCard(new cards()); 
     computer.addCard(new cards()); 
     cardswoprob.correctduplicates(player, computer); 
    } 

    public static void play(){ 
     for(int index=0;compmoney>=10&&playermoney>=10;index++){ 
      wlt=7; 
      int p1 = checkpattern(player); 
      int c1 = checkpattern(computer); 
      while(dealorfold==1){ 
       if(dealorfold==2){ 
        if(p1>c1){ 
         compmoney = compmoney - bet; 
         playermoney = playermoney + bet; 
         showcard=2; 
         wlt=1; 
        } 
        else if (p1<c1){ 
         compmoney = compmoney + bet; 
         playermoney = playermoney - bet; 
         showcard=2; 
         wlt=2; 
        } 
        else if(p1==0 && c1==0){ 
         if(checkhighercardinhand(player, computer)==0){ 
          compmoney = compmoney - bet; 
          playermoney = playermoney + bet; 
          showcard=2; 
          wlt=1; 

         } 
         else if(checkhighercardinhand(player, computer)==1){ 
          compmoney = compmoney + bet; 
          playermoney = playermoney - bet; 
          showcard=2; 
          wlt=2; 
         } 
         else if(checkhighercardinhand(player, computer)==2){ 
          showcard=2; 
          wlt=3; 
         } 
        } 
        else if(p1==1 && c1==1){ 
         if(checkhigherpair(player, computer)==0){ 
          compmoney = compmoney - bet; 
          playermoney = playermoney + bet; 
          showcard=2; 
          wlt=1; 
         } 
         else if(checkhigherpair(player, computer)==1){ 
          compmoney = compmoney + bet; 
          playermoney = playermoney - bet; 
          showcard=2; 
          wlt=2; 
         } 
         else if(checkhigherpair(player, computer)==2){ 
          showcard=2; 
          wlt=3; 
         } 
        } 
        else if(p1 == c1 && p1 >= 3){ 
         int check = checkhigherthanstraight(player, computer); 

         if(check == 0){ 
          compmoney = compmoney - bet; 
          playermoney = playermoney + bet; 
          showcard=2; 
          wlt=1; 
         } 
         else if (check == 1){ 
          compmoney = compmoney + bet; 
          playermoney = playermoney - bet; 
          showcard=2; 
          wlt=2; 
         } 
         else{ 
          showcard=2; 
          wlt=3; 
         } 
        } 
        else if(p1 == c1 && p1 == 2){ 
         showcard=2; 
         wlt=3; 
        } 
       } 

       else{ 
        playermoney=playermoney-50; 
        showcard=2; 
        wlt=4; 
       }     
      } 
     } 
     if(playermoney > compmoney) 
      wlt=5; 
     else 
      wlt=6; 
    } 

    public static void main(String[] args) { 
     setmoney(); 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       while(compmoney>=10&&playermoney>=10) 
       createAndShowGUI(); 
       if(playermoney > compmoney) 
        wlt=5; 
       else 
        wlt=6; 
      } 
     }); 
    } 
} 

的主要問題是,在createAndShowGUI(),如果我把while循環,這是我試過(for循環,do循環)那裏,JPanel將剛剛出現,但沒有在那裏。如果在那裏沒有while循環,那麼JPanel將定期顯示在那裏的所有東西。 但我需要while循環內的代碼,因爲代碼基本上是遊戲。

我試圖做一個自己的方法(play()),但如果我在main()或任何地方調用它,JPanel將是白色(無)。

這是我的第二個問題:如果刪除while循環並且JPanel正常顯示,您將看到計算機的手和玩家的手,但是是以文本形式。我想知道如何用圖像替換文字。

+2

您不希望事件驅動程序中出現繁忙循環。 – sje397 2011-05-19 03:54:05

+5

重寫代碼並擺脫所有靜態變量和方法。 Thast不是設計程序的方式。 – camickr 2011-05-19 04:26:47

+2

而且,請遵循通常的Java命名約定,以便爲類名使用!沒有Java程序員會想要在當前狀態下檢查你的代碼。 – jfpoilpret 2011-05-19 06:59:02

回答

0

對於GUI,你應該實現Runnable,並使用SwingUtilities.invokeLater(Runnable)

public class Foo implements Runnable { 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Foo()); 
    } 

    public void run() { 
     ... 
    } 
} 

爲了使用圖像,您應該使用ImageIcon類:

ImageIcon imageIcon = new ImageIcon("<path to image>"); 

或者相反,如果你已經有一個Image物件:

ImageIcon imageIcon = new ImageIcon(image); 

然後顯示它:

JLabel imageLabel = new JLabel(imageIcon); 
1

有幾件事情,並不是所有的人都直接GUI相關:

失去靜態成員,他們不應該在那裏,我假設你使用它們,因爲一切從主要方法調用,但這不是要走的路。 Java是一種面向對象的編程語言,創建類,實例化和使用它們,不要在一個類中執行所有的工作,甚至沒有實例化。

重構重複代碼,而不是創建不同的類與一個值的差異,創建更多的泛型類。例如,而不是5不同類這樣的:

static class ten implements ActionListener{ 
    public void actionPerformed(ActionEvent e){ 
     if(playermoney<10) 
      wlt=9;   
     else 
      bet=10; 
    } 
} 

聲明一類這樣的:

static class ten implements ActionListener{ 
    int money; 
    public ten(int money) { 
     this.money = money 
    } 
    public void actionPerformed(ActionEvent e){ 
     if(playermoney<money) 
      wlt=9;   
     else 
      bet=money; 
    } 
} 

,並根據情況用適當int實例化。

讓應用程序事件驅動。由於玩家必須進行交互,因此不要使用較大的while循環,而是從聽衆觸發事件(例如,現在發生什麼事情)。用戶按下一個按鈕)

與上一點相關,不要在UI線程上執行很長的操作(如整個程序),它不會讓UI更新,在後臺線程上執行所有工作,並且當您需要更改UI時,請使用SwingUtilities.invokeLater(Runnable)。我想你可能會發現this tutorial有幫助。