2017-12-16 16 views
1

に多くの画像を追加することでこんにちは、私は、任意の数のショーのための画像を表示したいサイコロ1-6文はJavaのスイング

の乱数を取るために必要とするプログラムに取り組んでいます。ここで

私は、これは文と私は私が転がり始めるときここdiceNumber

JPanel panel6 = new JPanel(); 
    diceNumber = new JLabel("1"); 

    if (diceNumber.getText().equals("1")) { 
     panel6.add(diceNum1); 
    } 

    else if (diceNumber.getText().equals("2")) { 
     panel6.add(diceNum2); 
    } 

    else if (diceNumber.getText().equals("3")) { 
     panel6.add(diceNum3); 
    } 

    else if (diceNumber.getText().equals("4")) { 
     panel6.add(diceNum4); 
    } 

    else if (diceNumber.getText().equals("5")) { 
     panel6.add(diceNum5); 
    } 

    else if (diceNumber.getText().equals("6")) { 
     panel6.add(diceNum6); 
    } 

を初期化する必要がある場合は、1枚の絵がで に表示されるパネルであるjlable

JLabel diceNumber; 

Random r = new Random(); 
int roll; 


ImageIcon dicePic1 = new ImageIcon("dice1_.png"); 
ImageIcon dicePic2 = new ImageIcon("dice2.png"); 
ImageIcon dicePic3 = new ImageIcon("dice3.png"); 
ImageIcon dicePic4 = new ImageIcon("dice4.png"); 
ImageIcon dicePic5 = new ImageIcon("dice5.png"); 
ImageIcon dicePic6 = new ImageIcon("dice6.png"); 

JLabel diceNum1 = new JLabel("", dicePic1, JLabel.CENTER); 
JLabel diceNum2 = new JLabel("", dicePic2, JLabel.CENTER); 
JLabel diceNum3 = new JLabel("", dicePic3, JLabel.CENTER); 
JLabel diceNum4 = new JLabel("", dicePic4, JLabel.CENTER); 
JLabel diceNum5 = new JLabel("", dicePic5, JLabel.CENTER); 
JLabel diceNum6 = new JLabel("", dicePic6, JLabel.CENTER); 

と一緒に写真を初期化

public void actionPerformed(ActionEvent e) { 


    if (e.getSource() == dice) { 

     roll = r.nextInt(6) + 1; 

     if (roll == 1) { 
      diceNumber.setText("1"); 
     } 

     else if (roll == 2) { 
      diceNumber.setText("2"); 
     } 

     else if (roll == 3) { 
      diceNumber.setText("3"); 
     } 

     else if (roll == 4) { 
      diceNumber.setText("4"); 
     } 

     else if (roll == 5) { 
      diceNumber.setText("5"); 
     } 

     else if (roll == 6) { 
      diceNumber.setText("6"); 
     } 

問題はそれですceNumberは常に値 "1"をとり、何も変更しないでください

答えて

1

actionPerformed()メソッドでは、diceNumberラベルのテキストを変更します。これがすべての作業です。画像ラベルで表示される画像は変更しないでください。

実際には配列について学び、6つのイメージを配列に格納する必要があります。あなたのロジックは次のようになります。

init() { 
    // add a label displaying the number (1 by default) 
    // add a label displaying the icon (1 by default) 
} 

actionPerformed() { 
    // choose a random number 
    // change the text of the number label 
    // change the icon of the image label 
} 

、それを行うには、if文の大きなチェーンのNIの必要性:それはちょうど

imageLabel.setIcon(images[randomRoll - 1]); 
3
diceNumber = new JLabel("1"); 

する必要がありますが、すぐにdiceNumberテキスト上の論理チェックを先行します。この段階では、テキスト "1"でJLabelを開始しました。したがって、この段階では常に "1"になります。テキストを後で変更することはありません。その段階では、サイコロ画像を設定する必要はありません。あなたが取ったアプローチは、Rube Goldbergマシンに似ています。一連のイベントをトリガして比較的単純なタスクを実行します。 シンプル解決策は次のようになります。

List<JLabel> disc = new ArrayList<>(); 
    for (int i = 1; i <= 6; i++) { 
     disc.add(new ImageIcon(String.format("dice%d_.png", i)); 
    } 

次に、あなたが呼び出すイメージ変更する必要がある場合:

int roll = r.nextInt(6) + 1; 
panel.add(disc[roll]); 

かかわらず、それがあるようなコードにすることができる多くの改良があるが、過剰なif文を次のように変更することをお勧めします。

diceNumber.setText(roll.toString()); or 
diceNumber.setText(roll + ""); 
関連する問題