Java课程设计---实现登录(1)

 1、新建登录窗口类:LoginView.java

以下是自动生成的代码

package com.system.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

/*
 * 项目名称: 
 * 
 * 文件名称为:LoginView.java
 * 文件创建人:daxiang
 * @author daxiang
 * @version 
 * @time  2018年6月11日 下午8:09:51
 * @copyright daxiang
 */
public class LoginView extends JFrame {

	private static final long serialVersionUID = -8428252014482273530L;
	
	private JPanel contentPane;

	/**
	 * 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() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
	}

}

2、通过可视化工具制作登录界面

界面完整代码(由于是生成的,仅供参考)

package com.demo.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

/*
 * 项目名称: 
 * 
 * 文件名称为:LoginView.java
 * 文件创建人:daxiang
 * 
 * @author daxiang
 * @version 
 * @time  2018年6月13日 下午7:58:47
 * @copyright daxiang
 */
public class LoginView extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;

	/**
	 * 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("系统登录窗口");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("登录名");
		label.setBounds(50, 52, 72, 18);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("密  码");
		label_1.setBounds(50, 111, 72, 18);
		contentPane.add(label_1);
		
		textField = new JTextField();
		textField.setBounds(135, 49, 199, 24);
		contentPane.add(textField);
		textField.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(136, 108, 198, 24);
		contentPane.add(passwordField);
		
		JButton button = new JButton("确 认");
		button.setBounds(50, 187, 113, 27);
		contentPane.add(button);
		
		JButton button_1 = new JButton("取 消");
		button_1.setBounds(221, 187, 113, 27);
		contentPane.add(button_1);
	}
}

  

3、对相应组件添加操作

   (1)获取用户名和密码

    说明:双击组件即可添加监听

  

 “确定”按钮监听部分代码

	button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("输入的用户名"+textField.getText());
				System.out.println("输入的密码"+new String(passwordField.getPassword()));
			}
		});

  (2)对取消按钮添加操作

 “取消”按钮监听部分代码

button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField.setText("");
				passwordField.setText("");
			}
		});

效果

posted @ 2018-06-11 20:11  大象老师  阅读(1687)  评论(0编辑  收藏  举报