简体   繁体   English

从另一个Java文件调用的方法未运行

[英]Method called from another Java file is not running

I am having trouble calling the method verify() and connect() in the Client_GUI.java file, from my PasswordFrame.java file. 我从我的PasswordFrame.java文件中调用Client_GUI.java文件中的verify()connect()方法时遇到麻烦。

PasswordCheck.java PasswordCheck.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
{                                         
passwordCheck();
} 

public void passwordCheck(){
String pass = new String(jPasswordField1.getPassword());

    if (pass.equals(password))
    {
        Client_GUI clientGUI = new Client_GUI();
        clientGUI.verify();
        clientGUI.connect();
        dispose();
    }
    else
    {  
        System.out.println(pass);
        JOptionPane.showMessageDialog(null,
                "Incorrect password",
                "",
                JOptionPane.ERROR_MESSAGE);
    }

}

Client_GUI.java Client_GUI.java

    private void b_connectActionPerformed(java.awt.event.ActionEvent evt) 
    {                                          

    PasswordFrame passwordFrame = new PasswordFrame ();
    passwordFrame.show();
    }                                         

    private void tf_video_file_nameActionPerformed(java.awt.event.ActionEvent evt) {                                                   
   // TODO add your handling code here:
    }                                                  

    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new Client_GUI().setVisible(true);
        }
    });
}
    public void verify() {

    // SERVER IP ADDRESS INITIALISATION

    // SERVER PORT INITIALISATION
    if (!tf_server_port.getText().isEmpty()) {
        int ServerPort = Integer.parseInt(tf_server_port.getText());
        RTSP_server_port = ServerPort;
        if (ServerPort <= 1024 || ServerPort > 65535) {
            JOptionPane.showMessageDialog(null,
                    "Please enter a port number between 1024 and 65535",
                    "Wrong Port",
                    JOptionPane.ERROR_MESSAGE);
        }
    } else {
        JOptionPane.showMessageDialog(null,
                    "Please enter a port number between 1024 and 65535",
                    "Wrong Port",
                    JOptionPane.ERROR_MESSAGE);
    }

    // PROTOCOL TYPE INITIALISATION
    ButtonModel protocol_grp = bt_protocol_group.getSelection();
    if (protocol_grp != null){
        String selection = protocol_grp.getActionCommand();

        if(selection.equals("tcp")){
            protocolType = "tcp";
        }
        else if(selection.equals("udp")){
            protocolType = "udp";
        }
    }

    ButtonModel ports_grp = bt_ports_group.getSelection();
    if (ports_grp != null) {
        NoOfPorts = Integer.parseInt(ports_grp.getActionCommand());
    }

    ButtonModel vlc_grp = bt_ports_group.getSelection();
    if (vlc_grp != null) {
        String selection = vlc_grp.getActionCommand();

        if (selection.equals("yes")) {
            vlc_select = true;
        }
    }

    ButtonModel java_vlc = bt_java_vlc.getSelection();
    if (java_vlc != null) {
        String selection = java_vlc.getActionCommand();

        if (selection.equals("yes")) {
            //vlc_player player = new vlc_player();
            java_vlc_select = true;
        }
    }

    }
    System.out.println("All fields verified");

    }
    public void connect() {
    try {
        ServerIP = InetAddress.getByName(tf_server_IP.getText());

        client_fnc client = new client_fnc();
        client.setServerIP(ServerIP);
        client.setServerIP(RTSP_server_port);
        client.setServerIP(VideoFileName);
        client.setframe_start_vlc(50);
        client.setBuffer_Size(bufferSize);
        client.setNoOfPorts(NoOfPorts);
        client.setProtocolType(protocolType);
        client.setFilePath(stringFilePath);
        client.setVLCPath(stringVLCPath);
        client.setencryption_type(encryption_type);
        client.setServerIP(java_vlc_select);
        client.setEncSelected(encryption_select);
        client.setStartVLCatFrame(FRAME_START_VLC);

        try {
            Thread client_thread = new Thread(client);
            client_thread.start();
            client.addObserver(Client_GUI.this);
        } catch (Exception ex) {
            Logger.getLogger(this.getName()).log(Level.SEVERE, "WRONG IP ", ex);
        }
    } catch (UnknownHostException ex) {
        Logger.getLogger(this.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(null,
                "Please enter a correct IP address.",
                "IP Address wrong",
                JOptionPane.ERROR_MESSAGE);
    }
    System.out.println("Connected to Server");
}

When I type in "password" in the password field and click the jButton1 , the password window just closes without verifying fields in my Client_GUI window. 当我在密码字段中输入“ password”并单击jButton1时 ,密码窗口将关闭,而无需验证Client_GUI窗口中的字段。 For example, I typed in 20500000 as my Server port. 例如,我输入20500000作为我的服务器端口。 If I call methods verify() and connect() in the Client_GUI.java file itself, the error message saying "Please enter a port number between 1024 and 65535" "Wrong Port" would pop up. 如果我在Client_GUI.java文件本身中调用方法verify()connect() ,则会弹出错误消息“请输入介于1024和65535之间的端口号”“错误的端口”。 However, when I call methods verify() and connect() from the PasswordFrame file, no error message pops up, only "All fields verified" and "Connected to Server" at the end of the code appears. 但是,当我从PasswordFrame文件调用方法verify()connect()时 ,没有错误消息弹出,代码末尾仅显示“所有字段已验证”和“连接到服务器”。 It seems that certain parts of the codes are being skipped. 似乎代码的某些部分已被跳过。 Why is that? 这是为什么?

EDIT: Sorry for being vague. 编辑:对不起,含糊其辞。 The flow I'm looking for is basically this: 1. When b_button in the Client_GUI frame is pressed, the PasswordFrame pops up, where users need to enter their password. 我在寻找的流动基本上是这样的:1.当Client_GUI框架b_button被按下时,PasswordFrame弹出,即用户需要输入自己的密码。 2. When the jButton in PasswordFrame is pressed, the PasswordCheck method is called and checks whether the text entered ie pass = 'password' 3. If it is, the PasswordFrame is closed and runs the verify() and connect() methods. 2.按下PasswordFrame中的jButton时,将调用PasswordCheck方法并检查是否输入了文本,即pass ='password'。3.如果是,则关闭PasswordFrame并运行verify()connect()方法。

thanks for trying to help out. 感谢您的帮助。 I've decided to simply add the password input field in the Client_GUI window instead of having another separate frame, and simply call this method when b_button is pressed: 我决定仅在Client_GUI窗口中添加密码输入字段,而不是使用另一个单独的框架,并在按下b_button时简单地调用此方法:

public void passwordCheck(){
String pass = new String(jPasswordField1.getPassword());

    if (pass.equals(password))
    {
        verify();
        connect();

    }
    else
    {  
        System.out.println(pass);
        JOptionPane.showMessageDialog(null,
                "Incorrect password",
                "",
                JOptionPane.ERROR_MESSAGE);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM