我正在尝试制作一个扑克程序,并且我正在尝试在程序执行时显示所有五张牌;但是,我试图按顺序执行此操作,并在显示卡片之前显示一个
Shuffling...
JLabel。但由于某种原因,什么也没有显示。
有关更多信息,我的
Cards
驱动器中有一个文件夹(称为 H:
)。
洗牌:
public void shuffleDeck() {
ImageIcon card1 = new ImageIcon("H:\\Cards\\Spades\\QOS.png") ;
ImageIcon card2 = new ImageIcon("H:\\Cards\\Spades\\7OS.png") ;
ImageIcon card3 = new ImageIcon("H:\\Cards\\Clubs\\3OC.png") ;
ImageIcon card4 = new ImageIcon("H:\\Cards\\Clubs\\2.png") ;
ImageIcon card5 = new ImageIcon("H:\\Cards\\Clubs\\K.png") ;
JLabel label1 = new JLabel(card1) ;
JLabel label2 = new JLabel(card2) ;
JLabel label3 = new JLabel(card3) ;
JLabel label4 = new JLabel(card4) ;
JLabel label5 = new JLabel(card5) ;
this.deck.add(label1) ;
this.deck.add(label2) ;
this.deck.add(label3) ;
this.deck.add(label4) ;
this.deck.add(label5) ;
JLabel shuffling = new JLabel("Shuffling...") ;
for (int i = 0 ; i < 5 ; i++) {
int randint = (int) (Math.random() * this.deck.size()) ;
this.panel.add(this.deck.get(randint)) ;
this.deck.remove(randint) ;
}
this.frame.add(shuffling) ;
this.playSound("C:\\Users\\8018156\\Downloads\\shuffling-cards-1.wav") ;
this.sleep(1) ;
shuffling = new JLabel("") ;
this.frame.add(this.panel) ;
gc() ;
}
类别属性:
public long $$$ ;
public long bet ;
public long minBet = 1 ;
public ANSIColors ANSI = new ANSIColors() ;
public ArrayList<JLabel> deck = new ArrayList<JLabel>() ;
public Scanner sc = new Scanner(in) ;
public BufferedImage image ;
private final String[] cardTypes = {"OS", "OH", "OC", "OD"} ;
private final String[] cardValues = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"} ;
private final int BUFFER_SIZE = 128000;
private File soundFile ;
private AudioInputStream audioStream ;
private AudioFormat audioFormat ;
private SourceDataLine sourceLine ;
private JPanel panel = new JPanel(new GridLayout(0, 5)) ;
private JFrame frame = new JFrame() ;
private ArrayList<ImageIcon> shownCards = new ArrayList<ImageIcon>() ;
private ImageIcon card1 = new ImageIcon("C:\\Users\\8018156\\Downloads\\Q.png") ;
private ImageIcon card2 = new ImageIcon("C:\\Users\\8018156\\Downloads\\7.png") ;
private ImageIcon card3 = new ImageIcon("C:\\Users\\8018156\\Downloads\\A.png") ;
private ImageIcon card4 = new ImageIcon("C:\\Users\\8018156\\Downloads\\2.png") ;
private ImageIcon card5 = new ImageIcon("C:\\Users\\8018156\\Downloads\\K.png") ;
我尝试了很多很多方法来尝试改变这一点:仅使用 ImageIcons,在没有方法的情况下使用它,但是,无论如何,它只显示第二个。
如何使所有图像显示?
这是因为如果 JLabel 作为图像加载,Java 一次只能显示一个 JLabel。
建议您使用
GridLayout
,如图:
import javax.swing.*;
import java.awt.*;
public class ImageDisplay extends JFrame {
public ImageDisplay() {
super("Image Display");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create the images
ImageIcon image1 = new ImageIcon("image1.jpg");
ImageIcon image2 = new ImageIcon("image2.jpg");
ImageIcon image3 = new ImageIcon("image3.jpg");
ImageIcon image4 = new ImageIcon("image4.jpg");
ImageIcon image5 = new ImageIcon("image5.jpg");
// Create the labels
JLabel label1 = new JLabel(image1);
JLabel label2 = new JLabel(image2);
JLabel label3 = new JLabel(image3);
JLabel label4 = new JLabel(image4);
JLabel label5 = new JLabel(image5);
// Add the labels to the panel
JPanel panel = new JPanel(new GridLayout(1, 5));
panel.add(label1);
panel.add(label2);
panel.add(label3);
panel.add(label4);
panel.add(label5);
// Add the panel to the frame and display it
getContentPane().add(panel);
pack();
setVisible(true);
setSize(800, 600);
}
public static void main(String[] args) {
new ImageDisplay();
}
}