2013-04-27 76 views
0

我正在嘗試編寫一個代碼,以便每次按下它時都會打開卡。 JCardDemo applet中的createGUI存在問題嗎?那麼如何糾正呢? 這是我的code.thx卡和JButtons

JCardbutton類 import java.applet。 ; import java.awt。; import java.awt.event。 ; import javax.swing。; import javax.swing.event。*; import java.awt.geom.AffineTransform;

public class JCardButton extends JButton { 
private ImageIcon myFace; 
private ImageIcon myBack; 
private boolean myFaceUp; 
private Rank myRank; 
private Suit mySuit; 

public Rank getRank() { 
    return myRank; 
} 

public Suit getSuit() { 
    return mySuit; 
} 

public JCardButton(Suit s, Rank r, ImageIcon face, ImageIcon back) { 
    super(); 

    mySuit = s; 
    myRank = r; 
    setFace(face); 
    setBack(back); 

    this.setSize(face.getIconWidth(), face.getIconHeight()); 
    showBack(); 
} 

public ImageIcon getFaceImage() { 
    return myFace; 
} 

public boolean isFaceUp() { 
    return true;//isFaceUp 
} 

public boolean isFaceDown() { 
    return false;// 
} 

public ImageIcon getBackImage() { 
    return myBack; 
} 

public void setFace(ImageIcon image) { 
    image = myFace; 
} 

public void setBack(ImageIcon image) { 
    image = myBack; 
} 

public void showBack() { 
    setIcon(myBack); 
    myFaceUp = false; 
} 

public void showFace() { 
    setIcon(myFace); 
    myFaceUp = true; 
} 

public void turnOver() { 
    if(isFaceUp()) { 
     showBack(); 
    } 
    else { 
     showFace(); 
    } 
} 

public int compareTo (JCardButton other) { 
    if(this.getSuit().compareTo(other.getSuit()) ==0) { 
     return this.getRank().compareTo(other.getRank()); 
    } 
    else { 
     return this.getSuit().compareTo(other.getSuit()); 
    } 
} 

public String toString() { 
    return myRank.toString() + mySuit.toString(); 
} 

}

JCardDemo 進口java.awt.event中。 ; import java.awt。; import javax.swing。*;

public class JCardDemo extends JApplet implements ActionListener 
{ 
private static final long serialVersionUID = 2005L; 
private Container window; 
private JCardButton jcbCard; 

public void init() { 
    window = new DoubleBufferedPanel(); 
    setContentPane(window); 

    createAppearance(); 
    createGUI(); 

} 

public void createAppearance(){ 
    window.setLayout(null); 
} 

public void createGUI() 
{ 
    ImageIcon front = new ImageIcon(getImage(getCodeBase(), "images/2c.gif")); 
    ImageIcon back = new ImageIcon(getImage(getCodeBase(), "images/b.gif")); 

    jcbCard = new JCardButton(Suit.clubs, Rank.two, front, back); 
    jcbCard.setLocation(50, 50); 
    jcbCard.addActionListener(this); 
    window.add(jcbCard); 
} 

public void actionPerformed(ActionEvent e) 
{ 
    if(e.getSource() instanceof JCardButton) 
    { 
     JCardButton c = (JCardButton) e.getSource(); 
     c.turnOver(); 
    } 
    repaint(); 
} 

class DoubleBufferedPanel extends JPanel { 
    private static final long serialVersionUID = 44L; 

    public void paint(Graphics g){ 
     super.paint(g); 
    } 
} 

}

排名枚舉

public enum Rank 
{ 
ace, 
two, 
three, 
four, 
five, 
six, 
seven, 
eight, 
nine, 
ten, 
jack, 
queen, 
king; 

public String toString() 
{ 
    switch(this) 
    { 
     case ace: return "A"; 
     case two: return "2"; 
     case three: return "3"; 
     case four: return "4"; 
     case five: return "5"; 
     case six: return "6"; 
     case seven: return "7"; 
     case eight: return "8"; 
     case nine: return "9"; 
     case ten: return "T"; 
     case jack: return "J"; 
     case queen: return "Q"; 
     case king: return "K"; 
     default: return "??"; 
    } 
} 

}

套裝枚舉

public enum Suit 
{ 
clubs, hearts, spades, diamonds; 

public String toString() 
{ 
    return this.name().substring(0, 1).toUpperCase(); 
} 
} 

回答

1

有2個原因,圖像切換功能沒有在這裏工作。

首先是圖像的分配是JCardButton

public void setFace(ImageIcon image) { 
    image = myFace; // local variable image assigned to myFace still null 
} 

以錯誤的方式應該是

public void setFace(ImageIcon image) { 
    myFace = image; 
} 

這applys到setBack也。

其次,isFaceUp總是返回true

public boolean isFaceUp() { 
    return true; //?? 
} 

解決我的問題再次換上

public boolean isFaceUp() { 
    return myFaceUp; 
} 
+0

THX :) – 2013-04-29 02:56:31