📄 frame1.java
字号:
contentPane.add(jLabel5, new XYConstraints(715, 470, 25, 25));
contentPane.add(jLabel7, new XYConstraints(770, 470, 25, 25));
contentPane.add(jTextField2, new XYConstraints(181, 564, 234, 21));
contentPane.add(jLabel3, new XYConstraints(133, 562, 48, 23));
contentPane.add(jLabel4, new XYConstraints(429, 117, 255, 451));
contentPane.add(jLabel9, new XYConstraints(10, 550, 90, 30));
contentPane.add(jButton13, new XYConstraints(710, 528, 70, 30));
contentPane.add(jLabel1, new XYConstraints(1, 0, 813, 638));
tetrics.m_sound.jScrollPane1.getViewport().add(jTextArea1, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
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<m_oGameArea.m_nCols;col++)
for(int row=0;row<m_oGameArea.m_nRows;row++)
{
m_oGameArea.m_nRivalField[col][row]=nRivalField[i];
///////////////////////////////////////////////////////////////////////////////////////
i++;
}
}
//如果读来的数据是添加一行的消息。
else if(str.equals("addRandomLine"))
{
tetrics.addRandomLine();
}
//如果读来的数据是随机添加一一些方块的消息。
else if(str.equals("addRandomBox"))
{
tetrics.addRandomBox();
}
//如果读来的数据是加快方块下落的消息。
else if(str.equals("addSpeed"))
{
tetrics.addspeed();
}
//如果读来的数据是对方游戏角色的信息
else if(str.startsWith("enemyPlayer:")){
str=str.substring(12,str.length());
try{
enemyuser=str;
}
catch(Exception e){}
jLabel9.setText(enemyuser);
}
//如果读来的数据是对方开始游戏的信息
else if(str.equals("StartGame"))
{
tetrics.start();
}
//如果读来的数据是对方暂停游戏的信息
else if(str.equals("PauseGame"))
{
tetrics.pause();
}
//如果读来的数据是对方中止游戏的信息
else if(str.equals("StopGame"))
{
tetrics.stop();
}
//如果读来的数据是对方要求改变游戏级别的信息
else if(str.startsWith("Level:"))
{
str=str.substring(6,str.length());
try{
tetrics.setPlayLevel(Integer.parseInt(str));
}
catch(Exception e){}
}
//如果读来的数据是对方对你谈话的内容
else if(str.startsWith("Talk:"))
{
str=str.substring(5,str.length());
if(m_nNetStatus==CLIENT)
jTextArea1.append("服务器:"+str+"\n");
else if (m_nNetStatus==SERVER)
jTextArea1.append("客户机:"+str+"\n");
else
jTextArea1.append("我:"+str+"\n");
}
//如果读来的数据是对方的游戏已经结束的信息
else if(str.startsWith("GameOver:"))
{
str=str.substring(9,str.length());
if(tetrics.m_bGameInPlay)
{
tetrics.m_bGameInPlay=false;
tetrics.stop();
sendStr("GameOver:"+tetrics.sScore.m_nTheScore);
}
try{
int nRivalScore=Integer.parseInt(str);
ScoreReport(tetrics.sScore.m_nTheScore,nRivalScore);
}catch(Exception e){e.printStackTrace();}
}
/* else if(str.startsWith("MyScore:")){
str=str.substring(8,str.length());
try{
System.out.println(str);
m_oScore=Integer.parseInt(str);
}
catch(Exception e){}
}*/
}
void this_windowClosing(WindowEvent e)
{
if(m_nNetStatus==SERVER)m_server.close();
if(m_nNetStatus==CLIENT)m_client.close();
System.exit(0);
}
////////////////////////////////////////////////////
public void ScoreReport(String name,int nScore){
QueFrame qf=new QueFrame(jLabel8.getText(),nScore);
qf.setSize(210,230);
qf.setLocation(450,200);
qf.show();
}
////////////////////////////////////////////////////
public void ScoreReport(int nScore,int oScore){
//将分数及玩家信息存入数据库
netScoreReportFrame nrst = new netScoreReportFrame();
nrst.m_tFrame=this;
nrst.username.setText(jLabel8.getText());
nrst.score.setText(String.valueOf(nScore));
nrst.o_username.setText(jLabel9.getText());
nrst.o_score.setText(String.valueOf(oScore));
nrst.setSize(210, 230);
nrst.setLocation(450, 200);
nrst.show();
}
/**
* 用来在StatusBar里显示现在的联网状况
*/
public void showMessage(String str)
{
jTextArea1.append(str+"\n");
}
/**
* 将信息发给对方
*/
public void sendStr(String str)
{ // System.out.println("就TMD不能发送么???????????????????");
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 jButton1_actionPerformed(ActionEvent e) {
//连接
connectDialog d=new connectDialog(this);
d.setSize(300,150);
d.setLocation(400,300);
d.show();
}
void jButton8_actionPerformed(ActionEvent e) {
//打开音乐文件
tetrics.m_sound.open_voicefile();
}
void jButton9_actionPerformed(ActionEvent e) {
//播放音乐
tetrics.requestFocus();//将焦点转回游戏区
tetrics.m_sound.play_voice();
jTextField1.setText("正在播放" + tetrics.m_sound.name);
jButton9.setEnabled(false); //将jButton9设置为不可用
jButton10.setEnabled(true); //将jButton10设置为可用
jButton8.setEnabled(false);
}
void jButton10_actionPerformed(ActionEvent e) {
//停止播放音乐
tetrics.requestFocus();//将焦点转回游戏区
tetrics.m_sound.stop_voice();
jTextField1.setText("停止");
jButton9.setEnabled(true); //将jButton9设置为可用
jButton10.setEnabled(false); //将jButton10设置为不可用
jButton8.setEnabled(true);
}
void jButton4_actionPerformed(ActionEvent e) {
//关于
this.setEnabled(false);
Dialog8 F=new Dialog8();
F.f=this;
F.setLocation(360,170);
F.show();
}
void jButton3_actionPerformed(ActionEvent e) {
//帮助
this.setEnabled(false);
Dialog9 F=new Dialog9();
F.f=this;
F.setLocation(360,170);
F.show();
}
//
void jButton7_actionPerformed(ActionEvent e) {
//设置本机为服务器
m_server=new MyServer(this);
System.out.println("开始监听");
m_nNetStatus=SERVER;
tetrics.requestFocus();//将焦点转回游戏区
}
void jButton5_actionPerformed(ActionEvent e) {
//退出
exitFrame ef=new exitFrame(this);
ef.setSize(210,160);
ef.setLocation(300,300);
ef.show();
//System.exit(0);
}
void jButton6_actionPerformed(ActionEvent e) {
//设置
tetrics.requestFocus();//将焦点转回游戏区
//控制设置级别的代码加到这儿
levelDialog slp=new levelDialog(this,"设置级别",true);//实例化 levelDialog
slp.tframe=this;//将本窗体中的设置传给设置级别窗体
slp.resize(320,200);//设置窗体大小
slp.setLocation(340,280);
slp.show();//显示窗体
}
void jButton2_actionPerformed(ActionEvent e) {
tetrics.requestFocus();//将焦点转回游戏区
//控制开始玩游戏的代码加到这
// if(jButton2.getText().equals("开始")){
if (m_nNetStatus == CLIENT) { //tetrics.m_theThread.suspend();//暂停线程
// tFrame.sendStr("PauseGame");
JOptionPane.showConfirmDialog(tFrame,"客户机无权做此操作!!!","提示",
JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);//若是客户机,则不允许其做此操作
// if(tetrics.b==0){
// tetrics.start();//继续游戏
// tFrame.sendStr("StartGame");
// }
}
//
if (m_nNetStatus == SERVER || m_nNetStatus == NOCONNECT) { //给客户机发送消息
sendStr("StartGame"); //
sendStr("Level:" + tetrics.getPlayLevel());
// if(tetrics.m_theThread!=null&&tetrics.b==0){//游戏开始状态
//暂停线程
// tetrics.m_theThread.suspend();//暂停线程
// tFrame.sendStr("PauseGame");
// JOptionPane.showConfirmDialog(tFrame, "游戏进行中,不能开始游戏!", "提示",
// JOptionPane.DEFAULT_OPTION,
// JOptionPane.WARNING_MESSAGE); //若游戏进行中,则不能开始
// tFrame.sendStr("StartGame");//继续游戏
tetrics.start();
// }
// else if(tetrics.m_theThread==null||tetrics.b==1)
// tetrics.start(); // 开始游戏
// System.out.println("开始游戏");
// jButton2.setText("结束");
}
/* if(jButton2.getText().equals("结束")){
//if(tetrics.nextPiece.m_bGameInPlay){
if (m_nNetStatus == CLIENT) {}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -