2011-04-24 51 views
0

你好我正在寫一個黑色的插孔程序,我希望能夠顯示我製作的卡片..我導入了每張卡片的圖像,並將每個圖像分配給特定的套裝和卡片編號然後我製作了一張牌組,並將每張牌加入其中。我製作了一個方法來洗牌,並彈出第一張。每次按下按鈕時我都想顯示每張卡片,我該怎麼做?使用gui的黑色插孔程序

我的代碼...

public class CardList { 

    private Stack<Cards> list; 

    public CardList(){ 
     list = new Stack<Cards>(); 


     for(Cards.CardSuit suit:Cards.CardSuit.values()){ 
      for(int i=1; i<=13; i++){ 
       list.push(new Cards(suit, i)); 
      } 
     } 
    } 

    public Cards getCard() 
    { 
     shuffle(); 
     return list.pop(); 

    } 

    public void shuffle(){ 
     Collections.shuffle(list); 
    } 
} 


public class Cards 
{ 

    public enum CardSuit{DIAMOND, SPADE, CLUB, HEART}; 

    private BufferedImage image; 
    int num; 
    CardSuit s; 


    public Cards(CardSuit suit, int number){ 
     num = number; 
     s = suit; 
     image = createCard(suit, number); 
    } 



    public Cards() { 
     // TODO Auto-generated constructor stub 
    } 



