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

📄 tframe.java~87~

📁 简单的俄罗斯方块游戏 源码 可 延伸成 网络版
💻 JAVA~87~
字号:
package tetrics;import java.awt.*;import java.awt.event.*;//import dialog.*;import javax.swing.*;//import client.*;import java.util.*;import com.borland.jbcl.layout.*;public class TFrame extends JFrame implements NetRead{    public Tetrics m_tetrics;//    public JPanel m_pStatus;    //public StatusBar m_pStatus;//    public JLabel m_lStatus;    //用于网络 //   public MyServer m_server; //   public MyClient m_client;    //定义表征现在网络状态的常量。    //NOCONNECT表示单机运行。    //SERVE表示这个游戏用作服务器。    //CLIENT表示这个游戏用作客户端    public static final int NOCONNECT=0,SERVER=1,CLIENT=2;    public int m_nNetStatus=NOCONNECT;  XYLayout xYLayout1 = new XYLayout();  JScrollPane jScrollPane1 = new JScrollPane();   JTextArea jTextArea1 = new JTextArea();   JTextField jTextField1 = new JTextField();   JButton jButton1 = new JButton();   JLabel jLabel1 = new JLabel();   private void addMenu()      {          MenuBar menuBar=new MenuBar();          MenuListener menuListener=new MenuListener(this);          MenuShortcut ms=new MenuShortcut(KeyEvent.VK_S);          Menu menu1=new Menu("游戏");          Menu menu2=new Menu("控制");          Menu menu3=new Menu("关于");          menuBar.add(menu1);          menuBar.add(menu2);          menuBar.add(menu3);          MenuItem menuItem1_1=new MenuItem("开始游戏");          MenuItem menuItem1_2=new MenuItem("暂停游戏");          MenuItem menuItem1_3=new MenuItem("结束游戏");          MenuItem menuItem1_4=new MenuItem("关闭");          menu1.add(menuItem1_1);          menu1.add(menuItem1_2);          menu1.add(menuItem1_3);          menu1.addSeparator();          menu1.add(menuItem1_4);                     //安装监听器          menuItem1_1.addActionListener(menuListener);          menuItem1_2.addActionListener(menuListener);          menuItem1_3.addActionListener(menuListener);          MenuItem menuItem2_1=new MenuItem("设置级别");          MenuItem menuItem2_2=new MenuItem("查看分数");          MenuItem menuItem2_3=new MenuItem("等待对方连接");          MenuItem menuItem2_4=new MenuItem("连接对方");//        MenuItem menuItem2_1=new MenuItem("设置游戏级别");          menu2.add(menuItem2_1);          menu2.add(menuItem2_2);          menu2.addSeparator();          menu2.add(menuItem2_3);          menu2.add(menuItem2_4);          menuItem2_1.addActionListener(menuListener);          menuItem2_2.addActionListener(menuListener);          menuItem2_3.addActionListener(menuListener);          menuItem2_4.addActionListener(menuListener);          MenuItem menuItem3_1=new MenuItem("关于");          menu3.add(menuItem3_1);          menuItem3_1.addActionListener(menuListener);          setMenuBar(menuBar);      }    public void ShowMessage(String str)    {    }    public TFrame()    {        super();        try        {            jbInit();            myInit();        }        catch(Exception e)        {            e.printStackTrace();        }    }    public static void main(String[] args)    {        TFrame mframe=new TFrame();        mframe.show();       // mframe.m_tetrics.start();    }    private void jbInit() throws Exception    {        this.addWindowListener(new java.awt.event.WindowAdapter() {            public void windowClosing(WindowEvent e) {                this_windowClosing(e);            }        });        this.addWindowListener(new java.awt.event.WindowAdapter()        {            public void windowClosing(WindowEvent e)            {                this_windowClosing(e);            }        });    this.getContentPane().setLayout(xYLayout1);    xYLayout1.setWidth(700);   xYLayout1.setHeight(600);   jTextArea1.setText("jTextArea1");   jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);   jTextField1.setText("jTextField1");   jButton1.setText("jButton1");   jLabel1.setText("jLabel1");   this.getContentPane().add(jScrollPane1,   new XYConstraints(5, 430, 430, 50));   this.getContentPane().add(jTextField1,   new XYConstraints(60, 480, 300, 26));   this.getContentPane().add(jButton1,  new XYConstraints(360, 480, 80, 26));   this.getContentPane().add(jLabel1, new XYConstraints(5, 480, 60, 26));   jScrollPane1.getViewport().add(jTextArea1, null);    }    private void myInit()    {        setSize(450,580);        resize(450,580);        setLocation(10,10);        addMenu();        JPanel gameP=new JPanel();        gameP.setLayout(null);        m_tetrics=new Tetrics(this);        m_tetrics.setBounds(0,0,Tetrics.WIDTH,Tetrics.HEIGHT);/*        m_pStatus=new JPanel();        m_pStatus.setBackground(Color.lightGray);        m_lStatus=new JLabel("游戏装载完毕");        m_lStatus.setBackground(Color.lightGray);        m_pStatus.add(m_lStatus);*/      //  m_pStatus=new StatusBar(this);     //   m_pStatus.setBounds(0,Tetrics.HEIGHT,Tetrics.WIDTH,Tetrics.HEIGHT+130);        gameP.add(m_tetrics);     //   gameP.add(m_pStatus);//        add(m_pStatus);        getContentPane().add(gameP);    }    void this_windowClosing(WindowEvent e)    {       // if(m_nNetStatus==SERVER)m_server.close();     //   if(m_nNetStatus==CLIENT)m_client.close();        System.exit(0);    }    public void insertScoreReport(int nScore)    {        Score score=new Score();        if(score.isScoreTop(nScore))        {            InsertURScoreP ius=new InsertURScoreP(this,"恭喜进入前十名",true);            ius.show();        }    }    /**     * 从对方读来字串时,调用这个方法     * @param str 读来的字串     */    public void readStr(String str)    {        //如果读来的数据是对方的状态信息        if(str.startsWith("Status:"))        {            int[] nRivalField=new int[Tetrics.m_nCols*(Tetrics.m_nRows+4)];            str=str.substring(7,str.length());            StringTokenizer st=new StringTokenizer(str,"|");            int i=0;            try            {                while(st.hasMoreTokens())                {                      nRivalField[i]=Integer.parseInt(st.nextToken());                      i++;                }            }catch(Exception e){}            i=0;            for(int col=0;col<Tetrics.m_nCols;col++)                for(int row=0;row<Tetrics.m_nRows;row++)                {                   // m_tetrics.m_nRivalField[col][row]=nRivalField[i];                    i++;                }        }        //如果读来的数据是对方消去一行的消息。        else if(str.equals("RemoveLine"))        {            m_tetrics.addRandomLine();        }        //如果读来的数据是对方开始游戏的信息        else if(str.equals("StartGame"))        {            m_tetrics.start();        }        //如果读来的数据是对方暂停游戏的信息        else if(str.equals("PauseGame"))        {            m_tetrics.pause();        }        //如果读来的数据是对方中止游戏的信息        else if(str.equals("StopGame"))        {            m_tetrics.stop();        }        //如果读来的数据是对方要求改变游戏级别的信息        else if(str.startsWith("Level:"))        {            str=str.substring(6,str.length());            try{                m_tetrics.setPlayLevel(Integer.parseInt(str));            }            catch(Exception e){}        }        //如果读来的数据是对方对你谈话的内容        else if(str.startsWith("Talk:"))        {            str=str.substring(5,str.length());          //  jTextArea1.append("对手:"+str+"\n");        }        //如果读来的数据是对方的游戏已经结束的信息        else if(str.startsWith("GameOver:"))        {            str=str.substring(9,str.length());            if(m_tetrics.m_bGameInPlay)            {                m_tetrics.m_bGameInPlay=false;                m_tetrics.stop();                //sendStr("GameOver:"+m_tetrics.m_nTheScore);            }            try{                int nRivalScore=Integer.parseInt(str);                //new GameOverD(this,m_tetrics.m_nTheScore,nRivalScore);            }catch(Exception e){e.printStackTrace();}        }    }    /**     * 用来在StatusBar里显示现在的联网状况     */    public void showMessage(String str)    {     //   m_pStatus.appendStr(str+"\n");    }    /**     * 将信息发给对方     */  public void sendStr(String str)    {      /*   switch(m_nNetStatus)        {            case SERVER:                if(m_server!=null)                    m_server.writeStr(str+"\n");                break;            case CLIENT:                if(m_client!=null)                    m_client.writeStr(str+"\n");                break;        }*/    } /* void jMenuItem1_actionPerformed(ActionEvent e) {       TFrame m_tFrame;       m_tetrics.start();  }*/class ActionListener implements java.awt.event.ActionListener {  TFrame adaptee;  ActionListener(TFrame adaptee) {    this.adaptee = adaptee;  }  public void actionPerformed(ActionEvent e) {  }}class MenuListener      implements java.awt.event.ActionListener {    TFrame m_tFrame;    public MenuListener(TFrame tFrame) {      m_tFrame = tFrame;    }    public void actionPerformed(ActionEvent e) {      //Invoked when an action occurs.      String sCommand = e.getActionCommand();      if (sCommand.equals("开始游戏")) {        //控制开始玩游戏的代码加到这儿                 System.out.println("开始游戏");        if (m_tFrame.m_nNetStatus == TFrame.CLIENT) {        // new WarningD(m_tFrame);          return;        }        if (m_tFrame.m_nNetStatus == TFrame.SERVER) {          m_tFrame.sendStr("StartGame");            m_tFrame.sendStr("Level:" + m_tFrame.m_tetrics.getPlayLevel());        }        m_tFrame.m_tetrics.start();                //   m_lStatus.setText("开始游戏");      }      else if (sCommand.equals("结束游戏")) {        //控制结束游戏的代码加到这儿。        if (m_tFrame.m_nNetStatus == TFrame.CLIENT) {          //  new WarningD(m_tFrame);          return;        }        if (m_tFrame.m_nNetStatus == TFrame.SERVER)        m_tFrame.sendStr("StopGame");          m_tFrame.m_tetrics.stop();                      //  m_lStatus.setText("结束游戏");      }      else if (sCommand.equals("暂停游戏")) {        //控制暂停游戏的代码加到这儿。        if (m_tFrame.m_nNetStatus == TFrame.CLIENT) {          //new WarningD(m_tFrame);          return;        }        if (m_tFrame.m_nNetStatus == TFrame.SERVER)           m_tFrame.sendStr("PauseGame");          m_tFrame.m_tetrics.pause();                      //    m_lStatus.setText("暂停游戏");      }      else if (sCommand.equals("关闭游戏")) {        //控制关闭游戏的代码加到这儿。       dispose();       //System.exit(0);        //                m_lStatus.setText("关闭游戏");      }      else if (sCommand.equals("设置级别")) {        //控制设置级别的代码加到这儿。        if (m_tFrame.m_nNetStatus == TFrame.CLIENT) {          //new WarningD(m_tFrame);          return;        }        JDialog d = new JDialog(m_tFrame, "设置级别");        // selectLevelP slp=new selectLevelP(m_tFrame,d);        //d.getContentPane().add(slp);        d.setSize(252, 126);        d.setLocation(400, 300);        //                System.out.println("ddddd");        d.show();      }      else if (sCommand.equals("查看分数")) {        //控制设置级别的代码加到这儿。        JDialog d = new JDialog(m_tFrame, "分数报告-Top10");        // ScoreReportP srp=new ScoreReportP(d);        // d.getContentPane().add(srp);        d.setSize(643, 300);        d.setLocation(400, 300);        d.show();      }      else if (sCommand.equals("等待对方连接")) {        //做一个服务器,等待对方连接        if (m_tFrame.m_nNetStatus == TFrame.CLIENT) {          //new WarningD(m_tFrame);          return;        }        // m_tFrame.m_server=new MyServer(m_tFrame);        System.out.println("开始监听");        //m_nNetStatus = SERVER;      }      else if (sCommand.equals("连接对方")) {        //连接对方        JDialog d = new JDialog(m_tFrame, "连接对方");        //d.getContentPane().add(new ConnectP(d,m_tFrame));        d.setSize(284, 131);        d.setLocation(400, 300);        d.show();      }      else if (sCommand.equals("关于")) {        JDialog d = new JDialog(m_tFrame, "关于");        //  d.getContentPane().add(new AboutP(d));        d.setSize(400, 320);        d.setLocation(400, 300);        d.show();        //控制显示游戏的关于对话框的代码加到这儿。      }    }  }}

⌨️ 快捷键说明

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