📄 loginscreen.java
字号:
package clients;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class LoginScreen extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private static JButton cancelbutton = new JButton("取消");
private static JButton okbutton = new JButton("登陆");
private static JLabel passlabel = new JLabel("密码");
private static JLabel userlabel = new JLabel("用户名");
public JPasswordField passtext = new JPasswordField(20);
public JTextField usertext = new JTextField(20);
public static void main(String args[]) {
JFrame screen = new LoginScreen();
screen.repaint();
screen.setVisible(true);
screen.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
}
public LoginScreen() {
super("FTP用户登陆");
initialize();
}
public LoginScreen(String args) {
super("FTP用户登陆");
initialize();
};
private void initialize() {
setLocation(450, 250);
setSize(300, 150);
setResizable(false);
this.getContentPane().setLayout(null);
userlabel.setBounds(new Rectangle(15, 20, 60, 20));
passlabel.setBounds(new Rectangle(15, 50, 60, 20));
usertext.setBounds(80, 20, 160, 20);
passtext.setBounds(new Rectangle(80, 50, 160, 20));
okbutton.setBounds(new Rectangle(80, 80, 60, 20));
cancelbutton.setBounds(new Rectangle(150, 80, 60, 20));
getContentPane().add(userlabel);
getContentPane().add(passlabel);
getContentPane().add(usertext);
getContentPane().add(passtext);
getContentPane().add(okbutton);
getContentPane().add(cancelbutton);
ActionListener s = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
login();
}
};
this.getRootPane().registerKeyboardAction(s,
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
JComponent.WHEN_IN_FOCUSED_WINDOW);
okbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
login();
}
});
cancelbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
}
public void login() {
String name = usertext.getText();
String pass = new String(passtext.getPassword());
if (name.equals("")) {
JOptionPane.showMessageDialog(this, "用户名不能为空");
usertext.requestFocus();
return;
} else if (pass.equals("")) {
JOptionPane.showMessageDialog(this, "密码不能为空");
passtext.setText("");
passtext.requestFocus();
return;
}
check(name, pass);
}
public void check(String username, String pass) {
String name = username;
String password = pass;
openConnect(name, password);
}
public void openConnect(String name, String password) {
String text = "userlogin@user@" + name + "@password@" + password;
Socket s = null;
try {
s = new Socket("localhost", 21);
} catch (UnknownHostException e) {
JOptionPane.showMessageDialog(this, "无法连FTP接服务器");
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "无法连接FTP服务器");
}
if (s != null) {
PrintWriter out = null;
try {
out = new PrintWriter(s.getOutputStream());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "数据传输异常");
}
out.println(text);
out.flush();
redirect(s, name);
}
}
public void redirect(Socket s, String user) {
BufferedReader bu = null;
try {
bu = new BufferedReader(new InputStreamReader(s.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
try {
String line = bu.readLine();
if (line.equals("true")) {
dispose();
new FtpUserScreen(user, s);
} else {
JOptionPane.showMessageDialog(this, "用户名或密码有误");
usertext.setText("");
passtext.setText("");
usertext.requestFocus();
}
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "数据传输有误");
}
}
public void actionPerformed(ActionEvent arg0) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -