📄 login.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Insets;
import java.awt.GridBagConstraints;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JTextPane;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.net.InetAddress;
import javax.swing.JPasswordField;
public class Login extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
private JPanel jPanel = null;
private JLabel jLabel = null;
private JTextField tf_Uname = null;
private JCheckBox jc_autolog = null;
private JCheckBox jc_rempass = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private JButton btn_Login = null;
private JButton btn_Back = null;
private JPanel jp=null;
private JPanel jPanel1 = null;
private JLabel jLabel3 = null;
private JLabel jLabel4 = null;
private JPasswordField tf_Pass = null;
private JLabel jl_LogInfo = null;
private MyQQ parentframe=null;
private JFrame fscreen=null;
public ReadConfig rc=null; // @jve:decl-index=0:
public String local_username=null;
public String server_addr=null;
public int server_port=0;
public String local_addr=null;
public int local_port=0;
public SendMsg sm=null;
public ReceivMsg rm=null;
public Timer timer=null;
public QQBar qqbar=null;
/**
* This is the default constructor
*/
public Login(JPanel jp,MyQQ parentframe) {
super();
this.jp=jp;
this.parentframe=parentframe;
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
jLabel4 = new JLabel();
jLabel4.setText("");
jLabel4.setPreferredSize(new Dimension(300, 100));
jLabel4.setBackground(Color.white);
jLabel4.setIcon(new ImageIcon(getClass().getResource("/top.PNG")));
this.setSize(451, 295);
this.setLayout(new BorderLayout());
this.add(getJPanel(), BorderLayout.CENTER);
this.add(getJPanel1(), BorderLayout.BEFORE_LINE_BEGINS);
this.add(jLabel4, BorderLayout.NORTH);
rc=new ReadConfig(); //获得设置
tf_Uname.setText(rc.getString("user",false));
tf_Pass.setText(rc.getString("pass", false));
if(rc.getString("alog", false).equals("1")) //自动登录
{
tf_Pass.setText(rc.getString("pass", false));//设置pass
jc_autolog.setSelected(true); //勾选"自动登录"
jc_rempass.setSelected(true); //勾选"记住密码"
}
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
gridBagConstraints12.gridx = 4;
gridBagConstraints12.insets = new Insets(0, 0, 15, 0);
gridBagConstraints12.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints12.gridy = 0;
jl_LogInfo = new JLabel();
jl_LogInfo.setText(" ");
GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
gridBagConstraints11.fill = GridBagConstraints.BOTH;
gridBagConstraints11.gridy = 5;
gridBagConstraints11.weightx = 1.0;
gridBagConstraints11.insets = new Insets(0, 0, 10, 10);
gridBagConstraints11.gridx = 4;
GridBagConstraints gridBagConstraints41 = new GridBagConstraints();
gridBagConstraints41.gridx = 4;
gridBagConstraints41.gridy = 9;
GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
gridBagConstraints31.gridx = 3;
gridBagConstraints31.fill = GridBagConstraints.NONE;
gridBagConstraints31.gridy = 9;
GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
gridBagConstraints21.gridx = 2;
gridBagConstraints21.gridwidth = 2;
gridBagConstraints21.insets = new Insets(0, 0, 15, 0);
gridBagConstraints21.fill = GridBagConstraints.NONE;
gridBagConstraints21.gridy = 0;
jLabel2 = new JLabel();
jLabel2.setText("用户登录");
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 3;
gridBagConstraints1.insets = new Insets(0, 20, 5, 0);
gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints1.gridy = 5;
jLabel1 = new JLabel();
jLabel1.setText("密 码:");
jLabel1.setAlignmentY(20.0F);
jLabel1.setAlignmentX(20.0F);
GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
gridBagConstraints5.gridx = 4;
gridBagConstraints5.insets = new Insets(0, 0, 0, 0);
gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints5.gridy = 7;
GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
gridBagConstraints4.gridx = 3;
gridBagConstraints4.insets = new Insets(0, 15, 0, 0);
gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints4.gridy = 7;
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints2.gridy = 3;
gridBagConstraints2.weightx = 1.0;
gridBagConstraints2.gridwidth = 1;
gridBagConstraints2.insets = new Insets(0, 0, 10, 10);
gridBagConstraints2.gridx = 4;
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.insets = new Insets(0, 20, 5, 0);
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridy = 3;
jLabel = new JLabel();
jLabel.setText("用户名:");
jLabel.setAlignmentY(20.0F);
jLabel.setAlignmentX(20.0F);
jPanel = new JPanel();
jPanel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
jPanel.setLayout(new GridBagLayout());
jPanel.setBackground(Color.white);
jPanel.add(jLabel, gridBagConstraints);
jPanel.add(getTf_Uname(), gridBagConstraints2);
jPanel.add(getJc_autolog(), gridBagConstraints4);
jPanel.add(getJc_rempass(), gridBagConstraints5);
jPanel.add(jLabel1, gridBagConstraints1);
jPanel.add(jLabel2, gridBagConstraints21);
jPanel.add(getBtn_Login(), gridBagConstraints31);
jPanel.add(getBtn_Back(), gridBagConstraints41);
jPanel.add(getTf_Pass(), gridBagConstraints11);
jPanel.add(jl_LogInfo, gridBagConstraints12);
}
return jPanel;
}
/**
* This method initializes tf_Uname
*
* @return javax.swing.JTextField
*/
private JTextField getTf_Uname() {
if (tf_Uname == null) {
tf_Uname = new JTextField();
tf_Uname.setPreferredSize(new Dimension(100, 22));
tf_Uname.setAlignmentY(20.0F);
tf_Uname.setHorizontalAlignment(JTextField.LEFT);
tf_Uname.setText("");
tf_Uname.setEditable(true);
tf_Uname.setAlignmentX(20.0F);
}
return tf_Uname;
}
/**
* This method initializes jc_autolog
*
* @return javax.swing.JCheckBox
*/
private JCheckBox getJc_autolog() {
if (jc_autolog == null) {
jc_autolog = new JCheckBox();
jc_autolog.setText("自动登录");
jc_autolog.setAlignmentY(20.0F);
jc_autolog.setHorizontalAlignment(SwingConstants.RIGHT);
jc_autolog.setBackground(Color.white);
jc_autolog.setAlignmentX(20.0F);
}
return jc_autolog;
}
/**
* This method initializes jc_rempass
*
* @return javax.swing.JCheckBox
*/
private JCheckBox getJc_rempass() {
if (jc_rempass == null) {
jc_rempass = new JCheckBox();
jc_rempass.setText("记住密码");
jc_rempass.setAlignmentY(20.0F);
jc_rempass.setBackground(Color.white);
jc_rempass.setAlignmentX(20.0F);
}
return jc_rempass;
}
/**
* This method initializes btn_Login
*
* @return javax.swing.JButton
*/
private JButton getBtn_Login() {
if (btn_Login == null) {
btn_Login = new JButton();
btn_Login.setText("登录");
btn_Login.addActionListener(this);
}
return btn_Login;
}
/**
* This method initializes btn_Back
*
* @return javax.swing.JButton
*/
private JButton getBtn_Back() {
if (btn_Back == null) {
btn_Back = new JButton();
btn_Back.addActionListener(this);
btn_Back.setText("返回");
}
return btn_Back;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn_Back)
{
this.setVisible(false);
jp.setVisible(true);
}
else if(e.getSource()==btn_Login)
{
setInfo("logging");
sendLogMsg();
}
}
public void sendLogMsg()
{
String uname=tf_Uname.getText().trim();
String passwd=tf_Pass.getText().trim();
if(uname.length()<=0)
{
setInfo("unamewrong");
}
else
{
if(passwd.length()<=0)
{
setInfo("passwdwrong");
}
else //发送登录信息
{
try
{
local_addr=InetAddress.getLocalHost().toString().split("\\/")[1];
}
catch(Exception e)
{
System.out.println("获取本地IP时发生异常!Login.java");
e.printStackTrace();
}
local_port=Integer.parseInt(rc.getString("lpor", false));
//"1|0"表示该消息需要回复,类别号为0
String msg="1|0|"+uname+"|"+passwd+"|"+local_addr+"|"+local_port;
server_port=Integer.parseInt(rc.getString("port", false));
server_addr=rc.getString("addr", false);
if(sm==null)
sm=new SendMsg(); //创建发送线程
if(rm==null)
rm=new ReceivMsg(sm,this); //创建接收线程
if(timer==null)
timer=new Timer(sm,rm); //创建定时器线程
rm.start(); //启动接收线程
sm.start(); //启动发送线程
//timer.start();
sm.setMsg(msg,server_port,server_addr,false,true); //发送用户名和密码
}
}
}
public void setInfo(String action)//设置此面板登录/登录错误信息提示
{
if(action.equals("success"))
{
jl_LogInfo.setText("登录成功!");
parentframe.setVisible(false);
local_username=tf_Uname.getText().trim();
qqbar=new QQBar(this,parentframe);
qqbar.setVisible(true);
}
else if(action.equals("fail"))
{
jl_LogInfo.setText("用户名或者密码错误!");
tf_Uname.setEditable(true); //恢复用户名可输入状态
tf_Pass.setEditable(true); //恢复密码可输入状态
btn_Login.setEnabled(true); //恢复登录按钮可用
}
else if(action.equals("timeout"))
{
jl_LogInfo.setText("连接超时!");
tf_Uname.setEditable(true); //恢复用户名可输入状态
tf_Pass.setEditable(true); //恢复密码可输入状态
btn_Login.setEnabled(true); //恢复登录按钮可用
}
else if(action.equals("logging"))
{
jl_LogInfo.setText("登录中...");
tf_Uname.setEditable(false); //设置用户名不可输入状态
tf_Pass.setEditable(false); //设置密码不可输入状态
btn_Login.setEnabled(false); //设置登录按钮不可用
}
else if(action.equals("unamewrong")) //用户名不正确
{
jl_LogInfo.setText("用户名不正确!");
tf_Uname.setEditable(true);
tf_Pass.setEditable(true);
btn_Login.setEnabled(true);
}
else if(action.equals("passwdwrong")) //用户名不正确
{
jl_LogInfo.setText("密码不正确!");
tf_Uname.setEditable(true);
tf_Pass.setEditable(true);
btn_Login.setEnabled(true);
}
}
/**
* This method initializes jPanel1
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel1() {
if (jPanel1 == null) {
GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
gridBagConstraints6.insets = new Insets(0, 0, 0, 0);
jLabel3 = new JLabel();
jLabel3.setText("");
jLabel3.setAlignmentX(1.0F);
jLabel3.setPreferredSize(new Dimension(130, 200));
jLabel3.setIcon(new ImageIcon(getClass().getResource("/QQ.png")));
jPanel1 = new JPanel();
jPanel1.setLayout(new GridBagLayout());
jPanel1.setBackground(Color.white);
jPanel1.add(jLabel3, gridBagConstraints6);
}
return jPanel1;
}
/**
* This method initializes tf_Pass
*
* @return javax.swing.JPasswordField
*/
private JPasswordField getTf_Pass() {
if (tf_Pass == null) {
tf_Pass = new JPasswordField();
tf_Pass.setPreferredSize(new Dimension(100, 22));
}
return tf_Pass;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -