我在JAVA制作UNO游戏。我遇到重复标签的问题。我有一个JFrame。其中有7个JPanel。每个JPanel里面都有一个JLabel。 JLabel存储在另一个类的arrayList中。每个JLabel里面都有一个图像。 arrayList里面有重复的JLabel。我随机将JLabels放入JPanels。但是当调用重复的JLabel时,会跳过它。请帮助。
The Deck:
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);
}
}
GUI:
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);
}
}
如果没有重复:
发生重复时:
答案 0 :(得分:1)
多个父母不能共享Swing组件。
但是可以共享Icon
。
因此,您需要更改get()
方法以获取随机Icon
,而不是随机标签。然后,一旦您检索到Icon
,就会创建一个新的JLabel
并将Icon
添加到标签中。然后将标签添加到表单中。