簡體   English   中英

從另一個Java文件調用的方法未運行

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

我從我的PasswordFrame.java文件中調用Client_GUI.java文件中的verify()connect()方法時遇到麻煩。

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

當我在密碼字段中輸入“ password”並單擊jButton1時 ,密碼窗口將關閉,而無需驗證Client_GUI窗口中的字段。 例如,我輸入20500000作為我的服務器端口。 如果我在Client_GUI.java文件本身中調用方法verify()connect() ,則會彈出錯誤消息“請輸入介於1024和65535之間的端口號”“錯誤的端口”。 但是,當我從PasswordFrame文件調用方法verify()connect()時 ,沒有錯誤消息彈出,代碼末尾僅顯示“所有字段已驗證”和“連接到服務器”。 似乎代碼的某些部分已被跳過。 這是為什么?

編輯:對不起,含糊其辭。 我在尋找的流動基本上是這樣的:1.當Client_GUI框架b_button被按下時,PasswordFrame彈出,即用戶需要輸入自己的密碼。 2.按下PasswordFrame中的jButton時,將調用PasswordCheck方法並檢查是否輸入了文本,即pass ='password'。3.如果是,則關閉PasswordFrame並運行verify()connect()方法。

感謝您的幫助。 我決定僅在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