我正在尝试编写一个非常简单的二十一点游戏。
这是应该显示当前绘制的卡片的类:
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 并没有消失。任何帮助,将不胜感激。