⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 player.java

📁 用java写的股票交易游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// jingjing Wang
//420 final project
import javax.swing.*;
import java.awt.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.awt.event.*;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Player extends JApplet{
	public String stockname;  // for the use of the button
	public int checkpoint=1;
	private int turn=1;
	public String name;
	public double balance=2000;
    private Bankinterface h;
    
    private StockExchangeinterface w;
    private int account=1;  //each one only has at most one account
    PlayerCallbackImpl g = null;
    public int stock1=20;      // Stock1
    public int stock2=20;      //Stock2
    public int stock3=20;      //Stock3
    public  int myturn=0;
    JFrame frame1 = new JFrame();                      // the interface at the beginning of the game
    JFrame frame2 = new JFrame();                      // the interface at the stockexchange
    JFrame frame3 = new JFrame();                      // for the bank interface
    public JTextField jtextfield5=new JTextField();    // At the begining to input the name
    public Label Input=new Label("Input your name");
    public JButton next=new JButton("Next");
    private JRadioButton s1=new JRadioButton("stock1");
    private JRadioButton s2=new JRadioButton("stock2");
    private JRadioButton s3=new JRadioButton("stock3");
    private ButtonGroup jrb=new ButtonGroup();
    public JTextArea jtextarea=new JTextArea("Welcome to the bank, you need to create a bank account at first.");// check account
    public JTextField jtextfield=new JTextField();  // input the money     
    private JButton Bankbutton1=new JButton("CreateAccount");
	private JButton Bankbutton2=new JButton("Withdraw");                
	private JButton Bankbutton3=new JButton("Deposit");     
	private JButton Bankbutton6=new JButton("Enter Market");  
	private Label query=new Label();  
	private JButton Bankbutton5=new JButton("CheckAccount");
	private Label Banklabel1=new Label(" Input the money");           
	private Label Banklabel3=new Label("                                            Transaction information");  //for bank     
	private Label Banklabel2=new Label("                                           Financial information");  //for bank     
	private Label ff=new Label("");
	private Label gg=new Label("");
    private JButton buy=new JButton("Buy"); 
    private JButton sell=new JButton("Sell"); 
    private JButton request=new JButton("request company information");// request the current company's sell and buy information
    private JButton request1=new JButton("request player information");// request the current player's sell and buy information
    private JButton show=new JButton("show my information"); // shows the player's information like the stocks
    private JButton startgame=new JButton("Start Game");
    private JButton enterbank=new JButton("Enter the Bank");
    public JTextArea jtextarea1;  // show the player's information
    public JTextArea jtextarea2;  // display the every transcation
    public JScrollPane j1;
    public JScrollPane j2;
    public JScrollPane j3;
    private Label stock=new Label("Input the name of the stock");
    public JTextField jtextfield1=new JTextField();   // for the name of the stock 
    private Label bid=new Label(" Bid ");
    public JTextField jtextfield2=new JTextField();     // input the bid
    private Label number=new Label(" Number ");
    public JTextField jtextfield3=new JTextField();      // input the number
   
    Player(){
		
	
	    Graphic2();   // the graphic of the input
		Graphic1();   // for the market   
		Graphic();     // for bank
		try{
			  g= new PlayerCallbackImpl(this);
			 
		     }
		catch(Exception e){			
		}
		
	}
   public double checkaccount(String name){
	   double nn=0;
	  try{
	  nn=h.CheckBalance(name);}
	  catch(Exception e){
		  
	  }
	  return nn;
   }
   public void Graphic(){
	    frame3.setLayout(new BorderLayout(5,10));
	    frame3.setSize(500,500);
	    frame3.setBounds(300,200,500,500);
	    JPanel p40=new JPanel();          
	    p40.setLayout(new GridLayout(2,1));
	    p40.add(Banklabel1);
	    p40.add(jtextfield);
	    JPanel p41=new JPanel();          
	    p41.setLayout(new GridLayout(1,2));
	    p41.add(p40);
	  
	   
	    p41.setBackground(Color.orange);
	  
		JPanel p30=new JPanel();          
	    p30.setLayout(new GridLayout(2,2,5,5));
      
	    p30.add(p41);
	    p30.add(Bankbutton5);
	    p30.add(Bankbutton3);   
	    p30.add( Bankbutton2);
	    p30.setBackground(Color.orange);
	    Banklabel1.setBackground(Color.orange);
	    JPanel p31=new JPanel();          
	    p31.setLayout(new GridLayout(1,2));
	    p31.add(p30);
	    p31.add(gg);
	    p31.setBackground(Color.orange);
	    JPanel p35=new JPanel();          
	    p35.setLayout(new GridLayout(2,1));
	    p35.add(p31);
	    JPanel p33=new JPanel();          
	    p33.setLayout(new GridLayout(2,1,5,5));
	    p33.add(Bankbutton1);	 
	    p33.add(Bankbutton6);
	    JPanel p36=new JPanel();          
	    p36.setLayout(new GridLayout(1,2));   
	    p36.add(ff);
	    p36.add(p33);
	    ff.setBackground(Color.orange);
	    p36.setBackground(Color.orange);
	    p35.add(p36);
	    j3=new JScrollPane(jtextarea,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
	    jtextarea.setLineWrap(true);
	    jtextarea.setForeground(Color.red);
	    jtextarea.setWrapStyleWord(true);
	    jtextarea.setEditable(false);
	   frame3.getContentPane().setBackground(Color.orange);
	   frame3.getContentPane().add(j3,BorderLayout.CENTER);
	   frame3.getContentPane().add(p35,BorderLayout.SOUTH);
      
       Bankbutton6.addActionListener(new ActionListener() { // go to the market
		    public void actionPerformed(ActionEvent evt) {
	          frame3.setVisible(false);
	          frame2.setVisible(true);
		   		      }
		    });
		Bankbutton2.addActionListener(new ActionListener() { // withdraw
		    public void actionPerformed(ActionEvent evt) {
		    try{
		    	if(account==0){
		    	
		    	double i=Double.parseDouble(jtextfield.getText().trim());
		    	if(i<=h.CheckBalance(name)){
		        jtextarea.setText("");
		        jtextarea.append("Withdraw succeeds"+'\n');
		    	h.Withdraw(name,i);
		    	jtextfield.setText("");
		    	balance+=i;}
		    	else{
		    		jtextarea.setText("");
		    		jtextarea.append("Withdraw fails"+'\n');}
		    	 
		    }}
		    catch(Exception e){	
		    }
		   		      }
		    });
		
		Bankbutton3.addActionListener(new ActionListener() { // Deposit
		    public void actionPerformed(ActionEvent evt) {
		    try{
		    	if(account==0){
		    	
		    	double i=Double.parseDouble(jtextfield.getText().trim()); 	
		    	if(i<=balance){
		    	h.Deposit(name, i);
		    	jtextfield.setText("");
		    	jtextarea.setText("");
		    	jtextarea.append("Deposit succeeds"+'\n');
		    	balance-=i;}
		    	else{
		    		jtextarea.setText("");
		    		jtextarea.append("Deposit fails"+'\n');
		    	}
		    }}
		    catch(Exception e){	
		    }
		   		      }
		    });
		Bankbutton1.addActionListener(new ActionListener() { // CreateAccount
		    public void actionPerformed(ActionEvent evt) {
		    try{
		    	if(account==1){
		        
		    	h.CreateAccount(name);
		    	account--;}
		    	
		    }
		    catch(Exception e){	
		    }
		   		      }
		    });
		Bankbutton5.addActionListener(new ActionListener() { // Check Account's balance 
		    public void actionPerformed(ActionEvent evt) {
		    try{
		    	if(account==0){
		    	jtextarea.setText("");
		    	jtextarea.append("Account's balance is :"+" "+h.CheckBalance(name)+'\n');	
		    	}
		    		    }
		    catch(Exception e){	
		    }
		   		      }
		    });
		
   }  // end Graphic
   public void Graphic2(){     // for the beginning 
	   frame1.setTitle("StockMarket");
	   Input.setBackground(Color.YELLOW);
	   frame1.setSize(400,100);
	   frame1.setBounds(500, 300,400,100);
        
	   frame1.setLayout(new GridLayout(2,2));
	   frame1.getContentPane().add(Input);
	   frame1.getContentPane().add(jtextfield5);
	   frame1.getContentPane().add(next);
	   frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	   frame1.setVisible(true);
	   frame2.setVisible(false);
	   frame3.setVisible(false);
	   next.addActionListener(new ActionListener() { 
		    public void actionPerformed(ActionEvent evt) {
		    	if(!jtextfield5.getText().trim().equals("")){
		      name=jtextfield5.getText().trim();
		      frame1.setVisible(false);	
		      frame2.setTitle("Stock Market"+"---"+name);
		      frame3.setTitle("Bank"+"---"+name);
		      frame2.setVisible(false);	
		      frame2.setVisible(true);	}
		   		      }
		    });    //end start game
	   
   } // end Graphic2
	public void Graphic1(){     // for the StockExchange
		
		frame2.setLayout(new BorderLayout(5,10));
	    frame2.setSize(1000,500);
	    frame2.setBounds(100,200,1000,500);
		j1=new JScrollPane(jtextarea1=new JTextArea(),
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		j2=new JScrollPane(jtextarea2=new JTextArea("Welcome to the game, at first you need to start the game."),
				 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
	    jtextarea1.setLineWrap(true);
	    jtextarea1.setWrapStyleWord(true);
	    jtextarea2.setLineWrap(true);
	    jtextarea2.setWrapStyleWord(true);
	    jtextarea2.setForeground(Color.blue);
		JPanel p3=new JPanel();
		p3.setLayout(new BorderLayout());
		p3.add(j2,BorderLayout.CENTER);
		p3.add(Banklabel3,BorderLayout.NORTH);
		Banklabel3.setBackground(Color.YELLOW);
		JPanel p4=new JPanel();     // for show my information
		p4.setLayout(new BorderLayout());
		   
		p4.add(j1,BorderLayout.CENTER);
		p4.add(Banklabel2,BorderLayout.NORTH);
		Banklabel2.setBackground(Color.YELLOW);
		JPanel p2=new JPanel();      // transcation imformation
		p2.setLayout(new GridLayout(1,1,5,5));
		p2.add(p3);
	    JPanel p11=new JPanel();                           // for Jbutton
		p11.setLayout(new GridLayout(3,1));
		p11.add(s1);
		p11.add(s2);
		p11.add(s3);
		jrb.add(s1);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -