密码输入后运行程序 [英] running program after password entry

查看:129
本文介绍了密码输入后运行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我在eclipse中写一个聊天服务器程序我有聊天客户端和聊天服务器互相交谈,想要添加一个用户名和密码条目,我有密码字段工作但我不能生命我想一下如何在点击提交按钮后运行聊天程序,任何帮助都会非常感谢,谢谢。





< pre lang =java> import javax.swing。*;

import java.awt。*;
import java.awt.event。*;

class 登录 extends JFrame 实现 ActionListener
{
JButton SUBMIT;
JPanel面板;
JLabel label1,label2;
final JTextField text1,text2;
登录()
{
label1 = new JLabel();
label1.setText( 用户名:);
text1 = new JTextField( 15 );

label2 = new JLabel();
label2.setText( 密码:);
text2 = new JPasswordField( 15 );

SUBMIT =新JButton( SUBMIT);

panel = new JPanel( new GridLayout( 3 1 ));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(SUBMIT);
add(panel,BorderLayout.CENTER);
SUBMIT.addActionListener( this );
setTitle( LOGIN FORM);
}
public void actionPerformed(ActionEvent ae)
{
String value1 = text1.getText();
String value2 = text2.getText();
if (value1.equals( Anfield )&& value2.equals( L1v3rp00l)){
聊天聊天=新聊天();
chat.setVisible(true);
JLabel label = new JLabel( Welcome : +值1);
chat.getContentPane()。add(label);
}
else {
System.out.println( 输入有效的用户名和密码);
JOptionPane.showMessageDialog( this 不正确登录或密码
错误,JOptionPane.ERROR_MESSAGE);
}
}
}
class LoginDemo
{
public static void main(字符串 arg [])
{
尝试
{
登录框架=新登录() ;
frame.setSize( 300 100 );
frame.setLocation( 60 40 );
frame.setVisible(true);
}
catch (例外e)
{JOptionPane.showMessageDialog(null,e.getMessage());}
}
}





[edit]已添加代码块 - OriginalGriff [/ edit]

解决方案

Richards评论是你需要做的简短形式:



创建一个在创建GUI之前调用的函数:



  public  字符串 [] doLogin(){
// 创建带有注释和2个文本字段的对话框
JTextField name = new JTextField();
JPasswordField pwd = new JPasswordField(); // 密码 - 仅显示*
Object [] message = {< span class =code-string> 请输入名称和密码\ n,name,pwd};
int iResponse = JOptionPane.showConfirmDialog(null,message, 登录 // 显示并等待对话框关闭
JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);

if (iResponse == JOptionPane.OK_OPTION){ // 检查对话框的退出条件
return new String [] {name.getText(),pwd.getPassword()); // 返回登录凭据
}
return null; // 您需要对null作出回应 - >最终应用程序的生命周期

}


Hi all im writing a chat server program in eclipse i have the chat client and chat server talking to each other and wanted to add a username and password entry to this, i have the password field working but i cant for the life of me think of how get the chat program to run after clicking the submit button, any help would be really appreciated, thanks.


import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
 
class Login extends JFrame implements ActionListener
{
 JButton SUBMIT;
 JPanel panel;
 JLabel label1,label2;
 final JTextField  text1,text2;
  Login()
  {
  label1 = new JLabel();
  label1.setText("Username:");
  text1 = new JTextField(15);

  label2 = new JLabel();
  label2.setText("Password:");
  text2 = new JPasswordField(15);
 
  SUBMIT=new JButton("SUBMIT");
  
  panel=new JPanel(new GridLayout(3,1));
  panel.add(label1);
  panel.add(text1);
  panel.add(label2);
  panel.add(text2);
  panel.add(SUBMIT);
  add(panel,BorderLayout.CENTER);
  SUBMIT.addActionListener(this);
  setTitle("LOGIN FORM");
  }
 public void actionPerformed(ActionEvent ae)
  {
  String value1=text1.getText();
  String value2=text2.getText();
  if (value1.equals("Anfield") && value2.equals("L1v3rp00l")) {
  Chat chat=new Chat();
  chat.setVisible(true);
  JLabel label = new JLabel("Welcome:"+value1);
  chat.getContentPane().add(label);
  }
  else{
  System.out.println("enter the valid username and password");
  JOptionPane.showMessageDialog(this,"Incorrect login or password",
  "Error",JOptionPane.ERROR_MESSAGE);
  }
}
}
 class LoginDemo
{
  public static void main(String arg[])
  {
  try
  {
  Login frame=new Login();
  frame.setSize(300,100);
  frame.setLocation(60,40);
  frame.setVisible(true);
  }
  catch(Exception e)
  {JOptionPane.showMessageDialog(null, e.getMessage());}
  }
}



[edit]Code block added - OriginalGriff[/edit]

解决方案

Richards comment is short form what you''ve got to do:

create a function that you call before you create your GUI:

public String[] doLogin(){
  //create Dialog with a Comment and 2 Textfields
  JTextField name = new JTextField();
  JPasswordField pwd = new JPasswordField(); // for password - will show only "*"
  Object[] message = { "Please enter Name and Password\n", name, pwd };
  int iResponse = JOptionPane.showConfirmDialog(null, message, "Login",  // show and wait for Dialog to be closed
      JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);

  if(iResponse== JOptionPane.OK_OPTION) { // check dialog's exit condition
      return new String[] {name.getText(), pwd.getPassword()); // returning the log in credentials 
  }
  return null; // You need to react on null as return -> end application's lifecycle
  
}


这篇关于密码输入后运行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