你好我正在写一个黑色插孔程序,我希望能够显示我制作的卡片..我导入每张卡片的图像,并使用案例分配每个特定的套装和卡号然后我做了一个套牌这是一个堆栈,并将每张卡添加到其中。我做了一个方法,洗牌卡和弹出第一个。我想在每次按下按钮时显示每张卡,我该怎么做?
我的代码......
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 :(得分: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
您可以在以下位置阅读有关swing(java gui)的更多信息: http://download.oracle.com/javase/tutorial/uiswing/index.html
我希望我能正确理解你的问题。