在点击“取消”后,就无法判定ip和port是否正确了
代码:
package SRC;
//ClientMain.java
public class ClientMain {
static StartInterface RemoteConnection= new StartInterface();
public static void main(String[] args) {
RemoteConnection.setVisible(true);
}
}
package SRC;
//StartInterface.java
import java.awt.*;
import java.util.regex.*;
import javax.swing.*;
public class StartInterface extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
static ModeSettings MS = new ModeSettings();
static JLabel RemoteLink;
static JLabel IP;
static JTextField ip;
static JLabel Port;
static JTextField port;
static JButton Link;
static JButton Exit;
public StartInterface() {
super("远程连接");
JOptionPane P = new JOptionPane(null, JOptionPane.INFORMATION_MESSAGE);
RemoteLink = new JLabel("是否进行远程连接?");
IP = new JLabel("IP : ");
ip = new JTextField();
Port = new JLabel("Port : ");
port = new JTextField();
Link = new JButton("连接");
Exit = new JButton("退出");
Object[] Options = {RemoteLink,IP,ip,Port,port,Link,Exit};
P.setLayout(new GridLayout(3,2,5,5));
P.setMessage(Options);
setLayout(new FlowLayout(FlowLayout.LEFT));
add(P);
Link.addActionListener((e) -> {
if(port.getText() != null) {
if(IsTheDataLegal(ip.getText(),port.getText())) {
System.out.println(true);
this.setVisible(false);
MS.setVisible(true);
}
}
});
Exit.addActionListener((e) -> {
System.exit(0);
});
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenw = screenSize.width;
int screenh = screenSize.height;
setLocation((screenw - 300) / 2, (screenh - 225) / 2);
setSize(300,225);
}
public static boolean IsTheDataLegal(String IP,String Port) {
System.out.println(10);
String regex = "^((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(IP);
if(m.find()){
try {
String regex1 = "\\d{6}";
Pattern p1 = Pattern.compile(regex1);
Matcher m1 = p1.matcher(Port);
String regex2 = "\\d{5}";
Pattern p2 = Pattern.compile(regex2);
Matcher m2 = p2.matcher(Port);
String regex3 = "\\d{4}";
Pattern p3 = Pattern.compile(regex3);
Matcher m3 = p3.matcher(Port);
if(m1.find() || m2.find() || m3.find()) {
int PORT = Integer.parseInt(Port);
if(PORT > 1024 && PORT < 49151) {
return true;
}else{
JOptionPane.showMessageDialog(null,"Port输入不合法","警告",JOptionPane.WARNING_MESSAGE);
return false;
}
}else {
JOptionPane.showMessageDialog(null,"Port输入不合法","警告",JOptionPane.WARNING_MESSAGE);
}
}catch(Exception e) {
e.printStackTrace();
return false;
}
}else{
JOptionPane.showMessageDialog(null,"IP输入不合法","警告",JOptionPane.WARNING_MESSAGE);
return false;
}
return false;
}
}
package SRC;
//InterfaceSettings.java
import java.awt.*;
import javax.swing.*;
public class ModeSettings extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
static StartInterface SI = new StartInterface();
static JOptionPane P = new JOptionPane(null, JOptionPane.INFORMATION_MESSAGE);
static ButtonGroup bp = new ButtonGroup();
static JRadioButton ScreenProjection = new JRadioButton("仅投屏");
static JRadioButton RemoteControl = new JRadioButton("远程控制");
static JButton Yes = new JButton("确定");
static JButton Cancellation = new JButton("取消");
static JButton Exit = new JButton("退出");
public ModeSettings() {
super("模式设置");
Yes.addActionListener((e) ->{
});
Cancellation.addActionListener((e) ->{
this.setVisible(false);
SI.setVisible(true);
});
Exit.addActionListener((e) ->{
System.exit(0);
});
bp.add(ScreenProjection);
bp.add(RemoteControl);
Object[] Options = {ScreenProjection,RemoteControl,Yes,Cancellation,Exit};
P.setMessage(Options);
setLayout(new FlowLayout(FlowLayout.LEFT));
add(P);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenw = screenSize.width;
int screenh = screenSize.height;
setLocation((screenw - 300) / 2, (screenh - 200) / 2);
setSize(300,200);
}
}