gpt4 book ai didi

java - JFrame 中的重复 JLabel

转载 作者:行者123 更新时间:2023-12-01 09:28:19 27 4
gpt4 key购买 nike

我正在用JAVA制作UNO游戏。我遇到重复标签的问题。我有一个 JFrame。其中有 7 个 JPanel。每个 JPanel 内部都有一个 JLabel。 JLabels 存储在另一个类的 arrayList 中。每个 JLabel 内部都有一个图像。 arrayList 内部有重复的 JLabel。我将 JLabels 随机放入 JPanels 中。但是当调用重复的 JLabel 时,它会被跳过。请帮忙。

套牌:

public class Deck {
private ArrayList<JLabel> cards;

Zero zero = new Zero();
One one = new One();
Two two = new Two();
Three three = new Three();
Four four = new Four();
Five five = new Five();
Six six = new Six();
Seven seven = new Seven();
Eight eight = new Eight();
Nine nine = new Nine();
Reverse reverse = new Reverse();
Skip skip = new Skip();
DrawTwo drawTwo = new DrawTwo();
Wild wild = new Wild();
WildDrawFour wildDrawFour = new WildDrawFour();

public Deck() {
cards = new ArrayList<JLabel>();
cards.add(zero.getLabelR());
cards.add(zero.getLabelY());
cards.add(zero.getLabelG());
cards.add(zero.getLabelB());
cards.add(one.getLabelR());
cards.add(one.getLabelR());
cards.add(one.getLabelB());
cards.add(one.getLabelG());
cards.add(one.getLabelY());
cards.add(one.getLabelB());
cards.add(one.getLabelG());
cards.add(one.getLabelY());
cards.add(two.getLabelB());
cards.add(two.getLabelG());
cards.add(two.getLabelY());
cards.add(two.getLabelR());
cards.add(two.getLabelB());
cards.add(two.getLabelG());
cards.add(two.getLabelY());
cards.add(two.getLabelR());
cards.add(three.getLabelB());
cards.add(three.getLabelG());
cards.add(three.getLabelY());
cards.add(three.getLabelR());
cards.add(three.getLabelB());
cards.add(three.getLabelG());
cards.add(three.getLabelY());
cards.add(three.getLabelR());
cards.add(four.getLabelB());
cards.add(four.getLabelG());
cards.add(four.getLabelY());
cards.add(four.getLabelR());
cards.add(four.getLabelB());
cards.add(four.getLabelG());
cards.add(four.getLabelY());
cards.add(four.getLabelR());
cards.add(five.getLabelB());
cards.add(five.getLabelG());
cards.add(five.getLabelY());
cards.add(five.getLabelR());
cards.add(five.getLabelB());
cards.add(five.getLabelG());
cards.add(five.getLabelY());
cards.add(five.getLabelR());
cards.add(six.getLabelB());
cards.add(six.getLabelG());
cards.add(six.getLabelY());
cards.add(six.getLabelR());
cards.add(six.getLabelB());
cards.add(six.getLabelG());
cards.add(six.getLabelY());
cards.add(six.getLabelR());
cards.add(seven.getLabelB());
cards.add(seven.getLabelG());
cards.add(seven.getLabelY());
cards.add(seven.getLabelR());
cards.add(seven.getLabelB());
cards.add(seven.getLabelG());
cards.add(seven.getLabelY());
cards.add(seven.getLabelR());
cards.add(eight.getLabelB());
cards.add(eight.getLabelG());
cards.add(eight.getLabelY());
cards.add(eight.getLabelR());
cards.add(eight.getLabelB());
cards.add(eight.getLabelG());
cards.add(eight.getLabelY());
cards.add(eight.getLabelR());
cards.add(nine.getLabelB());
cards.add(nine.getLabelG());
cards.add(nine.getLabelY());
cards.add(nine.getLabelR());
cards.add(nine.getLabelB());
cards.add(nine.getLabelG());
cards.add(nine.getLabelY());
cards.add(nine.getLabelR());
cards.add(reverse.getLabelB());
cards.add(reverse.getLabelG());
cards.add(reverse.getLabelY());
cards.add(reverse.getLabelR());
cards.add(reverse.getLabelB());
cards.add(reverse.getLabelG());
cards.add(reverse.getLabelY());
cards.add(reverse.getLabelR());
cards.add(skip.getLabelB());
cards.add(skip.getLabelG());
cards.add(skip.getLabelY());
cards.add(skip.getLabelR());
cards.add(skip.getLabelB());
cards.add(skip.getLabelG());
cards.add(skip.getLabelY());
cards.add(skip.getLabelR());
cards.add(drawTwo.getLabelB());
cards.add(drawTwo.getLabelG());
cards.add(drawTwo.getLabelY());
cards.add(drawTwo.getLabelR());
cards.add(drawTwo.getLabelB());
cards.add(drawTwo.getLabelG());
cards.add(drawTwo.getLabelY());
cards.add(drawTwo.getLabelR());
cards.add(wild.getLabel());
cards.add(wild.getLabel());
cards.add(wild.getLabel());
cards.add(wild.getLabel());
cards.add(wildDrawFour.getLabel());
cards.add(wildDrawFour.getLabel());
cards.add(wildDrawFour.getLabel());
cards.add(wildDrawFour.getLabel());

shuffle();

}

public ArrayList<JLabel> getCards() {
return cards;
}

public void shuffle() {
Collections.shuffle(cards);
}

}

