package espc;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class LoginFrame extends JFrame {
JLabel jLabel2 = new JLabel();
// JPasswordField jPasswordField1 = new JPasswordField();
JLabel jLabel1 = new JLabel();
JTextField username = new JTextField();
JButton jButton1 = new JButton();
String bCommand="",userpw="",user="",ip="";
String ServletString="";
Properties prop = new Properties();
String command ="";
JPasswordField password = new JPasswordField();
JLabel jLabel4 = new JLabel();
public LoginFrame() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
try {
InputStream is = getClass().getResourceAsStream("ServletProperties.properties");
prop.load(is);
System.out.println("这是一句测试语句");
if(is != null)
is.close();
}
catch(IOException e){
System.out.println("打开文件ServletProperties.properties时出现错误!");
}
ServletString = prop.getProperty("LoginServletString");
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.setSize(new Dimension(265, 169));
this.setTitle("用户登陆");
this.addWindowListener(new LoginFrame_this_windowAdapter(this));
//定义窗口显示位置
Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
int w=this.getSize().width;
int h=this.getSize().height;
int x=(dim.width-w)/2;
int y=(dim.height-h)/2;
this.setLocation(x,y);
this.setResizable(false);
username.setText("");
username.setBounds(new Rectangle(80, 17, 121, 26));
jLabel1.setText("用户名");
jLabel1.setBounds(new Rectangle(28, 9, 76, 44));
jLabel1.setFont(new java.awt.Font("Dialog", 0, 12));
jLabel2.setFont(new java.awt.Font("Dialog", 0, 12));
jLabel2.setText("密 码");
jLabel2.setBounds(new Rectangle(26, 50, 76, 44));
this.getContentPane().setLayout(null);
jButton1.setBounds(new Rectangle(99, 100, 71, 30));
jButton1.setFont(new java.awt.Font("Dialog", 0, 12));
jButton1.setText("登 录");
jButton1.addActionListener(new loginFrame_jButton1_actionAdapter(this));
password.setBounds(new Rectangle(80, 59, 121, 26));
password.setSelectionStart(15);
password.setText("");
password.setBounds(new Rectangle(80, 61, 121, 27));
jLabel4.setBorder(BorderFactory.createLoweredBevelBorder());
jLabel4.setText("jLabel4");
jLabel4.setBounds(new Rectangle(52, 189, 166, 62));
jLabel4.setVisible(false);
this.getContentPane().add(jLabel2, null);
this.getContentPane().add(password, null);
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(username, null);
this.getContentPane().add(jLabel4, null);
this.getContentPane().add(jButton1, null);
this.getContentPane().add(password, null);
}
void jButton1_actionPerformed(ActionEvent e) {
user=username.getText();
userpw=password.getText();
if(!(user.equals("")||userpw.equals(""))){
URL server;
URLConnection con;
ObjectOutputStream req;
ObjectInputStream res = null;
command="login";
try {
server = new URL(ServletString); //以下三句建立连接
con = server.openConnection ();
con.setDoOutput (true);
req = new ObjectOutputStream (new BufferedOutputStream(con.getOutputStream ())); //提交请求
req.writeUTF(command);
req.writeUTF(user);
req.writeUTF(userpw);
req.flush ();
req.close ();
res = new ObjectInputStream (new BufferedInputStream(con.getInputStream ())); //返回响应状态
// bCommand="ok";
bCommand = (String)res.readObject();
ip= (String)res.readObject();
String bMessage=(String)res.readObject();
System.out.println(bCommand+ip+"连接成功!!!");
System.out.println(bMessage);
res.close ();
}
catch (MalformedURLException me) {
System.out.println("Malformed URL: " + ServletString);
}
catch (IOException ignored) {
System.out.println("io exception"+ignored.toString());
ignored.printStackTrace ();
}
catch (ClassNotFoundException ex) {
System.out.println("class not found");
}
finally {
}
if (bCommand != null && bCommand.equals("ok")) {
{
MainFrame tf=new MainFrame();
tf.setVisible(true);
this.dispose();
}
}
else {
//bMessage显示不能登录的原因;
jLabel4.setVisible(true);
jLabel4.setText("用户名或密码有误");
}
}
}
void this_windowOpened(WindowEvent e) {
}
void this_windowClosed(WindowEvent e) {
}
void this_windowClosing(WindowEvent e) {
this.dispose();
}
}
class loginFrame_jButton1_actionAdapter implements java.awt.event.ActionListener {
LoginFrame adaptee;
loginFrame_jButton1_actionAdapter(LoginFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class LoginFrame_this_windowAdapter extends java.awt.event.WindowAdapter {
LoginFrame adaptee;
LoginFrame_this_windowAdapter(LoginFrame adaptee) {
this.adaptee = adaptee;
}
public void windowOpened(WindowEvent e) {
adaptee.this_windowOpened(e);
}
public void windowClosed(WindowEvent e) {
adaptee.this_windowClosed(e);
}
public void windowClosing(WindowEvent e) {
adaptee.this_windowClosing(e);
}
}