2009penguin 2023-04-26 21:53 采纳率: 54.5%
浏览 44

无法判定ip和port是否正确

在点击“取消”后,就无法判定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);
    }
}
  • 写回答

2条回答 默认 最新

  • ha_lydms 后端领域优质创作者 2023-04-26 23:01
    关注

    你的需求,目的是什么呢?感觉你写挺好的呀,没啥问题。

    评论

报告相同问题?

问题事件

  • 创建了问题 4月26日

悬赏问题

  • ¥50 前后端数据顺序不一致问题,如何解决?(相关搜索:数据结构)
  • ¥15 基于蒙特卡罗法的中介效应点估计代码
  • ¥15 罗技G293和UE5.3
  • ¥20 Tesla 特斯拉K80显卡 如果需要使用该设备,你需要禁用系统上的另一个设备。
  • ¥30 QT调用百度智能云千帆模型无法取得返回文本
  • ¥50 CCD工业视觉相机检测出现光边
  • ¥60 二次元手游日常任务自动化代肝(相关搜索:自动化)
  • ¥15 mysql将查询的结果作为动态列名怎么实现
  • ¥50 python自动地图截图脚本
  • ¥15 fastreport怎么判断当前页数