从另一个Java文件调用的方法未运行
从PasswordFrame.java文件调用Client_GUI.java文件中的方法verify()和connect()时遇到问题 PasswordCheck.java从另一个Java文件调用的方法未运行,java,Java,从PasswordFrame.java文件调用Client_GUI.java文件中的方法verify()和connect()时遇到问题 PasswordCheck.java private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { passwordCheck(); } public void passwordCheck(){
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);
}
}
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");
}
Client\u GUI.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);
}
}
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");
}
private void b_connectActionPerformed(java.awt.event.ActionEvent evt)
{
PasswordFrame PasswordFrame=新的PasswordFrame();
passwordFrame.show();
}
私有void tf_video_file_nameActionPerformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
}
公共静态void main(字符串参数[]){
invokeLater(new Runnable()){
公开募捐{
新客户端_GUI().setVisible(true);
}
});
}
公共无效验证(){
//服务器IP地址初始化
//服务器端口初始化
如果(!tf_server_port.getText().isEmpty()){
int ServerPort=Integer.parseInt(tf_server_port.getText());
RTSP_服务器_端口=服务器端口;
如果(服务器端口65535){
JOptionPane.showMessageDialog(null,
“请输入介于1024和65535之间的端口号”,
“错误端口”,
JOptionPane.ERROR\u消息);
}
}否则{
JOptionPane.showMessageDialog(null,
“请输入介于1024和65535之间的端口号”,
“错误端口”,
JOptionPane.ERROR\u消息);
}
//协议类型初始化
ButtonModel protocol_grp=bt_protocol_group.getSelection();
if(协议组!=null){
字符串选择=协议_grp.getActionCommand();
if(selection.equals(“tcp”)){
protocolType=“tcp”;
}
else if(selection.equals(“udp”)){
protocolType=“udp”;
}
}
ButtonModel ports_grp=bt_ports_group.getSelection();
如果(端口数)\u grp!=null){
noofport=Integer.parseInt(ports_grp.getActionCommand());
}
ButtonModel vlc_grp=bt_ports_group.getSelection();
如果(vlc_grp!=null){
字符串选择=vlc_grp.getActionCommand();
if(selection.equals(“是”)){
vlc_select=true;
}
}
ButtonModel java_vlc=bt_java_vlc.getSelection();
if(java_vlc!=null){
字符串选择=java_vlc.getActionCommand();
if(selection.equals(“是”)){
//vlc_播放器=新vlc_播放器();
java_vlc_select=true;
}
}
}
System.out.println(“所有字段已验证”);
}
公共void connect(){
试一试{
ServerIP=InetAddress.getByName(tf_server_IP.getText());
client_fnc client=新客户端_fnc();
client.setServerIP(ServerIP);
客户端.setServerIP(RTSP_服务器_端口);
client.setServerIP(VideoFileName);
客户端设置帧启动vlc(50);
客户端。设置缓冲区大小(缓冲区大小);
客户端。setNoOfPorts(NoOfPorts);
client.setProtocolType(protocolType);
client.setFilePath(stringFilePath);
client.setVLCPath(stringVLCPath);
client.setencryption_type(加密_type);
setServerIP(java_vlc_select);
client.setEncSelected(加密\选择);
client.setStartVLCatFrame(FRAME_START_VLC);
试一试{
线程客户端\线程=新线程(客户端);
client_thread.start();
client.addObserver(client\u GUI.this);
}捕获(例外情况除外){
Logger.getLogger(this.getName()).log(Level.SEVERE,“错误的IP”,例如);
}
}捕获(未知后异常除外){
Logger.getLogger(this.getName()).log(Level.SEVERE,null,ex);
JOptionPane.showMessageDialog(null,
“请输入正确的IP地址。”,
“IP地址错误”,
JOptionPane.ERROR\u消息);
}
System.out.println(“连接到服务器”);
}
当我在password字段中键入“password”并单击jButton1时,password窗口将关闭,而不验证客户端GUI窗口中的字段。例如,我输入20500000作为我的服务器端口。如果我在Client_GUI.java文件本身中调用方法verify()和connect(),则会弹出错误消息“请输入介于1024和65535之间的端口号”“错误端口”。但是,当我从PasswordFrame文件调用方法verify()和connect()时,不会弹出错误消息,只会在代码末尾显示“所有字段已验证”和“已连接到服务器”。似乎代码的某些部分被跳过了。为什么呢
编辑:抱歉说得含糊不清。我要寻找的流程基本上是这样的:
1.当按下客户端GUI框架中的b_按钮时,会弹出密码框架,用户需要在其中输入密码。
2.当按下PasswordFrame中的jButton时,将调用PasswordCheck方法并检查输入的文本是否正确,即pass='password'
3.如果是,则关闭PasswordFrame并运行verify()和connect()方法 谢谢你的帮助。我决定在Client_GUI窗口中简单地添加密码输入字段,而不是使用另一个单独的框架,并在按下b_按钮时简单地调用此方法:
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);
}
}
据我所知,当从另一个类调用ClientGUI时,没有显示给用户。你没有显示构造函数,所以我们不知道那里发生了什么。该字段是否已初始化为有效值?可能在您可以使用客户端GUI文件时,tf_服务器_端口的值已正确设置,因此显示相应的错误。当您从passwordcheck调用时,只需将其初始化为new Client_GUI()并调用该方法。构造函数是否初始化或设置tf_服务器_端口变量的值?能否解释
pas之间的关系