我想寫一個非常簡單的21點遊戲。showConfirmDialog後關閉背景JFrame
這是應該顯示當前抽卡類:
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
public class ShowRandomCard {
JFrame f = new JFrame();
JPanel p = new JPanel();
public void ShowUsARandomCard() {
f.setLayout(new BorderLayout());
p.add(new JLabel("A Panel"));
f.add(p, BorderLayout.NORTH);
// Picture
BufferedImage myPicture = null;
try {
myPicture = ImageIO.read(new File("somepicture"));
} catch (IOException e) {
e.printStackTrace();
}
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
f.add(picLabel,BorderLayout.NORTH);
// elements
f.pack();
f.setVisible(true);
Blackjack jt = new Blackjack();
jt.dialog();
}
public void hideCards() {
f.setVisible(false);
f.remove(p);
f.dispose();
f.repaint();
}
}
這是實際的遊戲類:
import static javax.swing.JOptionPane.*;
public class Blackjack {
ShowRandomCard it = new ShowRandomCard();
public void dialog() {
int answer2 = showConfirmDialog(null, "some message", "some title",
YES_NO_OPTION);
if (answer2 == YES_OPTION) {
garbageCollection();
it.ShowUsARandomCard();
if (answer2 == NO_OPTION || answer2 == CANCEL_OPTION) {
garbageCollection();
// System.exit(0);
}
}
}
public void garbageCollection() {
it.hideCards();
}
}
但持有的卡不消失的JPanel 。 任何幫助,將不勝感激。
爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 – 2013-04-26 17:28:17
以及我已盡全力顯示只有必要的部分 – herecy 2013-04-26 17:30:43
記得調用JFrame.redraw()後取出卡 – RuntimeError 2013-04-26 17:32:17