//download:http://www.codefans.net
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
public class RPC extends JFrame implements ActionListener
{
public JLabel lblWL,lblPlayer,lblPC;
public static void main(String[] args)
{
RPC theWindow = new RPC();
theWindow.show();
}
public RPC()
{
Button butRock = new Button("Rock");
butRock.addActionListener(this);
Button butPaper = new Button("Paper");
butPaper.addActionListener(this);
Button butScissors = new Button("Scissors");
butScissors.addActionListener(this);
lblPlayer = new JLabel();
lblPC = new JLabel();
lblWL= new JLabel();
JLabel lblP= new JLabel("Your Choice:");
JLabel lblC= new JLabel("Computers Choice:");
setTitle("Rock, Paper Scissors");
JPanel ButtPan=new JPanel();
ButtPan.setLayout(new GridLayout(1,3));
ButtPan.add(butRock);
ButtPan.add(butPaper);
ButtPan.add(butScissors);
JPanel LabelsPan=new JPanel();
LabelsPan.setLayout(new GridLayout(4,1));
LabelsPan.add(lblP);
LabelsPan.add(lblPlayer);
LabelsPan.add(lblC);
LabelsPan.add(lblPC);
JPanel WLPan=new JPanel();
WLPan.setLayout(new BorderLayout());
WLPan.add(lblWL,"Center");
JPanel TwoPanesN1=new JPanel();
TwoPanesN1.setLayout(new BorderLayout());
TwoPanesN1.add(LabelsPan,"West");
TwoPanesN1.add(WLPan,"East");
getContentPane().setLayout(new GridLayout(2,1));
getContentPane().add(ButtPan);
getContentPane().add(TwoPanesN1);
Font fontDisplay = new Font("Verdana", Font.BOLD, 22);
lblWL.setFont(fontDisplay);
setSize(300,200);
setVisible(true);
setResizable(false);
addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent ev){System.exit(0);}});
}
public void Play(String PlayerChoice)
{
String PCChoice=GetPCChoice();
lblPC.setText(PCChoice);
if(PlayerChoice.equals(PCChoice))
lblWL.setText("Tie!");
else if(PlayerChoice.equals("Rock"))
if(PCChoice.equals("Paper"))
lblWL.setText("You Lose!");
else
lblWL.setText("You Win!");
else if(PlayerChoice.equals("Paper"))
if(PCChoice.equals("Scissors"))
lblWL.setText("You Lose!");
else
lblWL.setText("You Win!");
else if(PlayerChoice.equals("Scissors"))
if(PCChoice.equals("Rock"))
lblWL.setText("You Lose!");
else
lblWL.setText("You Win!");
}
public String GetPCChoice()
{
String tmp2="";
int tmp=(int)(Math.random( )*3)+1;
if(tmp==1)
tmp2= "Rock";
else if(tmp==2)
tmp2= "Paper";
else if(tmp==3)
tmp2= "Scissors";
return tmp2;
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Exit"))
System.exit(0);
else
{
lblPlayer.setText(e.getActionCommand());
Play(e.getActionCommand());
}
}
}
评论0
最新资源