我试图将ImageIcon放在JLabel上,但它没有出现在JFrame中。我找不到错误的代码,我的控制台也没有显示任何错误。
下面是代码:
public class Gui extends JFrame implements ActionListener {
private JLabel card;
private JButton bBet;
private ImageIcon c2 = new ImageIcon("./images/c2.jpg");
private ImageIcon d2 = new ImageIcon("./images/d2.jpg");
private ImageIcon h2 = new ImageIcon("./images/h2.jpg");
private ImageIcon s2 = new ImageIcon("./images/s2.jpg");
private JPanel panel;
private int cardx1 = 250, cardy1 = 400;
public Gui() {
this.setTitle("Simple Blackjack");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1200, 800);
this.setLayout(null);
panel = new JPanel();
panel.setBounds(10, 10, 1200, 800);
panel.setLayout(null);
bBet = new JButton("Bet");
bBet.setBounds(10, 70, 200, 35);
bBet.addActionListener(this);
panel.add(bBet);
this.add(panel);
this.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == bBet) {
random = getRandom();
addLabel(cardx1, cardy1, random);
}
}
public void addLabel(int x, int y, int random) {
Random r = new Random(4);
int which = r.nextInt();
card = new JLabel();
card.setBounds(x, y, 166, 230);
switch (random) {
case 0:
if (which == 0){
card.setIcon(c2);
} else if (which == 1) {
card.setIcon(d2);
} else if (which == 2) {
card.setIcon(h2);
} else if (which == 3) {
card.setIcon(s2);
}
break;
}
panel.add(card);
panel.repaint();
}发布于 2016-06-08 13:58:22
我认为如果图像的位置正确,那么下一个问题就是随机数的生成。而不是
Random r = new Random(4);
int which = r.nextInt();使用下面的代码生成随机数
ThreadLocalRandom.current().nextInt(0, 4);https://stackoverflow.com/questions/37703562
复制相似问题