- package edu.jmi.xyh.view;
import java.awt.*;
public class LoginView_3 extends JFrame {
//创建一个容器
Container ct;
//创建背景面板。
BackgroundPanel bgp;
public static void main(String[] args)
{
new LoginView_3();
}
public LoginView_3()
{
//不采用任何布局方式。
ct=this.getContentPane();
getContentPane().setLayout(null);
//在这里随便找一张400*300的照片既可以看到测试结果。
bgp=new BackgroundPanel((new ImageIcon("C:\\Users\\asus123\\Desktop\\images\\2.jpg")).getImage());
bgp.setBounds(0,0,400,300);
ct.add(bgp);
bgp.setLayout(null);
this.setSize(400,300);
this.setLocation(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class BackgroundPanel extends JPanel
{
Image im;
public BackgroundPanel(Image im)
{
this.im=im;
this.setOpaque(true);
}
//Draw the back ground.
public void paintComponent(Graphics g)
{
super.paintComponents(g);
g.drawImage(im,0,0,this.getWidth(),this.getHeight(),this);
}
}
2. package edu.jmi.xyh.view;
import java.awt.BorderLayout;
public class LoginView extends JFrame {
private JPanel contentPane;
private JTextField txID;
private JPasswordField txPw;
protected int loginCount;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginView frame = new LoginView();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LoginView() {
setTitle("\u6B22\u8FCE\u767B\u5F55\u5B66\u751F\u7BA1\u7406\u7CFB\u7EDF");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 445, 306);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setForeground(Color.BLACK);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D\uFF1A");
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 12));
lblNewLabel.setBounds(114, 98, 54, 15);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("\u5BC6\u7801\uFF1A");
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 12));
lblNewLabel_1.setBounds(114, 135, 54, 15);
contentPane.add(lblNewLabel_1);
txID = new JTextField();
txID.setBounds(204, 95, 118, 21);
contentPane.add(txID);
txID.setColumns(10);
txPw = new JPasswordField();
txPw.setBounds(204, 132, 118, 21);
contentPane.add(txPw);
JButton btnNewButton = new JButton("\u767B\u5F55");
btnNewButton.setFont(new Font("宋体", Font.PLAIN, 12));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//登陆代码,将用户输入的内容打印在控制台中
String id=txID.getText();
String pw=txPw.getText();
//调用studentDao查询是否有该学生信息
Student student=StudentDao.query(id);
loginCount++;
if(student==null || !pw.equals(student.getStuPwd())){
if(loginCount<4)
JOptionPane.showMessageDialog(null, "您的用户名或者密码输入错误");
else{
JOptionPane.showMessageDialog(null, "您以输错三次退出登录");
System.exit(0);
}
}
else
{
LoginView.this.dispose();
MainView mv=new MainView();
mv.initView();
mv.setVisible(true);
//JOptionPane.showMessageDialog(null, "您的用户名或者密码输入正确!");
}
System.out.println("id="+id+"pw="+pw);
}
});
btnNewButton.setBounds(75, 198, 93, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("\u53D6\u6D88");
btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 12));
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//单击事件
System.exit(0);
}
});
btnNewButton_1.setBounds(260, 198, 93, 23);
contentPane.add(btnNewButton_1);
JLabel label = new JLabel("\u6B22 \u8FCE \u767B \u5F55 \u5B66 \u751F");
label.setFont(new Font("宋体", Font.PLAIN, 17));
label.setBounds(131, 21, 186, 23);
contentPane.add(label);
JLabel label_1 = new JLabel("\u7BA1 \u7406 \u7CFB \u7EDF");
label_1.setFont(new Font("宋体", Font.PLAIN, 17));
label_1.setBounds(156, 54, 130, 23);
contentPane.add(label_1);
JLabel lblAdmin = new JLabel("\u5982\u679C\u60A8\u9996\u6B21\u767B\u9646\u7528\u6237\u540D\u4E3A admin \u5BC6\u7801\u4E3A\u7A7A\uFF01");
lblAdmin.setForeground(Color.RED);
lblAdmin.setBounds(86, 173, 267, 15);
contentPane.add(lblAdmin);
}
protected Student StudentDao(String id) {
// TODO Auto-generated method stub
return null;
}
}