第二周 登录小界面

在对java的第二周作业中我们主要完成一个常用界面组件的使用,在制作的登录界面中我运用组合框(JComboBox)来完成下拉菜的的选择账号,但是目前还没有研究出密码的匹配及登录只是简单地在确定按钮上进行了监听从而可以弹出登陆成功的信息框,我会不断完善不断做出令自己满意的程序。

import java.awt.FlowLayout;..

public class login {

private JFrame frame;
private JComboBox username = new JComboBox();
private JPasswordField password = new JPasswordField();
private JButton ok = new JButton("确定");
private JButton cancel = new JButton("取消");

public login()
{
	makeFrame();
	java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    frame.setBounds((screenSize.width-481)/2, (screenSize.height-338)/2, 280,180);  //设置窗体的位置及大小
}
private void makeFrame()
{  

	frame = new JFrame("系统登录");

	JPanel fieldPanel = new JPanel();
	fieldPanel.setLayout(null);
	JLabel l1 = new JLabel("用户名:");
	l1.setBounds(30, 20, 50, 20);
	JLabel l2 = new JLabel("密    码:");
	l2.setBounds(30, 60, 50, 20);
    username.addItem("20143303");
	username.addItem("33012014");
	username.addItem("20153303");
	username.setEditable(true);
	password = new JPasswordField(10);

	
	fieldPanel.add(l1);
	fieldPanel.add(l2);
	username.setBounds(106,20,120,20);
	password.setBounds(106,60,120,20);
	fieldPanel.add(username);
	fieldPanel.add(password);
	frame.add(fieldPanel,"Center");

	username.setEditable(true);
	//底部按钮
	JPanel buttonPanel = new JPanel();
	buttonPanel.setLayout(new FlowLayout());
	buttonPanel.add(ok);
	buttonPanel.add(cancel);
	frame.add(buttonPanel,"South");
	
	ok.addActionListener(new ActionListener(){

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			JOptionPane.showMessageDialog(frame,"登陆成功","登录",JOptionPane.WARNING_MESSAGE);
		}});
	cancel.addActionListener(new ActionListener(){

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			System.exit(0);
		}});


	frame.pack();
	frame.setVisible(true);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
	// TODO Auto-generated method stub
    new login();
}

}
照片墙

posted @ 2016-03-22 16:58  14软三2014330326杨佳  阅读(141)  评论(0编辑  收藏  举报