ip_input.java
来自「软件工程实践课程的答案哦」· Java 代码 · 共 129 行
JAVA
129 行
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class IP_Input extends JDialog implements ActionListener
{
private JTextField text;
private JLabel label1;
private JButton confirm;
private GameFace face;
public IP_Input(GameFace face)
{
setTitle(
"Please Enter the Host IP Address");
this.face=face;
Container c=getContentPane();
c.setLayout(new FlowLayout());
label1=new JLabel("IP:");
text=new JTextField(20);
text.addActionListener(this);
confirm=new JButton("OK");
confirm.addActionListener(this);
c.add(label1);
c.add(text);
c.add(confirm);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setSize(300,120);
setLocation(245,200);
show();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==confirm||e.getSource()==text)
{
boolean flag=checkFormat();
if(flag==false)
{
JOptionPane.showMessageDialog(null,"Please check the IP format !");
text.requestFocus(true);
}
else
{
String s=(text.getText()).trim();
System.out.println(s);
Communication.setServerIP(s);
Communication.setGameFace(face);
Communication.init();
Name_Input name=new Name_Input(face);
//this.setVisible(false);
this.dispose();
}
}
}
public boolean checkFormat()
{
String s=text.getText();
String s1,s2,s3,s4="";
int[] n=new int[3];
int pointer=0;
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch=='.')
{
n[pointer]=i;
pointer++;
}
}
if(pointer!=3)
return false;
else
{
s1=s.substring(0,n[0]);
s2=s.substring(n[0]+1,n[1]);
s3=s.substring(n[1]+1,n[2]);
s4=s.substring(n[2]+1);
try{
int n1=Integer.parseInt(s1);
int n2=Integer.parseInt(s2);
int n3=Integer.parseInt(s3);
int n4=Integer.parseInt(s4);
if((n1>=0&&n1<256)&&(n2>=0&&n2<256)&&(n3>=0&&n3<256)&&(n4>=0&&n4<256))
return true;
else
return false;
}catch(NumberFormatException e)
{
return false;
}
}
}
public static void main(String[] args)
{
new IP_Input(new GameFace());
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?