简体   繁体   中英

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.

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

    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. For example, I typed in 20500000 as my Server port. 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. 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. 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. 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.

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:

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);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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