📄 enterframe.java
字号:
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class EnterFrame extends JFrame implements ActionListener{
JButton b1=new JButton("建立游戏");
JButton b2=new JButton("加入游戏");
JButton b3=new JButton("连接");
JButton b4=new JButton("关闭");
TextArea ta1=new TextArea(4,5);
JTextField tf1=new JTextField(15);
JLabel l1=new JLabel("输入IP地址:");
JLabel l2=new JLabel("端口:");
JTextField tf2=new JTextField(7);
JPanel p1=new JPanel();
JPanel p2=new JPanel();
Socket client;
EnterFrame(){
///////////界面初始化
getContentPane().setLayout(null);
getContentPane().add(p1);
getContentPane().add(p2);
p1.setBounds(0, 20, 550, 200);
p2.setBounds(0, 220, 550, 300);
setSize(550,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width-500)/2,(screen.height-400)/2);
setTitle("welcome to my world!");
setVisible(true);
setResizable(false);
init_p1();
init_p2();
/////////////////
}
public void init_p1(){
p1.setLayout(null);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(l1);
p1.add(l2);
p1.add(tf1);
p1.add(tf2);
//
b1.setBounds(20, 20, 120, 30);
l1.setBounds(20, 80, 80, 30);
tf1.setBounds(110, 80, 100, 30);
l2.setBounds(240,80,40,30);
tf2.setBounds(300,80,80,30 );
b3.setBounds(410, 80, 120, 30);
b2.setBounds(20, 140, 120, 30);
b4.setBounds(410, 20,120, 30);
///
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
/////
b2.setEnabled(false);
tf1.setText("127.0.0.1");
tf2.setText("4444");
}
public void init_p2(){
p2.add(ta1);
p2.setLayout(null);
JLabel l3=new JLabel("游戏信息");
p2.add(l3);
ta1.setBounds(20, 30, 500,220);
l3.setBounds(20, 0, 100, 30);
ta1.setEditable(false);
ta1.setText("欢迎使用联网梭哈游戏小程序...你可以选择建立游戏或寻找服务器游戏来加入!\n");
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
b1.setEnabled(false);
tf1.setEnabled(false);
tf2.setEnabled(false);
this.setVisible(false);
new gameServer(Integer.parseInt(tf2.getText()));
}
if(e.getSource()==b3){//连接
String sIP=tf1.getText().trim();
String sPORT=tf2.getText().trim();
ta1.append("正在连接 "+sIP);
ta1.append("端口 "+sPORT+"...\n");
try{
client=new Socket(sIP,Integer.parseInt(sPORT));
ta1.append("成功!可以加入游戏了...\n");
b2.setEnabled(true);
}catch(IOException e1){
ta1.append(e1.getMessage()+" ....连接失败\n");
b3.setEnabled(true);
}
}
if(e.getSource()==b2){//加入游戏
this.setVisible(false);
new gameClient(client);
}
if(e.getSource()==b4){
System.exit(0);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -