swing(二)可视化方式拖拽组件快速生成登陆界面

使用eclipse的WindowBuilder插件的可视化方式拖拉组件快速生成登陆界面

安装好WindowBuilder插件之后,在eclipse里面使用快捷键ctrl+N ==> new JFrame:

给面板设置需要的布局,然后通过拖拉组件画出登陆界面:

swing(二)可视化方式拖拽组件快速生成登陆界面

然后再修改一下代码,编写登陆按钮和重置按钮的方法:

public class LoginFrame extends JFrame {

	private JPanel contentPane;
	private JTextField userNameTf;
	private JPasswordField passwordField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginFrame frame = new LoginFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginFrame() {
		setResizable(false);
		setTitle("登陆界面");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(600,400);
		this.setLocationRelativeTo(null);
		
//		setBounds(100, 100, 600, 400);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel userNameLbl = new JLabel("用户名:");
		userNameLbl.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		userNameLbl.setBounds(100, 70, 93, 40);
		contentPane.add(userNameLbl);
		
		userNameTf = new JTextField();
		userNameTf.setBounds(239, 75, 239, 30);
		contentPane.add(userNameTf);
		userNameTf.setColumns(10);
		
		JLabel passwordLbl = new JLabel("密码:");
		passwordLbl.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		passwordLbl.setBounds(100, 137, 93, 40);
		contentPane.add(passwordLbl);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(239, 142, 239, 30);
		contentPane.add(passwordField);
		
		JButton loginBtn = new JButton("登陆");
		loginBtn.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		loginBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();
			}
		});
		loginBtn.setBounds(132, 238, 113, 40);
		contentPane.add(loginBtn);
		
		JButton resetBtn = new JButton("重置");
		resetBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				reset();
			}
		});
		resetBtn.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		resetBtn.setBounds(331, 238, 113, 40);
		contentPane.add(resetBtn);
		
	}
	//登陆
	public void login() {
		if(userNameTf.getText().equals("admin")&&String.valueOf(passwordField.getPassword()).equals("123456")) {
			JOptionPane.showMessageDialog(null, "登陆成功");
		}else {
			JOptionPane.showMessageDialog(null, "登陆失败");
		}
	}
	//重置
	public void reset() {
		userNameTf.setText("");
		passwordField.setText("");
	}
}

即可快速生成简单的登陆界面。

swing(二)可视化方式拖拽组件快速生成登陆界面

上一篇:Linux iostat命令详解


下一篇:基于PHP的微信支付教程