import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.util.*;public class CrapsGame extends JFrame
{
private JPanel pointDiceJPanel;
private TitledBorder pointDiceTitledBorder;

private JLabel pointDie1JLabel;
private JLabel pointDie2JLabel;
private JLabel die1JLabel;
private JLabel die2JLabel;

private JButton playJButton;
private JButton rollJButton;

private JLabel resultJLabel;
private JTextField resultJTextField;

private final int LUCKY_SEVEN = 7;
private final int YO_LEVEN = 11;

private final int SNAKE_EYES = 2;
private final int TREY = 3;
private final int BOX_CARS = 12;
private final int CRARS = 7;

private final String FILE_PREFIX = "Image/die";
private final String FILE_SUFFIX = ".gif";

private int myPoint = 0;
private Random randomObject ;//= new Random();

public void CrapsGame()
{
createUserInterface();
}
private void createUserInterface()
{
Container contentPane = getContentPane();//思维定势new getContainer();错误的方法getContainer()
contentPane.setLayout(null);

pointDiceJPanel = new JPanel();
pointDiceJPanel.setBounds(16,16,200,200);
            
pointDiceTitledBorder = new TitledBorder("point number");
      //错的方法pointDiceTitledBorder.add(pointDiceJPanel);//pointDiceJPanel.add(pointDiceTitledBorder);
      pointDiceJPanel.setBorder(pointDiceTitledBorder);
      contentPane.add(pointDiceJPanel);       
      
      pointDie1JLabel = new JLabel();
      pointDie1JLabel.setBounds(26,26,85,85);
      pointDiceJPanel.add(pointDie1JLabel);       
      
      pointDie2JLabel = new JLabel();
      pointDie2JLabel.setBounds(121,26,85,85);
      pointDiceJPanel.add(pointDie2JLabel);
      
      die1JLabel = new JLabel();
      die1JLabel.setBounds(26,226,50,50);
      contentPane.add(die1JLabel);
      die2JLabel  = new JLabel();
      die2JLabel.setBounds(81,226,50,50);
      contentPane.add(die2JLabel);       
      
      playJButton = new JButton("Play");
      playJButton.setBounds(236,16,30,10);       
      contentPane.add(playJButton);
      playJButton.addActionListener(new ActionListener()
                                    {
                                     public void actionPerformed(ActionEvent evt)
                                     {
                                     playJButtonActionPerformed(evt);
                                     }
                                    });
      rollJButton = new JButton("Roll");
      rollJButton.setBounds(236,36,30,10);
      contentPane.add(rollJButton);
      rollJButton.addActionListener(new ActionListener()
       {
       public void actionPerformed(ActionEvent evt)
       {
       rollJButtonActionPerformed(evt);
       }       
       });
      resultJLabel = new JLabel();
      resultJLabel.setText("Result:");
      resultJLabel.setBounds(236,56,30,10);       
      contentPane.add(resultJLabel);
             
      resultJTextField = new JTextField();       
      resultJTextField.setBounds(236,66,30,30);
      resultJTextField.setEnabled(false);
        resultJLabel.setHorizontalAlignment(JLabel.RIGHT);
      contentPane.add(resultJTextField);
      
      setTitle("CrapsGAme");
      setSize(450,300);
      contentPane.setVisible(true);
  }
  public void playJButtonActionPerformed(ActionEvent evt)
  {
   // pointDie1JLabel.setIcon(null);
   // pointDie2JLabel.setIcon(null);
  
   pointDiceTitledBorder.setTitle("Point number");
   pointDiceJPanel.repaint();
  
   int sum_dice = rollDice();
switch(sum_dice)
{
case LUCKY_SEVEN:
case YO_LEVEN:
 rollJButton.setEnabled(false);
 resultJTextField.setText("You win");  
 break;
case SNAKE_EYES:
case TREY:
// case CRARS:
case BOX_CARS:
     resultJTextField.setText("You lose");
     playJButton.setEnabled(false);
     rollJButton.setEnabled(true);
     break;
default:
     myPoint = sum_dice;
 resultJTextField.setText("Please play again");
 
// displayDice(pointDie1JLabel,die1JLabel.getIcon());
// displayDice(pointDie2JLabel,die2JLabel.getIcon());
 pointDie1JLabel.setIcon(die1JLabel.getIcon());
 pointDie2JLabel.setIcon(die2JLabel.getIcon());
 pointDiceTitledBorder.setTitle("Point number is"+sum_dice);
 pointDiceJPanel.repaint();
 
 playJButton.setEnabled(false);
 rollJButton.setEnabled(true);          
 break;      
 
}   
  
  }
  private void rollJButtonActionPerformed(ActionEvent evt)
  {
   int sum_dice = rollDice();
  
   if(sum_dice == myPoint)
   {
   resultJTextField.setText("You win");
   rollJButton.setEnabled(false);
   playJButton.setEnabled(true);
   }
   else if(sum_dice == CRARS)
       {
            resultJTextField.setText("You lose");
   rollJButton.setEnabled(false);
   playJButton.setEnabled(true);
       }
  }
  private int rollDice()
  {
       Random randomObject = new Random();        
   int dice1 = 1+randomObject.nextInt(6);
   int dice2 = 1+randomObject.nextInt(6);
  
   displayDice(die1JLabel,dice1);
   displayDice(die2JLabel,dice2);
  
   return dice2+dice1;
    
  }
  private void displayDice(JLabel picDiceJLabel,int face)
  {
   ImageIcon image1 = new ImageIcon(FILE_PREFIX+face+FILE_SUFFIX);
   picDiceJLabel.setIcon(image1);   
  }
  public static void main(String[] args)
  {
   CrapsGame application = new CrapsGame();
   application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  } 

}