    public BufferedImage createCard(CardSuit s , int num) 
    { 
     switch (s) 
     { 
      case DIAMOND : 
       switch (num) 
       { 
        case 1: 
         try {     
           image = ImageIO.read(new File("ad.gif")); 
          } catch (IOException ex) { 
           // handle exception... 
          } 
        case 2: 
         try {     
           image = ImageIO.read(new File("2d.gif")); 
          } catch (IOException ex) { 
           // handle exception... 
          } 
        case 3: 
         try {     
           image = ImageIO.read(new File("3d.gif")); 
          } catch (IOException ex) { 
           // handle exception... 
          } 
        case 4: 
         try {     
           image = ImageIO.read(new File("4d.gif")); 
          } catch (IOException ex) { 
           // handle exception... 
          } 
        case 5: 
         try {     
           image = ImageIO.read(new File("5d.gif")); 
          } catch (IOException ex) { 
           // handle exception... 
          } 
        case 6: 
         try {     
           image = ImageIO.read(new File("6d.gif")); 
          } catch (IOException ex) { 
           // handle exception... 
          } 
        case 7: 
         try {     
           image = ImageIO.read(new File("7d.GIF")); 
          } catch (IOException ex) { 
           // handle exception... 
          } 
        case 8: 
         try {     
           image = ImageIO.read(new File("8d.gif")); 
          } catch (IOException ex) { 
           // handle exception... 
          } 
        case 9: 
         try {     
           image = ImageIO.read(new File("9d.gif")); 
          } catch (IOException ex) { 
           // handle exception... 
          } 
        case 10: 
         try {     
           image = ImageIO.read(new File("td.gif")); 
          } catch (IOException ex) { 
           // handle exception... 
          } 
        case 11: 
         try {     
           image = ImageIO.read(new File("jd.gif")); 
          } catch (IOException ex) { 
           // handle exception... 
          } 
        case 12: 
         try {     
           image = ImageIO.read(new File("kd.gif")); 
          } catch (IOException ex) { 
           // handle exception... 
          } 
        case 13: 
         try {     
           image = ImageIO.read(new File("qd.gif")); 
          } catch (IOException ex) { 
           // handle exception... 
          } 
       } 
      break; 
      case SPADE: 
       switch (num) 
       { 
       case 1: 
        try {     
          image = ImageIO.read(new File("as.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 2: 
        try {     
          image = ImageIO.read(new File("2s.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 3: 
        try {     
          image = ImageIO.read(new File("3s.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 4: 
        try {     
          image = ImageIO.read(new File("4s.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 5: 
        try {     
          image = ImageIO.read(new File("5s.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 6: 
        try {     
          image = ImageIO.read(new File("6s.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 7: 
        try {     
          image = ImageIO.read(new File("7s.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 8: 
        try {     
          image = ImageIO.read(new File("8s.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 9: 
        try {     
          image = ImageIO.read(new File("9s.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 10: 
        try {     
          image = ImageIO.read(new File("ts.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 11: 
        try {     
          image = ImageIO.read(new File("jc.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 12: 
        try {     
          image = ImageIO.read(new File("ks.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 13: 
        try {     
          image = ImageIO.read(new File("qs.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 

       } 
      break; 
      case CLUB: 
       switch (num) 
       { 
       case 1: 
        try {     
          image = ImageIO.read(new File("ac.gif")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 2: 
        try {     
          image = ImageIO.read(new File("2c.gif")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 3: 
        try {     
          image = ImageIO.read(new File("3c.gif")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 4: 
        try {     
          image = ImageIO.read(new File("4c.gif")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 5: 
        try {     
          image = ImageIO.read(new File("5c.gif")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 6: 
        try {     
          image = ImageIO.read(new File("6c.gif")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 7: 
        try {     
          image = ImageIO.read(new File("7c.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 8: 
        try {     
          image = ImageIO.read(new File("8c.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 9: 
        try {     
          image = ImageIO.read(new File("9c.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 10: 
        try {     
          image = ImageIO.read(new File("tc.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 11: 
        try {     
          image = ImageIO.read(new File("jc.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 12: 
        try {     
          image = ImageIO.read(new File("kc.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 13: 
        try {     
          image = ImageIO.read(new File("qc.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       } 
      break; 
      case HEART: 
       switch (num) 
       { 
       case 1: 
        try {     
          image = ImageIO.read(new File("ah.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 2: 
        try {     
          image = ImageIO.read(new File("2h.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 3: 
        try {     
          image = ImageIO.read(new File("3h.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 4: 
        try {     
          image = ImageIO.read(new File("4h.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 5: 
        try {     
          image = ImageIO.read(new File("5h.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 6: 
        try {     
          image = ImageIO.read(new File("6h.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 7: 
        try {     
          image = ImageIO.read(new File("7h.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 8: 
        try {     
          image = ImageIO.read(new File("8h.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 9: 
        try {     
          image = ImageIO.read(new File("9h.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 10: 
        try {     
          image = ImageIO.read(new File("th.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 11: 
        try {     
          image = ImageIO.read(new File("jh.GIF")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 12: 
        try {     
          image = ImageIO.read(new File("kh.gif")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       case 13: 
        try {     
          image = ImageIO.read(new File("qh.gif")); 
         } catch (IOException ex) { 
          // handle exception... 
         } 
       } 
     } 
     return image; 
    } 

    public int carNum() 
    { 
     return num; 
    } 
    public void paintComponent(Graphics g) { 
      g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters 


    } 
    public String toString() 
     { 
      return "" +image; 

     } 
+0

這是功課還是什麼? – 2011-04-24 15:58:01

+0

我們要寫一個黑色的傑克程序,但gui部分是我選擇的,我們並不真正使用它,但我決定讓我們得到額外的信貸..我添加了評論,所以我回來完成它的大部分完成我只需要顯示我試圖卡.. \t \t如果(event.getSource()==命中) \t \t \t { \t \t \t \t \t \t playerC [I] = cl.getCard(); \t \t \t \t \t \t如果(playerC [I]!= NULL) \t \t \t { \t \t \t \t圖標圖像=(圖標)playerC [I]; \t \t \t \t JLabel label = new JLabel(「」,image,JLabel.CENTER); \t \t \t \t contentPane.add(label); \t \t \t \t label.setVisible(true); \t \t \t} \t \t \t我++; \t \t} – namdizy 2011-04-24 16:01:57

+1

我認爲你的'Cards'類必須擴展'JComponent',否則'paintComponent()'不會在需要的時候被調用。 – 2011-04-24 16:02:36

回答

0

基本GUI:

創建另一個類實際上是同一個main方法的主類。在main方法寫:

JFrame frame = new JFrame("Cards!"); // new window + title 
frame.setLocationRelativeTo(null); //posions the window in the center of the screen 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
JPanel primary = new JPanel(); //the main panel 
frame.setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS)) 
ImageIcon img = new ImageIcon(); //ImageIcon to hod the image 
JLabel label = new JLabel(img); //label to hold the icon 
JButton btn = new JButton("new card"); //button with text 
btn.addActionListener(new ActionListener() //adding the event 
{ 
    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     //event logic 
    } 
}); 
primary.add(label); 
primary.add(btn); 
frame.getContentPane().add(primary); //add the panel to the window 
frame.pack(); //resizes everything to fit 
frame.setVisible(true); 

這是GUI部分應該如何工作的基本想法。 現在事件的邏輯應該是沿着線的東西:

img.setImage(...); //your part of getting the right card image 
frame.pack(); //so window size adjusts to the new size 

你可以閱讀更多關於擺動(Java圖形用戶界面): http://download.oracle.com/javase/tutorial/uiswing/index.html

我希望我理解正確你的問題。

+0

嘿謝謝..我實際上有貴班,但沒有使用正確的代碼,我添加了img.setImage(..);和ImageIcon img = new ImageIcon(); JLabel label = new JLabel(img);現在我可以看到圖像,非常感謝!!!!!!!!!!!! – namdizy 2011-04-24 18:37:29