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日

悬赏问题

  • ¥15 vb6.0+webbrowser无法加载某个网页求解
  • ¥15 RPA财务机器人采购付款流程
  • ¥15 计算机图形多边形及三次样条曲线绘制
  • ¥15 根据protues画的图用keil写程序
  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误
  • ¥20 cesm的气溶胶排放文件
  • ¥15 逐月累计,月份不连续,补齐月份
  • ¥15 应用简单的Python代码完成一个学生成绩管理系统
  • ¥15 用matlab求微分方程初值问题