[java]代码库
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package myframe;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
class AnnaPanel extends JPanel { //自定义一种面板
@Override
public void paintComponent(Graphics g) { //重写paintComponent()方法,在面板上画图片作为背景
ImageIcon icon = new ImageIcon("f://5.jpg");
g.drawImage(icon.getImage(), 0, 0, this);
}
}
public class Frame_Login extends JFrame implements ActionListener{
//将用到的组件定义成全局变量,这样在该类里的所有方法里都能访问
JLabel label1;
JLabel label2;
JTextField userID;
JTextField pwd;
JCheckBox autoLogin;
JCheckBox rememberPwd;
JButton login;
JButton cancel;
public Frame_Login() {
//设置窗体格式
this.setBounds(100, 100, 298, 300); //此处窗体大小和图片一致
this.setTitle("登录界面");
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
AnnaPanel panel = new AnnaPanel(); //创建一个自定义面板的对象,用于放置其他组件
panel.setLayout(null);
label1 = new JLabel("用户名");
label1.setBounds(50, 60, 80, 30);
panel.add(label1); //将label1添加到面板上
userID = new JTextField();
userID.setBounds(100, 60, 100, 30);
panel.add(userID); //将文本框添加到面板上
label2 = new JLabel("密码");
label2.setBounds(50, 100, 80, 30);
panel.add(label2); //将label2添加到面板上
pwd = new JTextField();
pwd.setBounds(100, 100, 100, 30);
panel.add(pwd);
autoLogin = new JCheckBox("自动登录");
autoLogin.setBounds(60, 150, 80, 30);
autoLogin.setOpaque(false);
panel.add(autoLogin); //将复选框添加到面板上
rememberPwd = new JCheckBox("记住密码");
rememberPwd.setBounds(150, 150, 80, 30);
rememberPwd.setOpaque(false);
panel.add(rememberPwd);
login = new JButton("登录");
login.setBackground(Color.pink);
login.setOpaque(false);
login.setBounds(50, 200, 90, 30);
login.addActionListener(this);
cancel = new JButton("取消");
cancel.setBackground(Color.pink);
cancel.setOpaque(false); //设置按钮背景透明
cancel.setBounds(150, 200, 90, 30);
panel.add(login); //将按钮添加到面板上
panel.add(cancel);
this.add(panel); //将面板添加到窗体上
this.setVisible(true);
}
public static void main(String[] args) {
Frame_Login frame = new Frame_Login();
}
@Override
public void actionPerformed(ActionEvent e) {
if(userID.getText().equals("anna")&&pwd.getText().equals("123"))
{
JFrame frame=new JFrame("登录成功");
frame.setBounds(100,100,200,200);
frame.setVisible(true);
}
else
JOptionPane.showMessageDialog(this, "用户名或密码不正确,请重新输入!");
}
}