0
我正在用JAVA製作UNO遊戲。我遇到重複標籤的問題。我有一個JFrame。其中有7個JPanel。每個JPanel都有一個JLabel。 JLabels存儲在另一個類的arrayList中。每個JLabel都有一個圖像。 arrayList裏面有重複的JLabel。我正在將JLabel隨機添加到JPanel中。但是,當調用重複的JLabel時,它會被跳過。請幫助。在JFrame中重複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);
}
}
的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);
}
}
當複製不發生:
當發生重複:
所有'Components'只能有1個父項。您需要製作重複的JLabels – ControlAltDel
注意圖標可以共享,但是您必須爲每個面板創建一個單獨的標籤。 – camickr