图形用户界面:

public class GUI extends JFrame {
JPanel card1;
JPanel card2;
JPanel card3;
JPanel card4;
JPanel card5;
JPanel card6;``
JPanel card7;

Back back = new Back();
Zero zero = new Zero();
One one = new One();
Two two = new Two();
Three three = new Three();
Four four = new Four();
Five five = new Five();
Six six = new Six();
Seven seven = new Seven();
Eight eight = new Eight();
Nine nine = new Nine();
Reverse reverse = new Reverse();
Skip skip = new Skip();
DrawTwo drawTwo = new DrawTwo();
Wild wild = new Wild();
WildDrawFour wildDrawFour = new WildDrawFour();

Deck ar = new Deck();
Random r = new Random();

public GUI() {
super("UNO");
setLayout(null);
setSize(1150, 690);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(new Color(35, 176, 40));

int a = r.nextInt(100) + r.nextInt(9) + 1;
card1 = new JPanel();
card1.setLayout(null);
card1.setBounds(65, 500, 85, 120);
card1.setBackground(new Color(35, 176, 40));
card1.add(ar.getCards().get(a)).setBounds(0, 0, 85, 120);
System.out.println(ar.getCards().get(a));
System.out.println(a);

int b = 0;
while (true) {
b = r.nextInt(100) + r.nextInt(9) + 1;
if (b != a) {
card2 = new JPanel();
card2.setLayout(null);
card2.setBounds(215, 500, 85, 120);
card2.setBackground(new Color(35, 176, 40));
card2.add(ar.getCards().get(b)).setBounds(0, 0, 85, 120);
System.out.println(ar.getCards().get(b));
System.out.println(b);
break;
} else
continue;
}

int c = 0;
while (true) {
c = r.nextInt(100) + r.nextInt(9) + 1;
if (c != a && c != b) {
card3 = new JPanel();
card3.setLayout(null);
card3.setBounds(365, 500, 85, 120);
card3.setBackground(new Color(35, 176, 40));
card3.add(ar.getCards().get(c)).setBounds(0, 0, 85, 120);
System.out.println(ar.getCards().get(c));
System.out.println(c);
break;
} else
continue;
}

int d = 0;
while (true) {
d = r.nextInt(100) + r.nextInt(9) + 1;
if (d != a && d != b && d != c) {
card4 = new JPanel();
card4.setLayout(null);
card4.setBounds(515, 500, 85, 120);
card4.setBackground(new Color(35, 176, 40));
card4.add(ar.getCards().get(d)).setBounds(0, 0, 85, 120);
;
System.out.println(ar.getCards().get(d));
System.out.println(d);
break;
} else
continue;
}

int e = 0;
while (true) {
e = r.nextInt(100) + r.nextInt(9) + 1;
if (e != a && e != b && e != c && e != b) {
card5 = new JPanel();
card5.setLayout(null);
card5.setBounds(665, 500, 85, 120);
card5.setBackground(new Color(35, 176, 40));
card5.add(ar.getCards().get(e)).setBounds(0, 0, 85, 120);
System.out.println(ar.getCards().get(e));
System.out.println(e);
break;
} else
continue;
}

int f = 0;
while (true) {
f = r.nextInt(100) + r.nextInt(9) + 1;
if (f != a && f != b && f != c && f != d && f != e) {
card6 = new JPanel();
card6.setLayout(null);
card6.setBounds(815, 500, 85, 120);
card6.setBackground(new Color(35, 176, 40));
card6.add(ar.getCards().get(f)).setBounds(0, 0, 85, 120);
System.out.println(ar.getCards().get(f));
System.out.println(f);
break;
} else
continue;
}

card7 = new JPanel();
card7.setLayout(null);
card7.setBounds(965, 500, 85, 120);
card7.setBackground(new Color(35, 176, 40));
card7.add(back.getLabel()).setBounds(0, 0, 85, 120);

add(card1);
add(card2);
add(card3);
add(card4);
add(card5);
add(card6);
add(card7);

setLocationRelativeTo(null);
setVisible(true);

}

@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.white);
g2.setStroke(new BasicStroke(6));
g2.drawRoundRect(965, 523, 100, 135, 15, 15);
g2.setStroke(new BasicStroke(5));
g2.drawLine(0, 480, 1150, 480);
}

}

当不发生重复时:

enter image description here

当发生重复时:

enter image description here

最佳答案

Swing 组件不能由多个父组件共享。

但是图标可以共享。

因此您的 get() 方法需要更改为获取随机 Icon,而不是随机标签。然后,一旦检索到 Icon,您就创建一个新的 JLabel 并将 Icon 添加到标签中。然后将标签添加到表单中。

关于java - JFrame 中的重复 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39660705/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com